Hi everyone,
I am trying to write a pogram in which first, you are given the description of a chess board (f rows and c columns). After, follows f lines with c numbers each line, meaning every number (from 0 to 9) the amount of coins in every square.
eg: imput << 2 1 (rows and columns)
5 coins on the first square of the first row.
4 coins on the second square of the second row.
output >> 9
here's how i do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
using namespace std;
int main () {
long long int f, c, n;
long long int a = 0;
cin >> f >> c;
if (f <= 0 or c <= 0) cout << "0";
else {
for (int i = 0; i < f; ++i) {
cin >> n;
for (int o = 0; n != 0; ++o) {
a = a + n%10;
n = n/10;
}
}
cout << a;
}
cout << endl;
}
|
brief explanation: once the amount of rows and columns is given, the program reads the next numbers as integers and uses % to obtain every number, which is added up to the counter 'a'.
Now for big numbers like bigger than 20 figures the program doesent seem to work.
If someone could help me I would apreciate it a lot, i have been fighting with this code for 3 days (not much but for a beginner like me looks like a lot :D).
Have a nice day!