These are the errors and warnings I get in VS 2019:
Forum_CPP.cpp(7,18): error C2371: 'nrow': redefinition; different basic types
Forum_CPP.cpp(4): message : see declaration of 'nrow'
Forum_CPP.cpp(8,18): error C2371: 'ncol': redefinition; different basic types
Forum_CPP.cpp(5): message : see declaration of 'ncol'
Forum_CPP.cpp(137,17): warning C4456: declaration of 'x' hides previous local declaration
Forum_CPP.cpp(128,7): message : see declaration of 'x'
Forum_CPP.cpp(141,5): warning C4456: declaration of 'i' hides previous local declaration
Forum_CPP.cpp(133,12): message : see declaration of 'i'
I think you really should get a good book on C++ and study it.
Trying to modify code you copied and don't understand will get you nowhere.
If you are not really interested maybe better to give up and focus on sth. else.
#include <iostream>
#include <fstream>
usingnamespace std;
constexpr size_t nrow {4};
constexpr size_t ncol {5};
void largest_value_in_cols(constint a[nrow][ncol], int l[ncol]) {
for (size_t c = 0; c < ncol; ++c) {
l[c] = a[0][c];
for (size_t r = 1; r < nrow; ++r)
if (a[r][c] > l[c])
l[c] = a[r][c];
}
}
size_t row_number_largest(constint a[nrow][ncol]) {
int largest_row_value {a[0][0]};
size_t largest_row_number {};
for (size_t r = 0; r < nrow; ++r)
for (size_t c = 0; c < ncol; ++c)
if (a[r][c] > largest_row_value) {
largest_row_value = a[r][c];
largest_row_number = r;
}
return largest_row_number;
}
int largestValue(constint a[nrow][ncol], size_t& rvalue, size_t& cvalue) {
int large {a[0][0]};
for (size_t r = 0; r < nrow; ++r)
for (size_t c = 0; c < ncol; ++c)
if (a[r][c] > large) {
large = a[r][c];
rvalue = r;
cvalue = c;
}
return large;
}
int arrayTotal(constint a[nrow][ncol]) {
int total {};
for (size_t r = 0; r < nrow; ++r)
for (size_t c = 0; c < ncol; ++c)
total += a[r][c];
return total;
}
size_t row_number_even(constint a[nrow][ncol], bool x[nrow]) {
size_t even_row_value {};
for (size_t r = 0; r < nrow; ++r)
for (size_t c = 0; c < ncol; ++c)
if (a[r][c] % 2 == 0) {
++even_row_value;
x[r] = true;
break;
}
return even_row_value;
}
int main()
{
int two_darray[nrow][ncol] {};
// Load the Array
ifstream myfilein("L:\\week6numbers2.txt");
if (!myfilein) {
cout << "Can not open file";
return 0;
}
for (size_t r = 0; r < nrow; ++r)
for (int c = 0; c < ncol; ++c)
myfilein >> two_darray[r][c];
for (size_t r = 0; r < nrow; ++r) {
for (size_t c = 0; c < ncol; ++c)
cout << two_darray[r][c] << ' ';
cout << '\n';
}
cout << "The total is " << arrayTotal(two_darray) << '\n';
size_t largestcol {};
size_t largestrow {};
int l[ncol] {};
cout << "The largest is" << largestValue(two_darray, largestrow, largestcol) << '\n';
cout << "The number is located in row " << largestrow << "and col " << largestcol << '\n';
cout << "The largest value is in row " << row_number_largest(two_darray) << '\n';
cout << "The largest column values are:\n";
largest_value_in_cols(two_darray, l);
for (size_t i = 0; i < ncol; ++i)
cout << l[i] << '\n';
bool x[nrow] {};
constauto noeven {row_number_even(two_darray, x)};
cout << "There are" << noeven << "rows with an even number\n";
for (size_t i = 0; i < nrow; ++i)
if (x[i])
cout << x[i] << ' ';
cout << '\n';
}