简体   繁体   中英

Output not correct for my C++ program

So, I have the following problem:

  1. From the file tabl.in a number n will be read (n<=50).
  2. After that a square array with n rows and n columns will be read; all the numbers in the array will be composed by a maximum of 2 digits each.
  3. Shown in the file tabl.out, the modulo between the sum of numbers found on the second diagonal of the array and 10, if the sum is palindrome (true=1, false=0), and the arithmetic mean of elements situated below of the main diagonal.

Will be writing functions for:

  • reading the array
  • calculation of the operation sum of secondary diagonal%10
  • checking if the previous result it is palindrome
  • calculation of the arithmetic mean below main diagonal

Example:

tabl.in :

4
5 8 2 12
1 0 3 16
1 2 1 11
5 7 2 19

tabl.out :

2 1 3

where
(12+3+2+5)%10 = 22%10 = 2
22 is palindrome = 1
1+2+2+1+7+5 = 18, 18/6=3

My code so far is:

#include <fstream>

using namespace std;

ifstream fin("tabl.in");
ofstream fout("tabl.out");

void readn(int Arr[][51], int n) {

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            fin >> Arr[i][j];

}

int sumsec(int Arr[][51], int n) {

    int s = 0;
    float r;
    for (int i = 1; i <= n; i++)
        s = s + Arr[i][n - i + 1];
    r = s % 10;
    return r;

}

void pald(int Arr[][51], int n) {

    int s = 0, pal = 0;
    for (int i = 1; i < n; i++)
        s = s + Arr[i][n - i + 1];
    while (s != 0) {
        pal = pal * 10 + s % 10;
        s = s / 10;
    }
    if (pal == s)
        fout << "1 ";
    else
        fout << "0 ";

}

int ambmd(int Arr[][51], int n) {

    int s = 0, k;
    float ame;
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= i - 1; j++) {
            s = s + Arr[i][j];
            k++;
        }
    }
    ame = s / k;
    return ame;

}

int main() {

    int Arr[51][51], n;
    float r, ame;
    fin >> n;
    readn(Arr, n);
    r = sumsec(Arr, n);
    fout << r << " ";
    pald(Arr, n);
    ame = ambmd(Arr, n);
    fout << ame;

}

But I have an issue with the palindrome() function: my output file will have 2 0 3 written to it for the given array from the example, instead of 2 1 3 . What am I doing wrong?

Your pald function would work, if you compute s the same way as you do in sumsec and if s would still contain the sum, after you compute pal .

In your case, while (s != 0) {...} , followed by if (pal == s) {...} could be re-written as if (pal == 0) , which is clearly not the intended solution. Just save your sum before computing pal , then compare with the saved sum.

Also, change your loop condition for computing s to for (int i = 1; i <= n; i++) .

int s = 0, pal = 0, sum = 0;
for (int i = 1; i <= n; i++)
    s = s + Arr[i][n - i + 1];
sum = s;
while (s != 0) {
    pal = pal * 10 + s % 10;
    s = s / 10;
}
if (pal == sum)
    fout << "1 ";
else
    fout << "0 ";

You should also consider the various comments for code improvements, like not re-computing the sum in the pald function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM