problems in Array

Oct 1, 2014 at 9:48am
I'm having problem here, i can't print out the 3rd - 5th day... i can only print the first 2 days.... how do i fix it? i changed practically almost everything.

Day 1 2 3 4 5
route
0 8 12 9 7 10
1 5 7 3 0 4
2 20 15 18 21 14
3 6 9 5 8 11

And can you give me some ideas of how to...
1. Print the total passengers for day 1.
2. Print the total number of passengers for the route stored in row 0.
3. Print the maximum number of passengers for the route stored in row 3.
4. Print the minimum number of passengers for the 4th day.
5. Print the average number of passengers for all days and all routes.

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
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int route, day=1;
    int bus[4][5];
    
    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(route=1; route<=4; route++)
     {
        for(day=1; day<=5; day++)
        {
            printf("[%d][%d]=%2d", route, day, bus[route][day]);
            printf("\n");
        }
     }  
                  

    system("PAUSE");
    return 0;
}


Oct 1, 2014 at 9:55am
Note that array indices starts at zero.
Oct 1, 2014 at 9:59am
yes... i get that. but i wanted the out put to be day1 instead of day 0.
Thats why i put in one instead of 0 in line 6.

is there another way?
Oct 1, 2014 at 12:32pm
You can loop day from 0-4 and then simply add +1 when you print it.

The alternative would be to have it as you have it right now only that you subtract 1 on each array access but I think this way is more error prone. Getting the wrong output is obvious and simple to fix while getting array indices wrong could lead to strange errors that are not always easy to spot.
Last edited on Oct 1, 2014 at 12:34pm
Oct 1, 2014 at 12:52pm
Oh Thanks! ^^
Topic archived. No new replies allowed.