Finding largest and smallest numbers in an array and other Qs

Hi all,
I am a beginner at C++ so please do bear with me if I do not understand your answers.
Firstly, I am making a simple program for checking how long my bus takes to reach home. This would be my database for my 4 buses. I think its a 2-deminesion array.
1.I want to know how I can only allow the user to 4 buses, I cannot int or char to declare the bus numbers(15,27,45,46) I have to put a "num" in front of it and I also want to know how I can declare my time input. In order for me to complete my program the user has to input the bus no. and the time period(which is every 1/2 hour e.g 1:00 or 6:30). Also, after I've declared both time period and the bus no. how can I code the program such that when the user inputs a bus no. and a time period the program will display the shown integer?

1
2
3
4
5
6
7
8
9
  //Database for all timings for the 4 buses.
#include <stdio.h>

int timePeriod[={1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30,7:00};

int bus15Timing[]={13,13,15,15,14,14,12,13,12,24,28,28,28};
int bus27Timing[]={18,18,15,15,14,21,19,11,13,26,25,25,25};
int bus45Timing[]={15,15,16,16,18,22,19,18,19,24,24,23,25};
int bus46Timing[]={22,23,23,16,16,12,11,12,15,30,26,25,25};


Next
I need to display the shortest and longest time taken to reach home for all time periods and buses. how do I do so?

Overall,
I just want to know how to declare my bus numbers and time periods so that the user will enter an invalid input an cause my program to fail and also how to get
the largest and smallest int in an array and display like the shortest/longest time for each specific bus.

Thank You for your time.

This is what my menu looks like.
[code}
#include <stdio.h>
void main (void)

{
extern int bus15Timing[], bus27Timing[], bus45Timing[], bus46Timing[];//declare as external global variable(All Options)
int num15,num27,num45,num46;//declare bus numbers(Option 1)
int averagetime;//Variable for Option B(done)
char selection;//Options 1,2,3,4,5,6



printf("=================================Bus Travel Viewer=============================\n");
printf("1:Display Buslist.\n");
printf("2:Display the daily average travel time.\n");
printf("3:Display predicted shortest travel time.\n");
printf("4:Display the longest/shortest travel time for all time periods.\n");
printf("5:Display the longest time to reach home from 1pm-7pm.\n");
printf("6:Please select Option 1,2,3,4,5,6.\n");
scanf("%c",&selection);


switch (selection)
{
case'1':
{

char num15,num27,num45,num46;//ensure user does not input wrong bus number
printf("The following are the bus numbers 15,17,45,46.\n");
printf("Please type in your bus number.\n");
scanf("%d",&num15,&num27,&num45,&num46);
switch(num15,num27,num45,num46)

case'15':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;

case'27':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;

case'45':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;

case'46':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;
}





break;
case'2':
averagetime=(bus15Timing[0]+bus27Timing[0]+bus45Timing[0]+bus46Timing[0])/4;
printf(" The daily average travel time is %d\nmins",averagetime);
break;
case'3':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;
case'4':;

//Finding the largest/longest time taken to reach home for each bus
default:printf("Sorry , you have entered an invalid key.\n");
}
}
[/code}

Some questions for you:
- Have you learned about pointers?
- Have you learned about structures or classes?

Regarding average/min/max, there are some different statistics here:
- Average/min/max for a specific bus during all times of the day.
- Average/min/max of all the buses for a specific time of day.
no I have not learn structs for the average timing I'm using the first timing after school which is 1pm
Line 4: Your values are not a valid integers. You can't use a : in an integer. I explained this in your other thread.
http://www.cplusplus.com/forum/beginner/135900/

I need to display the shortest and longest time taken to reach home for all time periods and buses

First, you need to write a functions to find the shortest and longest trip time for a single bus route. Then you need to call those functions for each of your four bus routes, keeping track of which of the 4 is shortest and longest.

Line 30: Your scanf statement has only one input descriptor "%d", but you're passing pointers to 4 variables. The remaining 3 variables won't be initialized. You also probably don't want to put the user's bus number in the variable called bus15. I gave you an example in your other thread of a function to prompt for bus number.

Line 31: You don't put 4 values in the switch() clause. This is a misuse of the comma operator. http://www.cplusplus.com/doc/tutorial/operators/

Line 33,38,43,48: You're using character literals for case values, while your switch variable is an int. These will not match. Character literals (ASCII values) are not the same as the binary value of your bus number (an int).

You haven't taken the advice given in the other thread to avoid nested switch statements. Nested switch statements make your code hard to follow.

Lines 34-49: You're doing exactly the same thing in each branch of the inner switch statement. Why are these lines even in a switch statement?

Your code tags are defective. They must end with a ] not }
Code tags make your code much easier to read and respond to.
Please use them correctly. Hint: You can edit you post and correct them.
Topic archived. No new replies allowed.