0

I've been looking for a solution quite a while now, but did not manage to find anything.

I need a program which takes the users input (a random amount of integers) until EOF, sums them up and gives me back the average.

I tried it using an array but I am not sure whats my mistake here. I managed to get it working with a fixed size array. But I need a flexible one.. Is this even possible?

Here is what I got so far:

#include <stdio.h>

int main()
{
  int count = 3;
  int numbers[count];
  long sum;
  float average;
  int i;
  for (i = 0; i < count; i++) {
    while (scanf("%d", &numbers[i]) != EOF) {
        sum += numbers[i];
    }
  }
  average = (float)sum/count;
  printf("Average of your numbers is: %.2f\n",average);
  return 0;
}
smnk
  • 471
  • 1
  • 6
  • 25
  • How am I supposed to add this dynamic memory allocation? – smnk Oct 21 '14 at 17:26
  • should be`long sum=0;` – BLUEPIXY Oct 21 '14 at 17:26
  • Doesn't it initialize with = 0 itself? – smnk Oct 21 '14 at 17:27
  • @Niklas [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) –  Oct 21 '14 at 17:28
  • you not need while (or for). – BLUEPIXY Oct 21 '14 at 17:29
  • No, without initialization in C, I believe most compilers will give you garbage values like 3437460 =D. – user3437460 Oct 21 '14 at 17:29
  • Not initializing it does not change the outcome in anyway. – smnk Oct 21 '14 at 17:30
  • @Niklas It may not for you, but it's considered undefined behavior. i.e., §J.2 `The behavior is undefined in the following circumstances: [..] — The value of an object with automatic storage duration is used while it indeterminate (6.2.4, 6.7.9, 6.8).` So it's good practice to always initialize your variables. –  Oct 21 '14 at 17:33
  • @Niklas, is there any reason you want to store all inputs as arrays? Why do you want to keep all the input? – user3437460 Oct 21 '14 at 17:34
  • There was no reason, I just thought that would make it easier for me. But the Solution is as simple as it could be. Thanks for the help! – smnk Oct 21 '14 at 17:35
  • Then I will give you a solution without storing the inputs in array. – user3437460 Oct 21 '14 at 17:36
  • @NiklasHein the typical strategy is to do a dynamically allocated array with a counter of the number of members in it and a counter for the maximum size. set it to a reasonable amount (probably a global constant for your silly code). When you add, check if count+1>size. If false, store value and increment counter. If true, allocate an array of twice the size, copy the old array to the beginning of the new array, free the old array, store the new value, increment the counter, set maxsize to 2*size. – IdeaHat Oct 21 '14 at 17:39
  • @MadScienceDreams That may be irrelevant since this is an X and Y problem. The OP indicated he doesn't need to store the input at all. –  Oct 21 '14 at 17:39

4 Answers4

1

If you're just trying to find the average then you don't need to actually store these numbers.

int count = 0;
int sum = 0;
int num = 0;
double avg = 0.0;

for(; scanf("%d", &num) != EOF; sum += num, count++)
    ;

avg = sum / count;
John
  • 2,395
  • 15
  • 21
0

After checking with OP, he does not mind if the inputs are not stored in array. In that case, you may want to consider this:

int num=0;
int sum=0
int count=0;

do
{
    printf("Enter number:");
    scanf("%d", num);

    sum += num;
    count++;

    printf("Proceed(y/n)?");
    scanf("%c", proceed);

}while(proceed == 'y');

This code may be a little troublesome that you need to enter 'y' to continue inputing, but this may solve your current problem of inputting n inputs.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • 1
    I had it this way before. But since this is a Homework for my class, my Teacher told us not to use any other input methods but the numbers and EOF.. – smnk Oct 21 '14 at 17:42
  • In that case, we just need to change the expression in the `while`. You still don't have to store your values in an array. Let me look in it. – user3437460 Oct 21 '14 at 17:44
0

You will need to realloc the size of the array if you have filled the existing array. This should work:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int input;
    int size = 0;
    int* arr = NULL;
    int* temp = NULL;

    long sum = 0;
    float average = 0;

    do {
        scanf("%d", &input);
        size++;

        temp = (int*) realloc(arr, sizeof(int)*size);

        if(temp != NULL) {
            arr = temp;
            arr[size-1] = input; 
        }

        sum += input;
    } while(input != 0);

    average = sum / size;
    printf("sum: %lu\n", sum);
    printf("average: %f\n", average);
    return 0;
}
user3270760
  • 1,444
  • 5
  • 23
  • 45
-3

It does not have to be an array if all you're doing is adding up numbers

int number = 0;
int count  = 0;
while(scanf("%d", &number)!=EOF)
{
    sum += number;
    count++;
}

Also this will never work because count is supposed to be a constant

int count = 3;
int numbers[count];
Sergey
  • 494
  • 2
  • 6
  • I'll quickly check this, but I will need to count the Numbers as well to be able to divide the sum by the count of numbers entered. – smnk Oct 21 '14 at 17:30
  • no problem, int count = 0; then inside do { count++; } – Sergey Oct 21 '14 at 17:32
  • 1
    The unusual do-while code structure will lead to bugs when you try to count your numbers – anatolyg Oct 21 '14 at 17:32
  • 1
    Everything is just working fine now. What do You mean by "bugs"? – smnk Oct 21 '14 at 17:36
  • It would be useful to show how to do this with an array though. He doesn't want a fixed size array, but he wants a more flexible option. – user3270760 Oct 21 '14 at 17:51
  • If there is e.g. 1 number, the body of the do-while loop will be executed twice (the first iteration will "cleverly" have no effect on the sum because `number` is initialized to 0). So `count` has to be initialized to -1 in order to count correctly. You can make bookkeeping more understandable if you replace `do-while` by `while`. (my opinion is, only use `do-while` when doing performance optimizations, but that's just an opinion) – anatolyg Oct 22 '14 at 07:37