+ 2
which formula should I use to count this?
"Mr. John is a baker. He wants to count how many breads he have made today. There is a number of breads written in every pan. Because he is already tired, he needs your help." input: A series of number named Bx which shows number in every x-pan output: Y which is the total number of breads example: input: 5 6 7 8 output: 26 -- how do I count this?
2 Antworten
+ 3
Not sure if you use cin or have an array ready with the inputs, but here are examples of what you can do:
int count = 0;
int B = 42;
//main loop, enter a negative number to exit it
while (B > 0) {
cin >> B;
if (B > 0) {
count += B;
}
}
cout << count << endl;
int count = 0;
int B[4] = {5, 6, 7, 8};
for (int i = 0; i < 4; i++) {
count += B[i];
}
cout << count << endl;
+ 2
wow I didn't think of using an array before!
Thank you very much, it works perfectly fine! :)