Calculate for total in a row

Hey guys/girls! I did a program to be able to input the day and the route for a bus in a week... and i got all the input and out put.
but i wanted to calculate all the passengers in only monday(row 31)... but i cant come up with a good equation that will plus every passenger only in monday.
Can anyone pls help me... its just one sentence and it will be complete.

Also if u can do it...
how can i find the maximum or the minimum in a row/column?
and what do i have to do if i want to find the average of every route and day?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int route, day=0;
    int bus[4][5];
    int sum;
    
    for(day=1; day<=5; day++)
    {
        printf("Give the number of passengers for day %d: ", day);
        for(route=1; route<4; route++)
          { scanf("%d", &bus[route][day]);
          }
    }
     printf("\n");
     for(day=1; day<=5; day++)
     {
        for(route=1; route<4; route++)
          { 
             printf("[%d][%d]=%2d", route, day, bus[route][day]);
             printf("\n");
          }
     }  
                  
     for(day=1; day<1; day++)
     {
        for(route=1; route<=4; route++)
         {
           sum=;
         }
           bus[route][day]= sum;
     }
          
           printf("Monday passenger is: %d\n", sum);
                  
                  system("PAUSE");
                  return 0;
}
Last edited on
Alright, the way I understand this you have a bus that hits four routes in one day, then does that five days each week, the number of passengers is represented by the 2d array. Just to make sure.

If you want to total the passengers for all routes on Monday, you have the loops set up almost correctly (where you initialize day and route right now, they don't start at the beginning of the array), you just need to keep a running total, adding where you are in the array for each iteration of the for loop.

Also, what is this line doing?

bus[route][day]= sum;
Last edited on
yea... i should probably get rid of bus[route][day]

I'm also having problems with entering the day 4... it just skipped day four and went to the line "Give number for passenger" in day 5. i dont know where to fix. the code does not appear to be wrong in anyway.

so... sum=sum+bus[route][day]??

the tried putting in the sum but the answer was a negative of some very big number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int route, day=0;
    int bus[4][5];
    int sum;
    
    for(day=1; day<=5; day++)
    {
        printf("Give the number of passengers for day %d: ", day);
        for(route=1; route<4; route++)
          { scanf("%d", &bus[route][day]);
          }
    }
     printf("\n");
     for(day=1; day<=5; day++)
     {
        for(route=1; route<4; route++)
          { 
             printf("[%d][%d]=%2d", route, day, bus[route][day]);
             printf("\n");
          }
     }  
                  
     for(day=0; day<1; day++)
     {
        for(route=0; route<=4; route++)
         {
           sum=sum+bus[route][day];
         }
     }
          
           printf("Monday passenger is: %d\n", sum);
                  
                  system("PAUSE");
                  return 0;
}


Thanks!
Last edited on
In lines 10, 13, 18 and 20 you initialize your loops at 1, in corresponding summing loops lines 27 and 29 you initialize them to 0. . .remember, first array value is at [0][0].

also:

Hint: looking at lines 6 and 8, What values are in route, day, sum before you use them elsewhere, and why?

Specifically, what is the value of sum before line 31?
Last edited on
Have you considered something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
enum Weekdays{
          Monday,
          Tuesday,
          Wednesday,
          Thursday,
          Friday
};
enum BusRoutes{
          A,
          B,
          C,
          D
};

//then for your loops:

          for (day = Monday, day <= Friday, day++)
          {
                  for (route = A, route <= D, route ++)
                  {
                           //some processing here;
                   }
         }


So you don't have to mentally figure out which integer day stands for which day of the week or which bus route?
Topic archived. No new replies allowed.