Task :

Given a two-dimensional array of 2 rows and 4 columns. Create a program that organizes a one-dimensional array, each element of which is the number of negative elements of the original array in a row. Calculate the sum of the elements of the resulting array.

//Sedyolkin Yuriu Alekseevich 4103 //lab5 #include "stdafx.h" #include <cstdlib> #include <math.h> #include <iostream> #include <stdio.h> #include <locale.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { setlocale(0, ""); system("cls"); int i, j, k; double s = 0; int a[2][4]; int b[2]; for (i = 0;i < 2;i++) { for (j = 0;j < 4;j++) { cout << "Введи a[" << i << "," << j << "]="; cin >> a[i][j]; cout << endl; } } for (i = 0;i < 2;i++) { for (j = 0;j < 4;j++) { cout << a[i][j] << " "; } cout << endl; } for (i = 0;i < 2;i++) { k = 0; for (j = 0;j < 4;j++) if (a[i][j] < 0) k++; b[j] = k; cout << b[j] << endl; } for (j = 0;j < 2;j++) s = b[j] + s; if (s == 0) cout << "Нет отрицательных значений в массиве а""\n"; else cout << "Сумма элементов преобразованного массива: " << s << endl; system("pause"); return 0; } 

The sum of the elements of the second array at the end of the program is not calculated correctly.

Closed due to the fact that off-topic by the participants Kromster , user194374, Nicolas Chabanovsky 23 Dec '16 at 6:55 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, Community Spirit, Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    You have a typo. In this loop, the variable i should be used as the index of the array b , not j

     for (i = 0;i < 2;i++) { k = 0; for (j = 0;j < 4;j++) if (a[i][j] < 0) k++; b[j] = k; ^^^^ cout << b[j] << endl; ^^^^ } 

    And there is no point in declaring the variable s as having type double.

     double s = 0; 

    Declare it as int .

    And you can remove the following headings

     #include <math.h> #include <stdio.h>