I was assigned to write a program using a 2D array. The code was returned to me with some comments of improvement by the professor. I am unsure how to correct these mistakes.
Here is my original code:
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/* Program to fill a two dimensional array, output the array, and determine
the sum of each column and the sum of each row using loops and functions
input: Assignment8.dat file
output: filled array, sum of columns and rows
processing: use a nested loop to fill the array, void function to output
the data
*/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int ROW = 20;
const int COL = 12;
void fillArray(int a[][COL]);
void outputArray(int b[][COL]);
int sumRow(int c[][COL]);
int sumCol(int d[][COL]);
int main()
{
int TwoD[ROW][COL];
//call to function to fill array
fillArray(TwoD);
//call to function to output array
outputArray(TwoD);
//output row sum
sumRow(TwoD);
//output column sum
sumCol(TwoD);
return 0;
}
/* Function to fill a two dimnensional array that may a maximum of contain 25 rows
and 15 columns.
input: a two dimensional array is passed to the function
output: the filled array is sent back to the function call
processing: use a nested loop to fill the array
*/
void fillArray(int a[][COL])
{
ifstream file;
file.open("Assignment8.dat");
if (!file.fail()) //test file is opened
{
cout << "File correctly opened" << endl;
file.ignore(18, '\n'); //ignore first row containing actual numbers
while (!file.eof())
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
file >> a[i][j];
}
}
}
}
else
{
cout << "Error opening file!" << endl;
file.close();
}
}
/* Function to output a 2 dimensional array
input: the numbers in the array will be output to
the screen nothing is sent back to the function call
processing: use a nested loop to output the array
*/
void outputArray(int b[][COL])
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
cout << setw(5) << b[i][j];
}
cout << endl;
}
}
/* Function to calculate the sum of each row from a two
dimensional ray and return the data to function as
an array.
input: a two dimensional array
output: the sum of each row
processing: use a nested loop to calculate the sum
of each row
*/
int sumRow(int c[][COL])
{
int rowTotal = 0;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
rowTotal += c[i][j];
}
cout << "Row " << (i + 1) << " total: " << rowTotal << endl;
}
return rowTotal;
}
/* Function to calculate the sum of each column from a two
dimensional ray and return the data to function as
an array.
input: a two dimensional array
output: the sum of each column
processing: use a nested loop to calculate the sum
of each row
*/
int sumCol(int d[][COL])
{
int colTotal = 0;
for (int j = 0; j < COL; j++)
{
for (int i = 0; i < ROW; i++)
{
colTotal += d[i][j];
}
cout << "Column " << (j + 1) << " total: " << colTotal << endl;
}
return colTotal;
}
|
Question 1:
The first issue she asks me to correct is instead of directly defining the actual number of rows(20) and columns(12) she wants me to declare the maximum which is 25 rows and 15 columns.
This first screenshot is my original functioning code.
http://i.imgur.com/g8PpGTG.png
This is what happens when I try to change the row and column declaration to the max. How do I prevent this from happening? Is there another way I should code the max?
http://i.imgur.com/J1B6PlT.png
Question 2:
I am supposed to have the program output the actual number of rows and then the actual number of columns in the fill function. How do I achieve this? I am not sure how to begin.
Question 3:
My professor commented "nested loop to fill arrays should only have 2-levels". What does this mean? Isn't my code already only 2-levels? I only have one nested loop inside another.
Question 4:
How do I turn my functions that sums rows and columns into a 1D array that send back to main? I already have the calculation coded, but I don't know how to store this calculation as an array. How would I output the data?
Question 5:
What does this mean? "need to reset sum to 0 between
iterations of out loop in functions to find sums", why is this necessary to do when the function already properly calculates the sums? How do I go about achieving this?
Thanks for all your help. Sorry if this format is not correct for posting.