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

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    8

    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!
    Last edited by Sgemin_001; 11-19-2008 at 02:19 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #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)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. Replies: 4
    Last Post: 11-07-2001, 02:46 PM