Thread: syntax confusion with array/pointer

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    syntax confusion with array/pointer

    Hello guys -- I have problem. There is specific function within my code that I'm having a hard time reconfiguring. In the "display_temps" function, I want to print a "+" beside the temp if it is higher than the average of the temperatures. I know that it's because of my comparison of the array/pointer with the int variable avg but I don't know what step to take in order to get it to do what I want. can anyone help.

    **my code compiles, but only with incorrect results**

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    //Function Prototype
    
    void printHeading ();
    
    void input_temps( int temp_array[], int n );
    
    void display_temps( int temps[], int num, int no_of_temps );
    
    void calculate_avg( int temps[], int no_of_temps );
    
    
    
    int main()
    
    {
    	
    	
    
       int* temps, count;
       int no_of_temps, no_of_bytes;
    
    
       
    		printHeading ();
    
    
    	    printf("How many days did you collect temperatures in a month: ");
    			
    			scanf("%d", &no_of_temps);
    	   
    		no_of_bytes = no_of_temps*sizeof(int);
    	    
    		temps = (int*)malloc(no_of_bytes);
    		
    		
    
    	    
    		input_temps(temps, no_of_temps);
    		
    		calculate_avg(temps, no_of_temps);
    		
    		display_temps(temps, no_of_temps, no_of_temps);
    
    
    }
    
    
    //********************************
    // Temp. input function
    //********************************
    
    
    void input_temps(int temp_array[], int n)
    
    {
    
    
       int i;
       printf("\n\nEnter the %d temperatures:\n\n", n);
       for (i=1; i<=n; i++)
          scanf("%d", &temp_array[i]);
    
    	  
    }
    
    
    //***************************************
    // Display temp. function
    //***************************************
    
    void display_temps( int temps[], int num, int no_of_temps )
    
    {
    
    
       int i, total;
       int avg;
       
       
       for ( i = 1 ; i <= num ; i++ ) {
          
    	//printf("\n\n\t%d \t%d", i , temps[i]);
    	 
    	 
    	 total += temps[i];
    	 
    	 avg = ( total / no_of_temps );
    	 
    
    	if ( avg < temps[i] ) 
    	
    		printf("\n\n\t%d \t%d +", i, temps[i]);
    	
    	
    		else {
    			printf("\n\n\t%d \t%d ", i, temps[i]);
    
    	}
    }
    
    }
    
    
    //***************************************
    // Calculate avg. temp
    //***************************************
    
    void calculate_avg( int *temps, int no_of_temps)
    
    {
    
       int i, total;
       float avg;
       total = 0;
    
     
       for( i = 1; i <= no_of_temps; i++) {
       
          total += temps[i];
    }
    
    
    
       avg = ( total / no_of_temps);
       
       
    
       printf("\nThe average temperature is  %.2f" , avg);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Not sure what you mean but if all you want is to print a + just put it at the beginning of the printf in which you are printing the value:

    double temp = 5.4;
    printf("+%lf",temp);
    Last edited by claudiu; 11-21-2010 at 06:14 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Looking at your code... What I'd do is have Calculate_avg return the average temp as a float value, then pass that value into Display_temps as a parameter. Once you have a known average at the top of Display_temps it's just a simple comparison to add a + or - to the printout.
    Last edited by CommonTater; 11-21-2010 at 06:18 AM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have two major problems with your current code. The first is that your loops are running from 1..n instead of from 0..n-1. In C, array indexes begin at 0 and go to 1 less than the number of elements. Thus, your for loops should look like this:
    Code:
    for (i = 0; i < num; i++)
    The second is that you never initialized total in display_temps, so you're adding the temperatures to some garbage value that's hanging around on the stack. Regardless of this second problem, I strongly suggest you take Tater's advice and have calculate_average return the average value and pass that in to your display_temps function, which doesn't need both num and no_of_temps, since they're the same.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    @commontater I tried returning avg and passing it as a parameter but it didn't work -- I'll try something else.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Or post your new code and we will help you track down your problems with Tater's suggestion (which is much better from a design standpoint).

    As an amendment to my previous post, even if you do initialize total, your average in display_temps isn't correct. When i is 0, you have only 1 element in your total, but you divide it by the number of total temperatures. Let's say there were the following 5 temps: 10, 20, 30, 40, 50. Your average on the first iteration would be 10/5 = 2. The second iteration would be (10+20)/5 = 6, and so on. The "average" would be changing each time and would only be correct on the last iteration, but at that point it's too late.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    @anduril462 I see what you're saying about the first part but I chose to do that so when I print out my input it will show 1 first, not 0. Secondly, I tried passing "return avg;" from calculate_avg and I got nothing but errors saying return with a value in function returning void.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    Here is the revised code

    Code:
    //Function Prototype
    
    void printHeading ();
    
    void input_temps( int temp_array[], int n );
    
    void display_temps( int temps[], int num, float avg);
    
    void calculate_avg( int temps[], int no_of_temps );
    
    
    
    int main()
    
    {
    	
    	
    
       int* temps, count;
       int no_of_temps, no_of_bytes;
    
    
       
    		printHeading ();
    
    
    	    printf("How many days did you collect temperatures in a month: ");
    			
    			scanf("%d", &no_of_temps);
    	   
    		no_of_bytes = no_of_temps*sizeof(int);
    	    
    		temps = (int*)malloc(no_of_bytes);
    		
    		
    
    	    
    		input_temps(temps, no_of_temps);
    		
    		calculate_avg(temps, no_of_temps);
    		
    		display_temps(temps, no_of_temps, avg);
    
    
    }
    
    
    //********************************
    // Temp. input function
    //********************************
    
    
    void input_temps(int temp_array[], int n)
    
    {
    
    
       int i;
       printf("\n\nEnter the %d temperatures:\n\n", n);
       for (i=1; i<=n; i++)
          scanf("%d", &temp_array[i]);
    
    	  
    }
    
    
    //***************************************
    // Display temp. function
    //***************************************
    
    void display_temps( int temps[], int num, float avg)
    
    {
    
    
       int i;
       
       
       for ( i = 1 ; i <= num ; i++ ) {
          
    	printf("\n\n\t%d \t%d", i , temps[i]);
    	
    	 
    	 
    	 //total += temps[i];
    	 
    	 //avg = ( total / no_of_temps );
    	 
    
    	
    	
    		//printf("\n\n\t%d \t%d +", i, temps[i]);
    	
    	
    		//else {
    			//printf("\n\n\t%d \t%d ", i, temps[i]);
    
    	
    }
    printf("%.2f", avg);
    }
    
    
    //***************************************
    // Calculate avg. temp
    //***************************************
    
    void calculate_avg( int *temps, int no_of_temps)
    
    {
    
       int i, total;
       float avg;
       total = 0;
    
     
       for( i = 1; i <= no_of_temps; i++) {
       
          total += temps[i];
    }
    
    
    
       avg = ( total / no_of_temps);
       
       
    
       printf("\nThe average temperature is  %.2f" , avg);
       
       return( avg);
    
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ceddsoboss View Post
    @commontater I tried returning avg and passing it as a parameter but it didn't work -- I'll try something else.
    Did you try it like this...
    Code:
    //***************************************
    // Calculate avg. temp
    //***************************************
    
    int calculate_avg( int *temps, int no_of_temps)
    {  int i, total = 0;
        for( i = 0; i < no_of_temps; i++) 
           total += temps[i];
        return (total / no_of_temps); }
    Your problem was that you had marked the function as "void" which doesn't return anything.

    Then to display them...
    Code:
    //***************************************
    // Display temp. function
    //***************************************
    
    void display_temps( int temps[], int num, int avg)
      { int i;
         for ( i = 0 ; i < num ; i++ ) 
            printf("%d \t%d", i + 1 , temps[i]); 
         printf("\n Average = %d", avg); }
    and in main....
    Code:
    int main(void)
     { int temps, count, int no_of_temps, no_of_bytes, avg;
    
        printHeading ();
        printf("How many days did you collect temperatures in a month: ");
        scanf("%d", &no_of_temps);
        no_of_bytes = no_of_temps*sizeof(int);
        temps = (int*)malloc(no_of_bytes);
        
        input_temps(temps, no_of_temps);
        avg = calculate_avg(temps, no_of_temps);
        display_temps(temps, no_of_temps, avg);   }
    Last edited by CommonTater; 11-21-2010 at 03:01 PM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I tried passing "return avg;" from calculate_avg and I got nothing but errors saying return with a value in function returning void.
    This is the compiler telling you you're not doing something correctly. Listen to it and learn. If you read it carefully, it says you are returning a value in a function that is declared to return void, or nothing. You need to tell the compiler your function should return a float value, by declaring it like this:
    Code:
    float calculate_avg( int *temps, int no_of_temps)
    . Then you can safely use "return avg;".

    I chose to do that so when I print out my input it will show 1 first, not 0.
    That's nice and all, but it's just plain wrong. I told you that in my previous post, and said it was a "major problem", not a suggestion. You can't just have C change the way it indexes arrays, it's from 0..n-1. If you want to print output starting at 1, you're just going to have to deal with writing something like "printf("%d", i+1)" all over. As is, you have basically written buffer overflows into your program simply for output aesthetics. This may not seem like an issue now, but as your programs grow in complexity, you will find yourself tromping all over other values and causing nightmarish problems to debug.

    Now, with your latest version, there are still a few kinks to work out:
    1. As I mentioned above, you need to change calculate_avg to have a return type of float.
    2. You need to fix your array indexes as I mentioned in my previous post. Adjust your output as necessary.
    3. You declared main to return an int, but there is no return statement in there. Finish the function with a "return 0;".
    4. You need to declare a float variable in main called avg, and assign it the return value of calculate_avg, like so:
    Code:
    float avg;
    ...
    avg = calculate_avg(...);

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    thanks anduril, I appreciate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM