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?
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:
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
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?
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
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>
usingnamespace 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.
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.