Write a code to determine the total number of stops an elevator would take to serve N number of people.



Write a code to determine the total number of stops an elevator would take to serve N number of peop..

Answer / Sumit Yadav

```cn#include <stdio.h>nnint elevators(int people[], int floor, int num_people)n{n int count = 0;n for (int i = 0; i < num_people; ++i) {n count += abs(floor - people[i]);n }n return count * 2;n}nnint main()n{n int people[] = {1, 3, 5, 7, 9};n int num_people = sizeof(people) / sizeof(people[0]);n printf("Total number of stops: %d
", elevators(people, 5, num_people));n return 0;n}n```nThe code assumes the elevator starts at floor 5 and serves people in the order they are listed. The total number of stops is calculated as twice the sum of the absolute differences between the current floor and each person's desired floor.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

Write c-code for 5+55+555+5555+55555+555555+5555555. Output will be it's answer...

4 Answers   TCS,


what will be the output off the following program? #include<stdio.h> int main() { int a; a=015+0*71+5; printf("%d,a"); return0; }

9 Answers   HCL,


int main() { Int n=20,i; For(i=0;i<=n;i--) { Printf(“-“); Return 0;

1 Answers  


how 2 compile & execute c program with out using editor?

2 Answers   HP,


Average of a couple 10 years ago was 25. The average remains same after having a child and twins after 3 years. What is the present age of the first child

10 Answers   IBM, Infosys,


What is information technology.

1 Answers  


What are the concepts introduced in OOPs?

3 Answers  


What is the difference between void main() and void main (void) give example programme?

0 Answers  


What is the difference between malloc() and calloc()?

4 Answers  


Write a C Programm.. we press 'a' , it shows the albhabetical number is 1, if we press 'g' it shows the answer 7.. any can help me

7 Answers  


What are global variables and how do you declare them?

1 Answers  


what information does the header files contain?

6 Answers   BSNL, Cisco, GDA Technologies,


Categories