-1

I really need guidance on an assignment.

Instructions

  • Can you post your code here? – TechGeek49 Jul 06 '21 at 02:26
  • 1
    Hint: 1) You probably need an int[] array to hold the numbers the user enters, 2) You need a "while()" loop to read the numbers from the user into the array. The loop will probably call "scanf()". 3) After the user enters "0", you'll have a second - "for()" loop, to compute the average. 4) You'll need to keep count of both A) the numbers, and B) how *MANY* numbers were entered. 5) Finally, you'll probably want to "printf()" the average. – FoggyDay Jul 06 '21 at 02:27
  • 7
    I don't think you need an array at all. Initialize sum and count variables to 0. Read a number, if it isn't 0 add it to the sum and increase the count. If it is 0 then divide sum by count. – Retired Ninja Jul 06 '21 at 02:29
  • I edited it to add my code so far. It doesn't work how I want it to because I think it's including the 0 entered by the user –  Jul 06 '21 at 02:36
  • 2
    @Steff, maybe you should check for `0` *before* processing the `0` entered by user? And `sum/counter` is doing integer division. – HAL9000 Jul 06 '21 at 02:39
  • 1
    I edited it again and added counter--, and it works overall, except it doesn't output decimal numbers. How do I fix this? –  Jul 06 '21 at 02:43
  • 1
    Ok I finished the code. Can someone make sure that I did the for loop correctly? I wasn't sure if I should have written i<50 –  Jul 06 '21 at 02:46
  • Also, sorry for all the questions, but how can I print "Invalid input" if the user inputs something that isn't a number? –  Jul 06 '21 at 02:53
  • 1
    That `i<50` basically limits to `50` the maximum amount of numbers provided by the user. It is not really necessary. You could well write instead of the `for` loop a `while (1)`, i.e., an infinite loop. To check if the input is a number, see [this](https://stackoverflow.com/a/45554707/6874310). Also, welcome to SO! – Jardel Lucca Jul 06 '21 at 03:03
  • Thank you! I wanted to use a while loop, but the instructions were to use a for loop, so that's why. :) –  Jul 06 '21 at 03:06
  • 1
    Use a for loop but make the condition the same as you would with a while loop. A for loop and a while loop are the same: `initializer; while(condition) { code; increment; }` and `for(initializer; condition; increment) { code; }` So maybe your for loop should be `for (;numbers!=0;) {` - just make sure to initialize `numbers` to something at the top of the program (something that is not zero...) Also consider that you can't compare a float to a literal number in some cases because not all numbers can be represented in a float (like 0.1 for example) but in this case it is ok because 0 is ok. – Jerry Jeremiah Jul 06 '21 at 03:28
  • In general, [Welford's algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm) gives you a numerically stable mean and variance on-line. – Neil Jul 06 '21 at 06:16

2 Answers2

1

In this case i suggest you should you while or do while loop, you cannot use for loop if do not know the destination

#include <stdio.h>

int main()
{
  int count = 0;
  float sum = 0;
  float numbers;
  printf("Enter numbers to find the average (0 to stop)\n");
  do {
    scanf("%f", &numbers);
    count++;
    sum += numbers;
  } while(numbers != 0);

  printf("Result: %f", sum/(count-1));
  return 0;
}
0

the following proposed cleanly compiles and performs the desired functionality

#include <stdio.h>

int main( void ) 
{
    
    int counter;
    int sum = 0; 

    int numbers;
    
    printf("Enter numbers to find the average (0 to stop)\n");
    
    for ( counter = 0; ; counter++) 
    {
        scanf("%d", &numbers);
        if( ! numbers )
            break;
        
        sum += numbers;
    }
    
    float fcounter = (float)counter;
    float fsum     = (float)sum;
    float average  = fsum / fcounter;
    printf("\nAverage = %.3f", average);

    return 0;
}
user3629249
  • 16,402
  • 1
  • 16
  • 17