vendredi 5 janvier 2018

how to add random numbers generated from a for loop

I have to write a program where it generates random numbers (from 1 to 6) and adds (sums) the randomly generated sumbers. So far I've managed to generate 10 random numbers (two pairs: ten numbers for x and another ten for y) My initial goal is to compare the two, which I can with an 'if' statement but so far I cannot figure out how to add the randomly generated numbers together. I tried diferent methods but nothing has worked so far. I am new to c++ so please excuse me for the question ,if it is simple, in advance. The code looks like this:

#include <iostream>
#include <cstdlib>
#include <string>
 using namespace std;

 int main(){
  int sum1=0, sum2=0, x, y;

  for(x = 1; x <= 10; x++) {

    cout << 1 + (rand() % 6) << endl;

     sum1=sum1+x;
  }

   cout<<"----------------"<<endl;

for ( y=1; y<=10; y++){


    cout<<1 + (rand() % 6)<<endl;

    sum2=sum2+y;

}

cout<<"-----------------"<<endl;

 cout<<sum1;


cout<<sum2;

return 0;  
}




Aucun commentaire:

Enregistrer un commentaire