+ 3

is there a way I can store all the values of x in this short code without having to display them using cout? int main() { for (int x=0; x<5; x++){ //how do i store the all the values of x without having to display them?// }

24th Dec 2016, 4:22 PM
Cody Arthur
Cody Arthur - avatar
5 odpowiedzi
+ 7
You may use an array to do so. int main () { int array[5]; for (int x = 0; x < 5; x++) { array[x] = x; } return 0; } ... Although I fail to see the point of doing so. Now, the set of integers stored in array would be as follows: {0, 1, 2, 3, 4}
24th Dec 2016, 4:44 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
Here goes. I am using switch statements though, to make it shorter. int x = 0; int choice1 = 0; int weaponpoint; int weap_sum; int array[5]; srand (time (0)); for (x = 0; x < 5; x++) { choice1 = 1+ rand() * 10; switch (choice 1) { case 1: weaponpoint = 10; array[x] = weaponpoint; break; case 2: weaponpoint = 20; array[x] = weaponpoint; break; // similar for the rest of the cases case 10: weaponpoint = 100; array[x] = weaponpoint; break; } } for (int j = 0; j < 5; j++) { weap_sum += array[j]; } cout << "weap_sum = " << weap_sum << endl; cout << "weap_sum = "; for (int k = 0; k < 4; k++) { cout << array[k] << " + "; } cout << array[4];
25th Dec 2016, 12:47 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
thanks guys for your answers, but to be more specific, this is the code. I want to store each value of weaponpoint . And then after the looping, I want to display the addition of the weapon point values in the form (eg 50+20+60+10+30). how will I be able to store and then obtain each weaponpoint value? srand(time(0)); for(int x= 0;x<5;x++){ choice1 =1+(rand()%10) ; if (choice1 ==1){ weaponpoint =10; } else if (choice1 ==2){ weaponpoint=20; } else if (choice1 ==3){ weaponpoint=30; } else if (choice1 ==4){ weaponpoint=40; } else if (choice1 ==5){ weaponpoint =50; } else if (choice1 ==6){ weaponpoint=60; } else if (choice1 ==7){ weaponpoint=70; } else if (choice1 ==8){ weaponpoint=80; } else if (choice1 ==9){ weaponpoint=90; } else if (choice1 ==10){ weaponpoint =100; }
24th Dec 2016, 10:58 PM
Cody Arthur
Cody Arthur - avatar
0
store the values in an array
24th Dec 2016, 4:43 PM
emuka0
emuka0 - avatar
0
cout is used to DISPLAY values not to store them arrays can simply store them otherwise if you explained why or what you want to do I would be able to help you
24th Dec 2016, 5:22 PM
Youssef Mohammed Hassan
Youssef Mohammed Hassan - avatar