generating sums of rand numbers

Hey all, i'm new to coding and trying to understand the basics. Right now I'm trying to create a code that generates random numbers and spits out a sum average and lowest and highest number. I am stuck on the sum however and once I get that I think the average will fall into place. Here's what I have.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <math.h>
using namespace std;

int main()
{
int i, number;

cout<<"input the amount of number's you want summed and averaged\n";
cin>>number;

cout<<"The numbers we will be using are\n";

int x;
long int sum=0;
srand(time(NULL));
for (i=0; i<number; i++)
{
x=(rand()%10)+1;//generate random numbers
cout<<x<<endl; //
x+=i;
sum=x+sum;
}
cout<<endl;

cout<< "the sum of these integers is "<<sum<<endl;

double average=(sum/number);// to find the average in next line

cout<<"the average of all of your integers is "<<average<<endl;

return 0;


edit: i've noticed that it's almost always off by 1 or 3 for whatever reason
Last edited on
What is the line x += i; supposed to accomplish?
that calculates the sum of the numbers generated from the loop. is there another way to do so?
if x += i; calculates the sum of the numbers generated from the loop, what does sum = x + sum do?
that was just a temporary variable I made to try and fix the bug. But it did not work
that was just a temporary variable I made to try and fix the bug. But it did not work

If it's just a temporary variable you made, why does it show up again later in your code?

If x is the random number generated in each iteration of the loop (as x=(rand()%10)+1 suggests) and i is the number of times the loop has executed, then for any given iteration of the loop x+i is a random number plus the number of times the loop has executed. How does that equate to "the sum of the numbers generated from the loop?"
you're right it odes not and I also don't need the extra variable which I have since taken out. My problem is I'm not sure how to equate something to get it to add the random integers together x+=i gives me the right amount of integers but the wrong sum so it seems thats the problem. I will try deleting that and seeing if i can come up with a sum equation but so far ive had no luck
edit


int i, number;

cout<<"input the amount of number's you want summed and averaged\n";
cin>>number;

cout<<"The numbers we will be using are\n";

int x=0;
int sum=0;

for (i=1; i<=number; i++)
{
x=(rand()%10)+1;//generate random numbers
cout<<x<<endl;
sum=sum+x;

}
cout<<endl;

cout<< "the sum of these integers is "<<sum<<endl;

double average=(sum)/number;// to find the average in next line

cout<<"the average of all of your integers is "<<average<<endl;

return 0;


ive since done this which gives me the right sum but wrong average
Last edited on
closed account (48T7M4Gy)
Have you thought of cout'ing the value of number to check whether it's what you expect?
Looks like you've got the right idea.

The result of (sum)/number is an integer, born of integer division. The fact that you're assigning to a variable of type double suggests that you are expecting it not to be. In order for that to be true, you must cast one of the variables:

double average = static_cast<double>(sum) / number;
awesome thanks a lot! and your explanation made me understand what I was doing with the double int that made it not work. As far as the lowest number highest number the hardest part has been the gameplan to get it done. I was thinking maybe do an if statement. but i'm not sure how I could do that unless I did one for every number. Or maybe I could loop that?
Topic archived. No new replies allowed.