Thread: Help with C programming Mathematical Calculations

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    Help with C programming Mathematical Calculations

    I have been doing c programming for about 3 days now and am pretty confused as to why this is not working. It basically is asking to estimate the temp inside a freezer after a certain time (t) power outage...

    here is what i have so far, anyhelp is appreciated...

    [tag]
    Code:
    
    #include <stdio.h>
    int
    main(void)
    
    {
    
    	int time; /*time */
    	int temp; /* temperature */
    
    	
    	printf("EE233 Spring 2010, P1: Power Failure Freezer Temperature Estimate."); /*Greeting */ 
    	printf("How long (hours and minutes) since the power failure to the freezer?>"); 
    	scanf("%d", &time); 
                    temp = (4*(time*time)/( time +2))-20; /*Calculate the temperature */ 
    	printf("The estimated temperature inside your freezer is %d degrees Celsius.", temp ); 
    
    
    		
    		return (0);
    }
    [/tag]

    Not asking anyone to do this for me, just give me some hints on where to make this thing work...

    Thanks for any help.
    Last edited by StangFan; 01-13-2010 at 01:42 PM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Well it's hard to answer that question without knowing what's wrong with it. If your math is off I'd recommend making temp a double. And then use double values in your math and your compiler will automatically do casting as necessary:

    Code:
    double temp;
    
    /*....*/
    
    temp = (4.0*(time*time)/( time +2.0))-20.0;

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your equation needs to use either hours or minutes or some ONE unit of time. You've confused the poor equation!

    "hours and minutes"

    time * time

    You know better!

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Ok, i've been working on this a bit and this is what i have now... still have a couple errors:

    1) Warning 1 warning C4313: 'printf' : '%d' in format string conflicts with argument 1 of type 'char [79]' c:\documents and settings\fst50svt\my documents\visual studio 2005\projects\p1\p1\p1.c 25

    2)Warning 2 warning C4313: 'printf' : '%d' in format string conflicts with argument 2 of type 'char [79]' c:\documents and settings\fst50svt\my documents\visual studio 2005\projects\p1\p1\p1.c 25

    3)Error 3 error C2296: '&' : illegal, left operand has type 'int *__w64 ' c:\documents and settings\fst50svt\my documents\visual studio 2005\projects\p1\p1\p1.c 27


    4) Error 4 error C2065: 'time' : undeclared identifier c:\documents and settings\fst50svt\my documents\visual studio 2005\projects\p1\p1\p1.c 29


    Here is the code...



    Code:
    #include <stdio.h>
    #define HOUR 60 //conversion constant
    
    int
    main(void)
    
    {
    
    	int hour=0; //hour input
    	int minutes=0; //minute input
    	double temp=0; //temperature output
    
    
    
    	printf("EE233 Spring 2010, P1: Power Failure Freezer Temperature Estimate. \n"); //Greeting
    	printf("How long (in hours and minutes) since the power failure to the freezer?> %d %d"); //prompts for input from user
    	
    	scanf("%d" "%d", &hour &minutes); //input from user in hours and minutes
    
    	time = hour + minutes; //convert hours to minutes
    
    	
    	temp = ((4*(time*time)/(time+2))-20); //Calculate the temperature 
    	printf("The estimated temperature inside your freezer is %3.1f degrees Celsius.", temp); //displays results
    
    
    	return (0);
    
    }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The first two are because of this red part:
    Code:
    printf("How long (in hours and minutes) since the power failure to the freezer?> %d %d");
    The & error is probably because of this red part:
    Code:
    scanf("%d" "%d", &hour &minutes);
    WRT to using scanf(), make sure you read and understand this:
    STDIN pitfalls

    I'm gonna let you contemplate what this might mean:
    'time' : undeclared identifier
    ...if you really don't get it, ask again
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    The & error is probably because of this red part:
    Code:
    scanf("%d" "%d", &hour &minutes);
    If you put two string literals next to each other, the preprocessor merges them into a single literal. That part's okay, if a bit weird. What I see there is a missing comma between &hour and &minutes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by StangFan View Post
    I have been doing c programming for about 3 days now and am pretty confused as to why this is not working. It basically is asking to estimate the temp inside a freezer after a certain time (t) power outage...
    I've hilighted the problem in bold above. Never ever under any circumstance, just say that something "is not working" or "does not work", or "did not work" in a programming forum.
    It's the most useless, fustrating, and downright infuriating thing you can do.
    Instead, always explain in detail exactly what happens, or what errors you get!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Quote Originally Posted by MK27 View Post
    The first two are because of this red part:
    Code:
    printf("How long (in hours and minutes) since the power failure to the freezer?> %d %d");
    The & error is probably because of this red part:
    Code:
    scanf("%d" "%d", &hour &minutes);
    WRT to using scanf(), make sure you read and understand this:
    STDIN pitfalls

    I'm gonna let you contemplate what this might mean:
    'time' : undeclared identifier
    ...if you really don't get it, ask again
    i need to put double time; up at the top?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-21-2009, 08:29 PM
  2. Mathematical Operation
    By madahmad1 in forum C Programming
    Replies: 29
    Last Post: 07-30-2008, 09:58 AM
  3. How do I get these calculations correct?
    By nadeni0119 in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2003, 11:09 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. mathematical calculations
    By Unregistered in forum Windows Programming
    Replies: 7
    Last Post: 01-10-2002, 11:24 PM