Need Help With An Algorithm Problem/Question From A Book

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

ChunkyTeXas

Thread Starter
Joined
Dec 14, 2002
Messages
84
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!!
 
Joined
Mar 13, 2003
Messages
358
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);
 
Joined
Nov 24, 2002
Messages
2,438
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:

Code:
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. ;)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top