Thread: averaging

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    averaging

    I am trying to calculate the average of the numbers the user entered but I can't figure out how many numbers the user actually entered to so I can divide by the sum. The user can enter up to 10 numbers or a zero to exit at anytime. I need to be able to calculate the average but not include zero.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
     
    
    int main(void)
    {
      char theNums[10][50];
      int nums[10];
      int i, j, sum = 0;
      int average, qty;
    
      printf("Start by entering up to 10 numbers.  To exit enter a zero.\n");
      printf("\n");
    	
      for(i=0; i < 10; i++)
      {
        printf("Enter a Number: ");
        fgets(theNums[i], 50, stdin);
    		
        j = strlen(theNums[i])-1;
    		
        theNums[i][j] = '\0'; /* get rid of '\n' */
    		
        if(theNums[i][0] == '0')
          break;
      else if(!isdigit(theNums[i][0]))
      {
        printf("%s is not a valid entry.\n", theNums[i]);
        nums[i] = 0;
      }
      else		
        nums[i] = atoi(theNums[i]);
      }
    	
      printf("\n\n");
    	
      for(j=0; j < i; j++)
        sum+=nums[j];
        average = (sum/qty);
    
      printf("The sum of your numbers is %d", sum);
      printf("\n");
    
      printf("The average of the numbers is %d", average);
      printf("\n")
    
      getchar(); /*wait for keystroke before exiting*/
    	
      return 0;
    }

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    make another variable

    int numberCount = 0;

    and everytime you add a number to your array, add one to the counter

    numberCount++;

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    OK, I have the sum and average working. I am trying to display what the user entered for numbers and also sort them. Can someone let me know what I am doing wrong.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
     
    
    int main(void)
    {
      char theNums[10][50];
      int nums[10];
      int i, j, sum = 0;
      float average;
      int numberCount = 0;
    
      printf("Start by entering up to 10 numbers.  To exit enter a zero.\n");
      printf("\n");
    	
      for(i=0; i < 10; i++) 
      {
        printf("Enter a Number: "); /*get the numbers*/
        fgets(theNums[i], 50, stdin);
    		
        j = strlen(theNums[i])-1;
    		
        theNums[i][j] = '\0'; /* get rid of '\n' */
    		
        if(theNums[i][0] == '0') /* exit if zero */
          break;
      else if(!isdigit(theNums[i][0])) /*verify user data is a digit*/
      {
    	printf("%s is not a valid entry.\n", theNums[i]);
        nums[i] = 0;
      }
      else		
        nums[i] = atoi(theNums[i]);
    	numberCount ++;	/*count the numbers being entered*/
      }
    	
      printf("\n\n");
    
      average = 0; /*calculate average*/
      for(j=0; j < i; j++)
        sum+=nums[j];
        average = (sum/numberCount);
    
    
      printf("Here are the numbers you entered:\n"); /*what am i doing wrong here*/
      for (++i;i>=0; ++i)
      printf("%d ",theNums[i]);
      printf("\n");
    
      printf("Here are the numbers you entered sorted from lowest to highest:\n"); /*what am i doing wrong here*/
      for (++i;i>=0; ++i)
      printf("%d ",theNums[i]);
      printf("\n");
    
    
    
      printf("The sum of your numbers is %d", sum); /* display sum*/
      printf("\n");
    
      printf("You have entered %d numbers", numberCount); /*display qty of numbers entered*/
      printf("\n");
    
      printf("The average of the numbers is %.1f", average); /*display average*/
      printf("\n");
    
      getchar();
    	
      return 0;
    }

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Look at your first 'for' loop
    Code:
    for(i=0; i < 10; i++)
    Now look at your second one
    Code:
    for (++i;i>=0; ++i)
    for (where to start; loop while this is true; increment)

    Maybe you want to...
    Start at 0? keep counting all the numbers that have been entered? increment by 1 after each loop?

    Something along the lines of (but maybe not exactly)
    Code:
    for (i=0;i < numberCount;i++)
    Also, if you need to sort, then you actually need some sort of sorting code.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    I made some more changes but still I am having a problem displaying the number enetered and sorted.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
     
    
    int main(void)
    {
      char theNums[10][50];
      int nums[10];
      int i, j, sum = 0, average;
      int numberCount = 0;
    
      printf("Start by entering up to 10 numbers.  To exit enter a zero.\n");
      printf("\n");
    	
      for(i=0; i < 10; i++) 
      {
        printf("Enter a Number: "); /*get the numbers*/
        fgets(theNums[i], 50, stdin);
    		
        j = strlen(theNums[i])-1;
    		
        theNums[i][j] = '\0'; /* get rid of '\n' */
    		
        if(theNums[i][0] == '0') /* exit if zero */
          break;
      else if(!isdigit(theNums[i][0])) /*verify user data is a digit*/
      {
    	printf("%s is not a valid entry.\n", theNums[i]);
        nums[i] = 0;
      }
      else		
        nums[i] = atoi(theNums[i]);
    	numberCount ++;	/*count the numbers being entered*/
      }
    	
      printf("\n\n");
    
      average = 0; /*calculate average*/
      for(j=0; j < i; j++)
        sum+=nums[j];
        average = (sum/numberCount);
    
      printf("Here are the numbers you entered:\n"); /*what am i doing wrong here*/
      for(i=0; i < 10; i++)
      printf("%d ",theNums[i]);
      printf("\n");
    
      printf("Here are the numbers you entered sorted from lowest to highest:\n"); /*what am i doing wrong here*/
      for(i=0; i < 10; i++)
      printf("%d ",theNums[i]);
      printf("\n");
      
      printf("The sum of your numbers is %d", sum); /* display sum*/
      printf("\n");
    
      printf("You have entered %d numbers", numberCount); /*display qty of numbers entered*/
      printf("\n");
    
      printf("The average of the numbers is %.1f", average); /*display average*/
      printf("\n");
    
      getchar();
    	
      return 0;
    }

  6. #6
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I can't compile your code where I am, and I'm not good enough to see all your issues just from your code. It would be helpful if you told us what was going wrong (i.e. what the output is and how it is wrong). Also, you need to learn about sorting, do a search on the board, you will probably find a 'bubble sort' fairly easy to grasp and it should suit what you want to do.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    it doesnt print the numbers the user entered. I must have some bug in there but can't figure out what is it.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm not sure what you doing within the for loop, so here's something that might help you.

    This program asks the user for 2 numbers, one at a time, stores their input in a char array, and converts that to an int in an int array.

    No error checking done, this is for demo purposed only
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char theNums[2][50];
      int  Nums[2];
      int  i;
      
      for (i = 0; i < 2; i++)
      {
        printf ("Enter a number: ");
        fgets(theNums[i], 50, stdin);
        printf ("You entered %s", theNums[i]);
        Nums[i] = strtol(theNums[i], NULL, 10);
        printf ("Thats number %d\n", Nums[i]);
      }
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Averaging just 2 lines of file of integers
    By dispatch4599 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2008, 09:44 PM
  2. Averaging code
    By dead_captain in forum C++ Programming
    Replies: 18
    Last Post: 01-18-2008, 09:03 PM
  3. averaging numbers?
    By rock4christ in forum C Programming
    Replies: 12
    Last Post: 09-09-2006, 06:38 PM
  4. averaging program
    By mattflick in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 09:50 PM
  5. Having trouble with averaging
    By live_2_dream in forum C++ Programming
    Replies: 8
    Last Post: 09-25-2005, 03:45 PM