Fill 2D Array

I have already fixed the code. Look at other post
Last edited on
The "fillArray()" function has a parameter with the identifier "arrayData". Later within the function's compound statement, another array is declared with the same identifier. The compiler is unable to determine which array is being referred to when that identifier is used in your source code. The identifiers within your source code must be unique, or you will receive a compilation error.

The definition of the "fillArray()" function and the first for loop within the function's compound statement are both missing a curl bracket. These missing brackets would have been much easier to locate if you had used the code tags to format your source 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
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>

using namespace std;
static const string DATAFILE = "array_data.txt";
static const int DIM = 10;

void fillArray(int arrayData[DIM][DIM]) // arrayData
{
    srand(time(0));
    int arrayData[]= {0}; // arrayData
    for (int i=0; i<100; i++){
        arrayData[i][i]=rand()%100;
    }

    for(int i=0; i<100; i++){
        cout<< arrayData[i]<< "";
    }
}

int main() 
{
    int write_data[DIM][DIM];
    fillArray(write_data);
    printArray("Random array",write_data);
    writeFile(DATAFILE,write_data);

    int read_array[DIM][DIM];
    readFileIntoArray(DATAFILE, read_array);
    transposeAndMultiplyArray(read_array, 5);
    printArray("Transposed and multiplied array", read_array);
    system("pause");
    return 0;
}


EDIT: After rereading the source code, I discovered a few more problems that I have not yet mentioned above. I'm sure the creator of this thread is able to solve the remaining problems without help from another member.
Last edited on
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace std;

// DO NOT ALTER THESE CONSTANTS!
static const string DATAFILE = "array_data.txt";
static const int DIM = 10;
//===============================

/**
* INSTRUCTIONS
* Complete each function in order, beginning with fillArray. Make sure your program compiles
* before moving on to the next function! DO NOT MODIFY THE MAIN() FUNCTION.
*
* SAMPLE OUTPUT
Random array
7 49 73 58 30 72 44 78 23 9
40 65 92 42 87 3 27 29 40 12
3 69 9 57 60 33 99 78 16 35
97 26 12 67 10 33 79 49 79 21
67 72 93 36 85 45 28 91 94 57
1 53 8 44 68 90 24 96 30 3
22 66 49 24 1 53 77 8 28 33
98 81 35 13 65 14 63 36 25 69
15 94 29 1 17 95 5 4 51 98
88 23 5 82 52 66 16 37 38 44
Transposed and multiplied array
35 1225 1825 1450 750 1800 1100 1950 575 225
1000 325 2300 1050 2175 75 675 725 1000 300
75 1725 45 1425 1500 825 2475 1950 400 875
2425 650 300 335 250 825 1975 1225 1975 525
1675 1800 2325 900 425 1125 700 2275 2350 1425
25 1325 200 1100 1700 450 600 2400 750 75
550 1650 1225 600 25 1325 385 200 700 825
2450 2025 875 325 1625 350 1575 180 625 1725
375 2350 725 25 425 2375 125 100 255 2450
2200 575 125 2050 1300 1650 400 925 950 220

*/


/**
* FUNCTION #1 - 10 points
* This function should fill the 2D array with random numbers between 0 and 100
* The numbers must be different every time the program is run, so you need to
* research the best way to generate random numbers in this range.
*/
void fillArray(int arrayData[DIM][DIM]){
for (int i=0;i<DIM;i++){
for (int j=0;j<DIM;j++){
arrayData[i][j]=rand()%100;
}
}
}


/**
* FUNCTION #2 - 10 points
* Print the given array in matrix form with sufficient spacing between numbers
* so that the columns line up. The message should be printed at the top of the matrix.
* See the sample output at the top of this file.
*/
void printArray(string message, int arrayData[DIM][DIM]){
cout << message << endl;
for (int i=0;i<DIM;i++){
for (int j=0;j<DIM;j++){
cout << setw(5) << arrayData[i][j];
}
cout <<endl;
}

}

/**
* FUNCTION #3 - 10 points
* Write the given matrix to a file named by the fileName parameter. The file
* should be written in matrix format (only numbers - no other text or bars)
*/
void writeFile(string fileName, int arrayData[DIM][DIM]){
ofstream myfile;
myfile.open("arrayData.txt");
for (int i=0;i<DIM;i++){
for (int j=0;j<DIM;j++){
myfile << arrayData[i][j] << " ";
}
myfile << "\n";
}
}

/**
* FUNCTION #4 - 10 points
* Read the file named by the fileName parameter into the
* 2D array arrayData. You can assume that the file is formatted
* correctly and contains integers.
*/
void readFileIntoArray(string fileName, int arrayData[DIM][DIM]) {
ifstream myfile;
myfile.open("arrayData.txt");
for (int i=0;i<DIM;i++){
for (int j=0;j<DIM;j++){
myfile >> arrayData[i][j];

}
}
}

/**
* FUNCTION #5 - 10 points
* Transpose the input array, multiplying each value by the supplied multiplier
*/

void transposeAndMultiplyArray(int arrayData[DIM][DIM], int multiplier){
for (int i=0;i<DIM;i++){
for (int j=0;j<DIM;j++){
arrayData[i][j]= arrayData[i][j]*multiplier;
}
}
}


/**
* DO NOT MODIFY THIS CODE!
*/
int main()
{
int write_data[DIM][DIM];
fillArray(write_data);
printArray("Random array",write_data);
writeFile(DATAFILE,write_data);

int read_array[DIM][DIM];
readFileIntoArray(DATAFILE, read_array);
transposeAndMultiplyArray(read_array, 5);
printArray("Transposed and multiplied array", read_array);
system("pause");
return 0;
}
^^ That is how I worked out the code. It works and compiles properly.
Topic archived. No new replies allowed.