# help with array calculations and function return values. I are noob!

• 11-19-2008
Sgemin_001
help with array calculations and function return values. I are noob!
I am trying to work on an assignment for college which is due Friday, but I am having some problems. The program is supposed to calculate the flight times in between two cites of the users choosing. Both times are stored in 2D arrays. The calculation will include layover times as well. I have it set up to display the hours only, since it is giving me a garbage value. I am having trouble using the return values in my functions as well. Source code attached.

Code:

```#include <stdio.h> #include <stdlib.h> //function prototypes// int getNum(void); int checkRange1(int city1); int checkRange2(int city2); int calculateTime(int flyingTime[][6], int layoverTime[][6], int city1, int city2); int displayResult(int totalTime); int main(void) { //declare and initialize variables// char numberIn; //number from keyboard// int numberOut; //stores the string in getNum// int c = '0'; //menu choice// int city1 = 0; int city2 = 0; //city choices// int totalTime = 0; //total time calculated// //arrays for the flying and layover times here// int flyingTime[2][6] = {         {0, 4, 3, 3, 2, 3}, //hours//         {0, 15, 58, 55, 14, 27} //minutes// }; int layoverTime[2][6] = {         {0, 1, 0, 11, 0, 0}, //hours//         {0, 20, 46, 29, 53, 0} //minutes// };         while (c != 4)         {         printf("\t\tAirline Flights\n\n\n");         printf("1. Starting City:\n");         printf("2. Ending City:\n");         printf("3. Calculate Time Between The Cities Chosen:\n");                        printf("4. Exit Program.\n\n");         printf("Option?\n");         printf("\n"); c = getNum();                 //check numbers 1 to 4 using if ststements//                 if (c == 1)                 {                         system("cls");                                        printf("Starting City:\n\n");                         printf("1 - Toronto\n");                         printf("2 - Atlanta\n");                         printf("3 - Austin\n");                         printf("4 - Denver\n");                         printf("5 - Chicago\n");                         printf("6 - Buffalo\n\n");                         city1 = getNum();                         checkRange1(city1);                 }                 if (c == 2)                 {                         system("cls");                         printf("Ending City:\n\n");                         printf("1 - Toronto\n");                         printf("2 - Atlanta\n");                         printf("3 - Austin\n");                         printf("4 - Denver\n");                         printf("5 - Chicago\n");                         printf("6 - Buffalo\n");                         city2 = getNum();                         checkRange2(city2);                         printf("Values chosen: &#37;d, %d", city1, city2);                         printf("\n\n");                 }                                 if (c == 3)                 {                         system("cls");                         calculateTime(flyingTime, layoverTime, city1, city2);                                printf("Total Time Between Cities Chosen: %02d"); displayResult(totalTime);                 }                 if (c == 4)                 {                         system("cls");                         printf("Have A Nice Flight!\n\n");                         break;                 }         } return 0; } //end of main// //getNum() function//                 // Function Description:                 // This function takes in a array of char with 80 characters,                 // and uses fgets(), sscanf() with stdin to get the string from the keyboard                 // Parameters Passed:                 // numberOut as an integer, and numberOut as an array of char                 // Return Values:                 // returns numberOut int getNum(void) {         int numberOut = 0;         char numberIn[80] = {0};         fgets(numberIn, 80, stdin);         sscanf(numberIn, "%d", &numberOut);         return numberOut; } //checkRange() function//                         // Function Description:                 // These functions check that the user is within the 6 number range                 // for the city choices (1 - 6)                 // Parameters Passed:                 // city numbers (city1 and city2), minimum and maximum values are passed as integers                 // Return Values:                 // returns 0 if false, returns 1 if true int checkRange1(int city1) {         if (city1 >= 1 && city1 <= 6)         {                 system("cls");                 printf("\n");                 return 1;         }         else         {                 system("cls");                 printf("Invalid Value. Try Again.\n\n");                 return 0;         } } int checkRange2(int city2) {         if (city2 >= 1 && city2 <= 6)         {                 system("cls");                 printf("\n");                 return 1;         }         else         {                 system("cls");                 printf("Invalid Value. Try Again.\n\n");                 return 0;         } } //calculateTime() function//                         // Function Description:                 // This function takes the two arrays from main() as parameters,                 // and does the addition of the times using a for loop                 // Parameters Passed:                 // The layover array and the flight time array are passed                 // Return Values:                 // totalTime as an integer variable int calculateTime(int flyingTime[][6], int layoverTime[][6], int city1, int city2) { int hours = 0; //hours// int minutes = 0; //minutes// int totalTime = 0;         int value = 0;                for(value = city1; value < city2; ++value)         {                 hours += flyingTime[0][value];                 minutes += flyingTime[1][value];                         if (value != city2 - 1)                 {                 hours += layoverTime[0][value];                 minutes += layoverTime[1][value];                 }         } return hours * 60 + minutes; } int displayResult(int totalTime) {         int displayHours = totalTime / 60;         int displayMinutes = totalTime % 60;         printf("%02d", displayHours);         return 0; }```
Any help would be great!
• 11-19-2008
Salem
> calculateTime(flyingTime, layoverTime, city1, city2);
This returns a result, but you throw it away

> printf("Total Time Between Cities Chosen: &#37;02d");
This prints an integer, but you supply no parameter to print

> displayResult(totalTime);
This could probably be declared void, since the answer is constant, zero, and ignored.
• 11-19-2008
hk_mp5kpdw
#1.
Code:

`printf("Total Time Between Cities Chosen: %02d");`
That bit of code expects another argument/value which is not provided. You probably don't want that %02d there.

#2.
Code:

```int totalTime = 0; //total time calculated// calculateTime(flyingTime, layoverTime, city1, city2);        ... displayResult(totalTime);```
I'm guessing you want to assign the return value of calculateTime to totalTime before you call the displayResult function.

#3. There is little point in having two functions (checkRange1 and checkRange2) that have the exact same code internally and have the same arguments and return value and behavior.

#4. Try to get away from magic numbers (hardcoded values), they are all over your code. If you change the number of items in the arrays for example, then you'll have a difficult time finding all the places in your code where you need to make adjustments.

#5. Your program only works if the starting city is "less" than the ending city... that is, you cannot travel from Buffalo to anywhere else and have the program display a meaningful result.

#6. Is it the intention of the program to treat the cities and there flight times in such a way that a flight from one to the other must travel through all the intervening cities? (i.e. flying from Toronto to Buffalo must travel through Atlanta, Austin, Denver, Chicago)