array help

This is a discussion on array help within the C Programming forums, part of the General Programming Boards category; I am trying to keep the value entered intot the array each time a number is entered and then average ...

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

    array help

    I am trying to keep the value entered intot the array each time a number is entered and then average that number dividing by the count of [i] for the number of times a value was entered into the array. So far I can take the input but it is only held through the first iteration. It appears to be overwritten each time. Please send help to let me know what I am doing wrong.

    #include <stdio.h>

    int main()
    {
    int number[10]; /* array of 10 numbers */
    int count = 10; /* count through the amount of times entered */
    long sum = 0L; /* The total amount of the numbers entered */
    float average = 00.0f; /* average of the total / count */
    int i = 0; /* count through the array */
    char more = 'y'; /* variable to continue with the loop y||Y */


    /* do loop to go through the user input */
    do
    {
    for(i =0; i <= count; i++)
    {
    printf("\n Enter the number you wish to average:\n"); /* enter the numbers display to the user */
    scanf("%d", &number[i]); /* read input */


    average = (float)(10 * number[i] / count);

    printf(" The value you entered was %d\n", number[i]);
    printf(" The average of these numbers is %.2f\n", average);

    printf("Would you like to try another value? (N/Y)?: \n"); /* ask for more input values */

    scanf(" %c", &more); /* look for value to be yes or no */
    if (more == 'N' || more == 'n')
    { break;}
    }


    }while(more == 'Y' || more == 'y');

    }

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    You're only taking the average of one number, that's hardly surprising output :-) Try this instead, it may be more to your liking
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      int number; /* array of 10 numbers */
      int count = 10; /* count through the amount of times entered */
      long sum = 0L; /* The total amount of the numbers entered */
      float average = 0.0f; /* average of the total / count */
      int i = 0; /* count through the array */
      char more = 'y'; /* variable to continue with the loop y||Y */
      
      do
      {
        for(i = 0; i < count && tolower(more) != 'n'; i++)
        {
          printf("\n Enter a number:\n");
          scanf("%d", &number); /* read input */
          
          sum += number;
    
          printf("Would you like to try another value? (N/Y)?: \n");
          scanf(" %c", &more); /* look for value to be yes or no */
        }
        
        average = (float)sum / i;
    
        printf(" The average of these numbers is %.2f\n", average);
        printf("Would you like to try another sequence? (N/Y)?: \n");
        scanf(" %c", &more); /* look for value to be yes or no */
      }while(tolower(more) == 'y');
      
      return 0;
    }
    *Cela*

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Thank you for your help. and your quick response

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Cela answered you, but...

    You again forgot to add the code tags...
    It's important to add the code tags, your code will be more readable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21