Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# c++ loop help

### #1 rzneader Posted 28 November 2005 - 12:12 PM

rzneader

Member

• Member
• 11 posts
Hi,

I need some help writing a c++ program.

The program needs to take 10 numbers entered into an array, and add them all together using a loop for the addition of the ten numbers.

I am having difficulty determining the correct loop to add these ten numbers togehter. I keep getting astronomical figures or my answers.

Can anybody help a beginner brother out?
• 0

### #2 ricci Posted 28 November 2005 - 12:28 PM

ricci

Member

• Member
• 64 posts
Hi rzneader,

It might be helpful if you post the code you have so far.

In the mean time, here are some general things to check:

1. Are there valid values in your array?
2. C++ arrays are zero based, meaning that an array of size 10 would be indexed starting at 0 and ending at 9. Check to make sure your loop goes from 0 - 9 instead of 1 - 10 or 0 - 10 or something.
3. Are you initializing the variable you are using to count? You should start by setting it to zero. If you don't, there could be some garbage number in there as you start adding things up.

Good luck,
Ricci
• 0

### #3 rzneader Posted 28 November 2005 - 01:53 PM

rzneader

Member

• Topic Starter
• Member
• 11 posts
Here is what i have so far:
#include<cstdio>
#include<iostream>

using namespace std;
int getnum (int min, int max);
int main()
{
int numbers[10];
int i;
int k;
int sum;
cout<<"This program will take 10 entered numbers (between -1000 and 1000) and calculate";
cout<<"the sum, display the smallest and largest numbers entered, and calcualte the average number entered."<<endl;

cout<<"\nPlease enter 10 numbers between -1000 and 1000"<<endl;
for (i=0; i<10; i++)
numbers[i] = getnum(-1000,1000); //send first number entered to validation function
cout<<"\nHere are the numbers you entered:"<<endl<<endl; //tell user entered values will be displayed

for(i=0; i<10; i++)
cout<<numbers[i]<<" ";
SOMEWHERE IN THIS AREA LIES MY PROBLEM
for(i=0; i<10; i++)
sum= numbers[i] + sum;
cout<<"\n"<<sum;
system("pause");
return(0);
}
int getnum( int min, int max) //function to validate numbers are between min and max

{ //declare variables
int num; //number to be entered
do //perform test for every number entered
{
cout<<" "; //space
cin>>num; //let user enter a number
if (num<min || num>max) //if number is out of range
cout<<"\nInvalid entry, number must be between "<<min<<" and "<<max<<endl; //tell user number is out of range
}
while (num<min || num>max); //if number is in range
return(num); //return number
} //end function getnum

Edited by rzneader, 28 November 2005 - 01:55 PM.

• 0

### #4 ricci Posted 28 November 2005 - 02:59 PM

ricci

Member

• Member
• 64 posts
It looks like option 3 of my previous post is probably your problem. You declare the sum variable and then start using it in your loop. The problem is that in C++, the memory that the computer uses to represent the sum variable will have any random garbage in it before you start using it. These random values can range from -2 billion to 2 billion (roughly) in the case of an int variable. The solution is to initialize the variable at some point. You can do this right where you declare it like so:
int sum(0);
or
int sum = 0;
Or you could initialize it just before you begin using it in your loop by adding the following line:
sum = 0;
Give one of these options a try and see what happens.

-Ricci
• 0

### #5 rzneader Posted 28 November 2005 - 04:25 PM

rzneader

Member

• Topic Starter
• Member
• 11 posts
man I hate when I make stupid mistakes like that! Thanks for the help and for not poking fun at the programming noobie.

One other question if you don't mind me asking. If I want the program to display the largest number entered, is it necessary to first arrange the numbers in ascending order? Or is there a simpler way to do this?
Thanks again for all your great help!
• 0

### #6 ricci Posted 28 November 2005 - 04:40 PM

ricci

Member

• Member
• 64 posts
There is a simpler way to do it. You can even do it in the same loop as you find the sum. If you were going to do it by hand, would you sort the array elements first? I wouldn't. I'd just keep track of the largest one as I scan down the list. Do it the same way in your code.

-Ricci
• 0

### #7 rzneader Posted 28 November 2005 - 06:15 PM

rzneader

Member

• Topic Starter
• Member
• 11 posts
Ok I got it! You kick [bleep], dude. Can't thank you enough.
• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users