Thread: I need help with a lab... very easy stuff

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    I need help with a lab... very easy stuff

    Well, I'm in my first semester of computer science class. We've just started learning functions, and had to have four separate ones in this lab. It seems to look okay, but I can't figure out whats wrong. The goal of the lab is to make a table that shows Fahrenheit values you input, a conversion to celsius, and its supposed to measure the distance of both to their freezing points. Could anyone help me troubleshoot what I've been doing wrong? Also, I'd appreciate an explanation as to why you changed or corrected it. Thanks a bunch





    Code:
    /*Lab 7*/
    
    #include <stdio.h>
    
    greet();
    calc();
    display();
    
    /*The first function is used to declare the variables being used in the program. 
    It also states the three other functions used in this program, and initiates them.*/
    int main() 
    {
        //Input variables
    
    	
    	greet();
    	calc();
    	display();
    	
    	return 0;
    }
    
    /*This function is used to prompt the user to input their values so calculations can begin*/
    
    greet()
    
    {
    
        float fahr1, fahr2, fahr3, fahr4;
    
    	//Put a heading to explain what the program is used for
    	
    	printf("This program is designed to convert a given degree in farenheit to celsius. This");
        printf("program is also used to find out how many degrees away the entered degree is "
               "from it's freezing point in both farenheit and celsius\n\n\n");
    	
    	//Assign values to variables
    	printf("Type your first degree in Farenheit: ");
    	scanf("%f", &fahr1);
    	printf("\n\nType your second degree in Farenheit: ");
    	scanf("%f", &fahr2);
        printf("\n\nType your third degree in Farenheit: ");
        scanf("%f", &fahr3);
    	printf("\n\nType your fourth degree in Farenheit: ");
    	scanf("%f", &fahr4);
    	printf("\n\n\n\n");
    	
    	calc(fahr1, fahr2, fahr3, fahr4);
    	display(fahr1, fahr2, fahr3, fahr4);
    	
    }	
    
    /*This next function will be used to carry out the calculations and work out the coversions*/
    	
    calc(fahr1, fahr2, fahr3, fahr4)
    
    {
    	
    	float cel1, cel2, cel3, cel4, diffahr1, diffahr2, diffahr3, diffahr4;
    	
    	//Use equation (5.0/9.0)*(Fahrenheit-32) to convert to celsius
    	
    	cel1= (5.0/9.0)*(fahr1-32);
    	cel2= (5.0/9.0)*(fahr2-32);
    	cel3= (5.0/9.0)*(fahr3-32);
    	cel4= (5.0/9.0)*(fahr4-32);
    	
    	
    	
    	//Farenheit to freezing point distance
    	
    	diffahr1= fahr1-32;
    	diffahr2= fahr2-32;
    	diffahr3= fahr3-32;
    	diffahr4= fahr4-32;
    	
    	display(cel1, cel2, cel3, cel4, diffahr1, diffahr2, diffahr3, diffahr4);
    	
    }
    
    /* This function is used to simply display all of the calculations done above.*/
    
    display(fahr1, fahr2, fahr3, fahr4, cel1, cel2, cel3, cel4, diffahr1, diffahr2, diffahr3, diffahr4)
    {	
    
      
    	/*Table which displays the farenheit degrees entered, their conversions to celsius, 
    	   and the amount of degrees from freezing for both celsius and farenheit */
    	
    	printf("Number     Farenheit     Celsius       Freezing-Fahrenheit     Freezing-Celsius\n");
    	printf("_______________________________________________________________________________\n");
    	printf("1          %f     %f      %f                 %f                  \n", fahr1, cel1, diffahr1, cel1);
    	printf("2          %f     %f      %f                 %f                  \n", fahr2, cel2, diffahr2, cel2);
    	printf("3          %f     %f      %f                 %f                  \n", fahr3, cel3, diffahr3, cel3);
    	printf("3          %f     %f      %f                 %f                  \n", fahr4, cel4, diffahr4, cel4);
    
    
    }
    Last edited by Salem; 10-24-2007 at 01:29 AM. Reason: Wrap long lines

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Yea...and what exactly do you think is wrong with it?

    Do you even know what's wrong with it or just assuming there is something wrong with it.

    Any compilational errors? Incorrect outputs?

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    calc(fahr1, fahr2, fahr3, fahr4){

    This is not the correct way to start a function.

    When calling it you can leave out the data types. But when prototyping it and writing the actual function the data types must be there.

    Your prototype should be:

    calc(float fahr1, float fahr2, float fahr3, float fahr4);

    and your actual function should be:

    calc(float fahr1, float fahr2, float fahr3, float fahr4) {

    When calling a function like calc, then it should be:

    calc(fahr1, fahr2, fahr3, fahr4);


    I haven't looked at your other functions, but correct these bits and see if others need the same.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Code:
    int main() 
    {
        //Input variables
    
    	
    	greet();
    	calc();
    	display();
    	
    	return 0;
    }
    greet() calls to calc on it's own, no point calling calc() in main() (especially with no arguments when it's supposed to take 4). Same story goes with display, which is called by calc -- no need to call it in main() nor in greet().
    And you should define argument types, which are supposed to be float (assuming what your program should do) as JFonseka said.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Are you familiar with arrays? If so, consider using some -- it would reduce your number of variables dramatically. http://www.cprogramming.com/tutorial/c/lesson8.html

    As the other two posters have said, there is a syntactical difference between calling a function and defining or declaring it. When you call a function, you can use constants or named variables in the current scope:
    Code:
    printf("&#37;i %i %i\n", 0, x, y);
    When you implement or prototype a function (in standard C), every variable must have a type before it.
    Code:
    int format_person(char *buffer, const char *name, const char *age, int age);
    The name of a variable in the calling function has no effect on the name of a variable in the function that is called.

    Also see this tutorial: http://www.cprogramming.com/tutorial/c/lesson4.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Ohhhh... I get it. It helps alot more when you have real people helping. My teacher is always occupied with other students questions, so thanks a bunch. I'll be back every now and then to ask for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting to a SQL Server to do simple stuff
    By GDR92 in forum C Programming
    Replies: 4
    Last Post: 01-07-2009, 07:56 AM
  2. Easy, simple programming book needed - HELP!
    By 3boyzmom in forum C Programming
    Replies: 1
    Last Post: 05-05-2008, 09:31 AM
  3. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  4. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM
  5. Lost on the easy stuff! Please Help!
    By Tom_Pearce in forum C++ Programming
    Replies: 1
    Last Post: 09-13-2001, 06:18 PM