im having hard time with this assignment? can someone let me know plz

Use Weekas1.txt file and load an 8 by 8 integer array. Create the following functions:

This function should return the row number with greatest number of 1’s in the array.
This function should return the col number with greatest number of 1’s in the array.
This function should return the total number of 1’s in the array.
This function should print the array in the following format.
1 1 1 0 1 0 1 0

0 0 0 1 0 1 1 0

1 1 0 0 0 0 1 0

1 1 1 0 1 0 1 0

0 0 0 1 0 1 1 0

1 1 0 0 0 0 1 0

1 1 1 0 1 0 1 0

0 0 0 1 0 1 1 0

This function should print out the which rows have a even number of 1’s and odd numbers 1’s.
Example:

Row 1 even

Row 2 odd


this is the txt file. That's the numbers in this file. i am copying and pasting the numbers from file.
0
1 64
0
1
1
1
0
1
0
0
1
0
1
0
1
0
0
0
0
0
1
1
1
1
0
0
1
1
0
1
0
0
1
1
0
1
1
1
0
1
0
0
1
1
0
0
0
1
1
1
0
1
1
0
0
0
0
1
0
1
1
1
0
0
and your C++ question is? What part are you having difficulty with? What have you done so far? Post your current code so that we can provide guidance on it.

Have you designed the program? Have you determined how to do this using pen/paper? Have you produced an algorithm for the various parts that could be given to someone else that knows nothing about the problem(s) so that they can complete the tasks?
Hello Abi2021,

While working on the program I noticed that your input file starts with:

0
1 64
0
1



What is the (64) for?? Does it have a use and should it be at the start of the file?

Andy
The input file also doesn't match the sample output. It contains 65 numbers in 64 lines. What is the format of the input?
Hello Abi2021,

I have come up with this so far:


Row 1:    0  1 64  0  1  1  1  0

Row 2:    1  0  0  1  0  1  0  1

Row 3:    0  0  0  0  0  1  1  1

Row 4:    1  0  0  1  1  0  1  0

Row 5:    0  1  1  0  1  1  1  0

Row 6:    1  0  0  1  1  0  0  0

Row 7:    1  1  1  0  1  1  0  0

Row 8:    0  0  1  0  1  1  1  0


 The row with the greatest number of 1s is: 1


 Press Enter to continue:



And after taking out the (64) I get this:


Row 1:   0 1 0 1 1 1 0 1

Row 2:   0 0 1 0 1 0 1 0

Row 3:   0 0 0 0 1 1 1 1

Row 4:   0 0 1 1 0 1 0 0

Row 5:   1 1 0 1 1 1 0 1

Row 6:   0 0 1 1 0 0 0 1

Row 7:   1 1 0 1 1 0 0 0

Row 8:   0 1 0 1 1 1 0 0


 The row with the greatest number of 1s is: 5


 Press Enter to continue:



Andy
Once you get the data confirmed, I suggest you write two utility functions:
1
2
int countOnesInRow(unsigned rowNum); // return the number of 1's in the row
int countOnesInColumn(unsigned colNum);  // return the number of 1's in the column. 


Once you have these, the rest are pretty easy. This is one of the hardest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// This function should return the row number with greatest number of 1’s in the array.
int greatestRow()
{
    int result = 0;
    int most=-1;                        // number of 1`s in the result row
    for (int r = 0; r<8; ++r) {
        int count = countOnesInRow(r);
        if (count > most) {
            most = count;
            result = r;
        }
    }
    return result;
}

Notice that this returns 0 for the first row, 1 for the second etc. It's always a good idea for a program's computation to use computer-friendly offsets. Convert to human-friendly offsets (1,2, etc) when you print the output.

Here is my main program and the results with the input given:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int
main()
{
    for (int i=0; i<8; ++i) {
        for (int j=0; j<8; ++j) {
            cin >> arr[i][j];
            if (i == 0 && j == 1) {
                int v;
                cin >> v;       // skip the '64'
            }
        }
    }

    printArray();
    cout << "The most 1's are in row " << greatestRow()+1 << '\n';
    cout << "The most 1's are in column " << greatestCol()+1 << '\n';
    cout << "There are " << countOnes() << " 1's in the array\n";
    evenAndOdd();
    return 0;
}


