I need even numbers in each row using array as a output but its showing me the error ?

Please edit you post and use code tags to format your code.
[code][/code]

What is the error?
Last edited on
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
using namespace std;

constexpr size_t nrow {4};
constexpr size_t ncol {5};

size_t row_number_even(const int 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] {};

	ifstream myfilein("L:\\week6numbers2.txt");

	if (!myfilein) {
		cout << "Can not open file";
		return 0;
	}

	for (size_t r = 0; r < nrow; ++r)
		for (size_t 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';
	}

	bool x[nrow] {};
	const auto 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';
}

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.

https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
You don't provide any test data, so this is untested but does compile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <fstream>
using namespace std;

constexpr size_t nrow {4};
constexpr size_t ncol {5};

void largest_value_in_cols(const int 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(const int 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(const int 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(const int 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(const int 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] {};
	const auto 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';
}

Topic archived. No new replies allowed.