# Need Help With An Algorithm Problem/Question From A Book



## ChunkyTeXas (Dec 14, 2002)

Im using the book "Simple Program Design" BY Lesley Anne Robertson. I need help finding the/a answer to the following question:

"Design an algorithm that will read a series of intergers at the terminal. The first interger is special, as it indicates how many more intergerrs will follow. Your alogorithm is to calculate and print the sum and average of the intergers, excluding the first interger, and display these values to the screen"

If anyone would please help me with this, my aim name is MikeMac619

Thanks!!


----------



## punjabian263 (Mar 14, 2003)

1-first take input for any value e.g user inter the (variable)value=5
2-run a loop to that value e.g in c++ for(int i=0;i<value;i++)
take input in this loop
3-just add those input value by a counter variable sum
4-display average by cout<<(sum/i);


----------



## AlbertB (Nov 24, 2002)

Hi MikeMac,

Seems if I have it right that the setup is like this.

We are to input an indeterminate number of integers. If the first integer is say 5, then 5 more are to follow, and we want to calculate the sum and average of these following 5 disregarding the first.

Assuming that is correct then it would seem that we simply take in the first integer and use it as the limiting value of the loop variable in a for/next loop construct.

Then within the loop we input another integer each time adding it to a variable which carries the total of all of these inputs.

This will occur the correct number of times according to 'limit' so that on entry we will have the total of 'limit' number of integers and simply need to divide that total by 'limit' itself, the number of integers added, to get the average. Something like:


```
Input first integer to 'limit';
for ( int loop = 0; loop < [b]limit[/b]; loop++ ) {
   input integer to value;
   add value to total;
}
display total;
display total/limit;
```
I have deliberately not put this in the form you will probably want it, it is important that you do that stage yourself. And please make sure that, if this is what you need, you fully understand each stage. We are not here just to do anyone's work for them, that is of no help at all. We are here to help them help themselves.

Good luck and post again if you need more help.


----------



## AlbertB (Nov 24, 2002)

We crossed in the post punjabian! Same method too I think.


----------



## ChunkyTeXas (Dec 14, 2002)

Thank you for your help, all of you.


----------