0 1 0 1 1 1 0 1
0 0 1 0 1 0 1 0
0 0 0 0 1 1 1 1
0 0 1 1 0 1 0 0
1 1 0 1 1 1 0 1
0 0 1 1 0 0 0 1
1 1 0 1 1 0 0 0
0 1 0 1 1 1 0 0
The most 1's are in row 5
The most 1's are in column 4
There are 32 1's in the array
Row 1 odd
Row 2 odd
Row 3 even
Row 4 odd
Row 5 even
Row 6 odd
Row 7 even
Row 8 even


Hi, dhayden. I'm having trouble with the same assignment and I did exactly as you said, but I'm having errors in my program. Can you please go over on how to find the total, even, and odd functions as well as the int main.
this is the txt file. That's the numbers in this file. i am copying and pasting the numbers from file.
0
1
0
1
1
1
0
1
0
0
1
0
1
0
1
0
0
0
0
0
1
1
1
1
0
0
1
1
0
1
0
0
1
1
0
1
1
1
0
1
0
0
1
1
0
0
0
1
1
1
0
1
1
0
0
0
0
1
0
1
1
1
0
0
> but I'm having errors in my program.
Well that would be the most sensible thing to post then.

We're all familiar with the string of 0 and 1, so posting it again doesn't do much.
As salem c said, please post your code.

Assuming that you've coded the countOnesInRow() and countOnesInColumn() functions that I mentioned then:

In pseudo-code, you find the total like this:
1
2
3
4
for each row {
   call countOnesInRow() on that row and add the result to the total
}
return the total;


As for oddAndEven(), a number N is odd if N%2 == 1. The % operator gives the remainder of the division, so it's the remainder of N/2. So the pseudocode is:
1
2
3
4
5
6
7
8
for each row {
    call countOnesInRow() on the row
    if (the value is odd) {
         print that the row is odd
   } else {
        print that the row is even.
   }
}

Ok, so for the main part the int main(), how would you write the function in order for the computer to find the text file and do the countings as mentioned because we were given a text file?
See: https://cplusplus.com/doc/tutorial/files/

You can open a file for reading like so:

1
2
3
4
5
#include <fstream>

// ...

std::ifstream input("filename.txt");


To read, for example, an int, you can do:
1
2
3
int my_int;
input >> my_int;
std::cout << my_int << '\n';


If your file is a series of ints, separated by newlines or whitespace, you can do:
1
2
3
4
5
6
7
int value;
while (input >> value)
{
    // do something with value:
    cout << value << ' ';
}
cout << '\n';
Last edited on
Thank you Mr. Ganado. I will try that method!
Last edited on
Hi, I typed in the entire code, but I got 18 errors in my program.

This is my code:

// This function should return the row number with greatest number of 1’s in the array.
int greatestRow()
{
int result = 0;
int most = -1; // number of 1`s in the result row
for (int r = 0; r < 8; ++r) {
int count = countOnesInRow(r);
if (count > most) {
most = count;
result = r;
}
}
return result;
}
// This function should return the column number with greatest number of 1’s in the array.
int greatestColumn()
{
int result = 0;
int most = -1; // number of 1`s in the result column
for (int c = 0; c < 8; ++c) {
int count = countOnesInColumn(c);
if (count > most) {
most = count;
result = c;
}
}
return result;
}
for each row{
call countOnesInRow() on that row and add the result to the total
}
return the total;
}
for each row{
call countOnesInRow() on the row
if (the value is odd) {
print that the row is odd
}
else {
print that the row is even.
}
}



int main()
{
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
cin >> arr[i][j];
if (i == 0 && j == 1) {
int v;
cin >> v; // skip the '64'
}
}
}

printArray();
cout << "The most 1's are in row " << greatestRow() + 1 << '\n';
cout << "The most 1's are in column " << greatestCol() + 1 << '\n';
cout << "There are " << countOnes() << " 1's in the array\n";
evenAndOdd();
return 0;

int value;
while (input >> value)
{
// do something with value:
cout << value << ' ';
}
cout << '\n';
}
The file that was assigned to me, I had it in my D Drive
Last edited on
I got 18 errors in my program


