tags:

views:

90

answers:

4

Currently my program allows the user to enter 5 integers which are used to create an average number. This is set to five as after the fifth number is entered the loop is broken.

I am trying to implement a method which will let the user continue to add as many numbers as they like to an array from which i can then use to create an average without a limit on the amount of numbers that can be entered.

I have come across a few problems, firstly i cannot create an array which is dyamic as i have no idea how many numbers the user may wish to enter which means i can't give it a definitive size.

Secondly the way my program currently creates the average is by looping through the elements in the array and adding the consecutively to an integer, from which the the average is made. I cannot specify the limit for the loop to continue running if i cannot determine the array.

Hopefully my example explains this better.

#include <stdio.h>
#include <string.h>

void main()
{
    int i = 0;
    int arrayNum[5];
    int temp = 1;
    int anotherTemp = 0;
    int answer = 0;


    printf("Enter as many numbers as you like, when finished enter a negative number\n");

    for(i = 0; i < 5; i++)
    {
        scanf("%d", &temp);

        arrayNum[i] = temp;

        anotherTemp = anotherTemp + arrayNum[i];
    }

    answer = anotherTemp / 5;

    printf("Average of %d,%d,%d,%d,%d = %d",arrayNum[0],arrayNum[1],arrayNum[2],arrayNum[3],arrayNum[4],answer);
}

Although this may not be the best way to implement it, it does work when the amount of numbers are specified beforehand.

What would be the best way to get around this and allow the user to enter as many number as necessary?

Edit: Although i needed to use an array I have decided that it is not necessary as the solution is much simpler without being restricted to it.

+3  A: 

In terms of code simplicity, you might want to check out the realloc() function; you can allocate an initial array of some size, and if the user enters too many numbers call realloc() to get yourself a bigger array and continue from there.

You don't, however, actually need to keep the numbers as you go along at all, at least if you only care about the average:

int input;
int sum = 0;
int count = 0;
int average;

while (1) {
    scanf("%d", &input);
    if (input < 0) {
        break;
    }
    sum += input;
    count++;
}

average = sum / count;
Carl Norum
Seems like the easiest solution to this, i don't want to overcomplicate it. Thanks.
Jamie Keeling
I seem to have an additional value in the counter when calculating the average, i have managed to fix this by simply removing 1 from the count to get the correct amount. Can you suggest a better solution?
Jamie Keeling
There's nothing wrong with subtracting one. Maybe you can change the order of the statements testing for the end of the loop and the statements used to increment your variables.
Carl Norum
A: 

Use a dynamic array data structure, like Vector in Java (java.util.Vector).

You can implement such a dynamic array yourself easily:

  • allocate array of size N
  • as soon as you need more elements than N, allocate a new bigger array (e.g. with size N+10), copy the content of the old array into the new array and set your working reference to the new array and your array size variable N to the new size (e.g. N+10). Free the old array.
Curd
The question is tagged C! There's no such luxury.
Mehrdad Afshari
@ Mehrdad Afshari: That's why I added "you can implemet such a dynamic array yourself easily" and explained it!
Curd
I'll upvote you for the solution using an array however Carl and everybody else seem to agree the easiest and simplest way is just ignoring the array altogether.
Jamie Keeling
@ Jamie Keeling: yes, if you just want to calculate the average you don't need an array at all (calculating the sum and finally dividing by the number of values will do). I assumed, however, that you _wanted_ to store your values into an array (maybe there are other things you wanted to do with the values, that were not mentioned)
Curd
My brief was to use the array however i can't understand the need for it if i am only needing the average from the numbers. Sorry for the confusion.
Jamie Keeling
+2  A: 

Basically you start with a dynamically allocated array with a fixed size, and then allocate a new array that is bigger (say, twice as big as initial size) and copy the stuff from the old array to the new one whenever you run out of space.

For the second part of the problem, keep a counter of the number of items the user entered and use it when averaging.

Something like this.

Firas Assaad
+3  A: 

If you're trying to compute an average, then you don't need to save the numbers. Save yourself the work of worrying about the array. Simply accumulate (add) each number to a single total, count each number, then divide when you're done. Two variables are all that you need.

With this method, you aren't in any risk of overflowing your array, so you can use a while loop... while (temp != -1)

Pestilence
I had the same idea already, see my edit =]
Jamie Keeling