how can I use a loop to sum random numbers together? it needs to be able to sum any amount of numbers determined by the user. I can generate the random numbers but adding them is a different story. I tried to make an inner loop (if that makes sense) but all that does is display the sum of each number. Effectively displaying the random number again and not adding anything. Then I tried to add the calculation in the loop and got a similar thing. I just need to add the numbers up at the end.

int main()
{
  int randValue;
  int a,b,i,n,sum, entry, y, count;
  char again = 'y';
  double average;

  while (again == 'y')
  {
  cout << " enter the number of random numbers (1-100): "<<endl;
  cin >> n;	 
  cout << endl;
  
	  
 if (n <= 100)
 {srand(time(NULL));  // seed
  cout << "Please enter the range you want (lowest to highest) : " <<endl;
  
  {
count = 5
  for (i = 1; i <= n; i++)
  {
    randValue = a + rand() % (b+1-a);
	cout << randValue << " ";
                if(i%count==0) cout<<endl;
                     
                     
                           sum=0;
                              sum = sum+randValue;
                              cout << "sum = " <<sum <<endl;



	cout << endl;

	
}
	 
  return 0;}

}} }

You add random numbers together the way you add any other numbers together. Initialize a sum variable to 0 and go through a for loop, adding each number.

if (n <= 100)
 {srand(time(NULL));  // seed
  cout << "Please enter the range you want (lowest to highest) : " <<endl;
  
  {
count = 5
  for (i = 1; i <= n; i++)
  {
    randValue = a + rand() % (b+1-a);
	cout << randValue << " ";
                if(i%count==0) cout<<endl;
                     
                     
                           sum=0;
                              sum = sum+randValue;
                              cout << "sum = " <<sum <<endl;



	cout << endl;

	
}

You never pause for input for the question asked in line 3. Line 9 is bad since a and b aren't initialized to anything. Line 14 - sum needs to be initialized to 0 BEFORE the loop, not INSIDE the loop.

That's what I initially tried but again it just displays the random number with out adding them. The other errors i've fixed i just don't want people to go copy my code.

That's what I initially tried but again it just displays the random number with out adding them. The other errors i've fixed i just don't want people to go copy my code.

We can't help if we can't see the current code. There's obviously still an error in it.

if your current posting is correct, sum=0 is in the wrong place. It should be at the top of the function outside the loop. Not reset each pass through the loop!

What Vernon Dozler said at the bottom of the post!

as I said, it just displays the random number not adding it.

int main()
{
  double randValue;
  int a,b,i, n, sum, y, count;
  char again = 'y';


  while (again == 'y')
  {
  cout << "enter the numbers : "<<endl;
  cin >> n;	 
  cout << endl;
  
	  
 
 {srand(time(NULL));  // "seed" value
  cout << "Please enter the range  : " <<endl;
  cin >> a;
  cin >> b;
  count = 5;
 

  if (a<b)
  {

	  
  for (i = 1; i <= n; i++)
  sum=0;
  {
    randValue = a + rand() % (b+1-a);
    
	sum=sum+randValue;
	
	cout << randValue << " ";
	if(i%count==0) cout<<endl;

	}
  
  cout << "sum is = " <<sum <<endl;
  
  return 0;}


	  
		  

  }

ok a and b are inputs by the user for range. it adds but it doesn't change the number anymore. if it's not one thing it's another.

sum=0;
  for (i = 1; i <= n; i++)
  
  {
    randValue = a + rand() % (b+1-a);
	
	for (i = 1; i<=n; i++)
	{sum=sum+randValue;
	cout << randValue << " ";
	if(i%count==0) cout<<endl;

	}}
  
  cout << "sum is = " <<sum <<endl;
  
  return 0;}

Your code is completely unreadable the way it is formatted. Here it is formatted:

int main()
{
    double randValue;
    int a, b, i, n, sum, y, count;
    char again = 'y';

    while (again == 'y')
    {
        cout << "enter the numbers : " << endl;
        cin >> n;
        cout << endl;

        {
            srand(time(NULL)); // "seed" value
            cout << "Please enter the range  : " << endl;
            cin >> a;
            cin >> b;
            count = 5;


            if (a < b)
            {
                for (i = 1; i <= n; i++)
                    sum = 0;
                {
                    randValue = a + rand() % (b + 1 - a);
                    sum = sum + randValue;
                    cout << randValue << " ";
                    if (i % count == 0)
                        cout << endl;
                }

                cout << "sum is = " << sum << endl;
                return 0;
            }
        }

Unless I copied and pasted wrong or formatted it wrong, you're missing a bracket. Your for-loop is lines 23 and 24. Lines 25 and on aren't in it. You've posted since with a change or two, but the formatting is still way off, so it's impossible to read. Formatting will make things much easier to debug.

As Vernon Dozler said...

Note your count is never decremented!
also sum= 0 is in the wrong place!

if (a < b)
   {
         sum = 0;
          for (i = 1; i <= n; i++) 
          {
                    randValue = a + rand() % (b + 1 - a);

you keep saying the sum=0 is off, yet no matter where I put it it still does the same thing. adds the same random number withouth changing to a new one. formatting is an issue I agree. I worked out last night and it's fine now. but it's completely different now and I had to take your advice to reformat. So thanks. but as for this one...there's still an issue with the loop but I don't care to figure it out since I have a working one now. and it's not the location of the sum=0 it's probably the whole loop all together. thanks again look forward to your advice in the future.

As Vernon Dozler said...

Vernon Dozler? Haha :icon_smile:

formatting is an issue I agree. I worked out last night and it's fine now.

Two thing could have happened:
1. you 'reformatted' the code and you're still missing a bracket
2. By 'reformatted' you mean 'rewrote' the code. In that case post your newest code so we can see what's wrong.

Vernon Dozler? Haha :icon_smile:

Two thing could have happened:
1. you 'reformatted' the code and you're still missing a bracket
2. By 'reformatted' you mean 'rewrote' the code. In that case post your newest code so we can see what's wrong.

yes by reformatting I mean rewrote. turns out that's the best thing to do when you have a pile of crap...flush it. I'll post code later.

Comments
Flushing is sometimes indeed the best option :)
This article has been dead for over six months. Start a new discussion instead.