Well thank goodness you didn't give us any information about what those errors are! Because it's much easier to solve your problems when you give us as little information as possible about it!
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Line 7: countOnesInRow() is not defined anywhere.

Line 21: countOnesInColumn is not defined anywhere.

Lines 30-43: What's this crap? It's not inside a function.

Lines 60-62: You need:
1
2
#include <iostream>
using namespace std;


Line 63: evenAndOdd() is not defined anywhere.

Lines 66-72: There lines are never reached. Why are they there?

Fix your errors, then post what you're having a problem with.
if you are not going to make any effort, just drop the class.
What in the –?

Ok...this is weird.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
for each row{
call countOnesInRow() on that row and add the result to the total
}
return the total;
}
for each row{
call countOnesInRow() on the row
if (the value is odd) {
print that the row is odd
}
else {
print that the row is even.
}
}


Er, that looks like the pseudocode block that @dhayden posted.

Shanaya, do you know what pseudocode even is? Hint is in the prefix "pseudo." It is not actual code; therefore it will not compile or run. It is meant for you to read over it and then write actual code based on the pseudocode. It is very common for large projects– I use it for anything over about 200 lines or so (large is a relative term).

Rewrite that code block (lines 29-42) as real code and try recompiling.

Also, who reported AbstractAnon? He was helping the OP, for goodness' sake.
Last edited on
Without using the stl:

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
#include <iostream>
#include <fstream>

constexpr size_t ARRSZE {8};

using matrix = int[ARRSZE][ARRSZE];

void readMat(matrix arr)
{
	std::ifstream ifs("weekas1.txt");

	if (ifs)
		for (size_t r = 0; r < ARRSZE; ++r)
			for (size_t c = 0; c < ARRSZE; ++c)
				ifs >> arr[r][c];
}

void printMat(const matrix arr)
{
	std::cout << '\n';

	for (size_t r = 0; r < ARRSZE; ++r) {
		std::cout << "Row " << r + 1 << ":  ";

		for (size_t c = 0; c < ARRSZE; ++c)
			std::cout << arr[r][c] << "  ";

		std::cout << '\n';
	}
}

size_t countOnesInRow(const matrix arr, size_t rowNum)
{
	size_t cnt {};

	for (size_t c = 0; c < ARRSZE; ++c)
		cnt += arr[rowNum][c] == 1;

	return cnt;
}

size_t countOnesInColumn(const matrix arr, size_t colNum)
{
	size_t cnt {};

	for (size_t r = 0; r < ARRSZE; ++r)
		cnt += arr[r][colNum];

	return cnt;
}

size_t greatestRow(const matrix arr)
{
	size_t result {};
	size_t most {};

	for (size_t r = 0; r < ARRSZE; ++r)
		if (const auto count {countOnesInRow(arr, r)}; count > most) {
			most = count;
			result = r;
		}

	return result;
}

size_t greatestCol(const matrix arr)
{
	size_t result {};
	size_t most {};

	for (size_t c = 0; c < ARRSZE; ++c)
		if (const auto count {countOnesInColumn(arr, c)}; count > most) {
			most = count;
			result = c;
		}

	return result;
}

size_t countOnes(const matrix arr)
{
	size_t cnt {};

	for (size_t r = 0; r < ARRSZE; ++r)
		for (size_t c = 0; c < ARRSZE; ++c)
			cnt += arr[r][c] == 1;

	return cnt;
}

int main()
{
	matrix arr {};

	readMat(arr);
	printMat(arr);

	std::cout << "\nRow " << greatestRow(arr) + 1 << " has the most 1's\n";
	std::cout << "Column " << greatestCol(arr) + 1 << " has the most 1's\n";
	std::cout << "There are " << countOnes(arr) << " 1's in the array\n";
}




Row 1:  0  1  0  1  1  1  0  1
Row 2:  0  0  1  0  1  0  1  0
Row 3:  0  0  0  0  1  1  1  1
Row 4:  0  0  1  1  0  1  0  0
Row 5:  1  1  0  1  1  1  0  1
Row 6:  0  0  1  1  0  0  0  1
Row 7:  1  1  0  1  1  0  0  0
Row 8:  0  1  0  1  1  1  0  0

Row 5 has the most 1's
Column 4 has the most 1's
There are 32 1's in the array

Registered users can post here. Sign in or register to post.