Hi im having trouble passing my 2d array to a function. I saved a matrix from a file into a 2d array, but i cannot seem to understand how to pass it so i can print out its diagonal.....
Heres my assignment:
1. Call another user-defined function to print only the diagonal numbers and print the results on the monitor.
Also if someone could also help me or at least tell me where to start:
2. Call a user-defined function to find the average of each row using pointer notations and print the result inside
this function into another file (output.dat).
The file is 3 X 3 and its
1 2 3
4 5 6
7 8 9
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
usingnamespace std;
void Raverage (float*, int*, int*);
void Diagonal (float*, int*, int*);
int main()
{
ifstream infile;
infile.open("input.dat");
if (infile.fail())
{
cout << "File could not be opened" << endl;
exit(1);
}
float matrix[100][100];
float *ptrmatrix;
ptrmatrix = &matrix[0][0];
int m;
int n;
int *pm;
int *pn;
pm = &m;
pn = &n;
for (int i = 0; i < 3 ; i++)
{
for (int j = 0; j < 3; j++)
{
infile >> matrix[i][j];
}
}
infile.close();
cout << "The diagonal is: " << endl;
for (int i = 0; i < 3; i++)
{
cout << setw(3) << matrix[i][i];
}
Raverage (&matrix[0][0],pm, pn);
Diagonal(&matrix[0][0], pm, pn);
}
void Raverage(float *x, int *row, int *col)
{
ofstream outfile;
outfile.open("output.dat");
if (outfile.fail())
{
cout << "The file could not be opened" << endl;
exit(1);
}
float a = ((*x) + (*x + 1) + (*x + 2)) / 3;
float b = ((*x + 3) + (*x + 4) + (*x + 5)) / 3; // works but is the wrong way to do get the average, need a for loop to do it
float c = ((*x + 6) + (*x + 7) + (*x + 8)) / 3;
outfile << "The average of the first row is: " << a << endl;
outfile << "The average of the second row is: " << b << endl;
outfile << "The average of the third row is: " << c << endl;
outfile.close();
}
void Diagonal(float *y, int *row, int *col)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << *(*(y + i) + i) << endl; // here lies the problem
}
cout << endl;
}
}
Lines 59-61 fail in the worst possible way; they seem to function correctly.
Remember, 'x' is a pointer and points to 'matrix[0][0]'. The 'matrix[0][0]' contains '1.0', from the file.
(*x) is 1.0
(*x + 1) is (1.0 + 1) is 2.0
(*x + 2) is (1.0 + 2) is 3.0
(*x + 3) is (1.0 + 3) is 4.0
...
Any other test input would give that away.
If you want to refer to 'matrix[0][2]' with 'x', then *( x + 2 )
If you want to refer to 'matrix[3][0]' with 'x', then *( x + 3*100 )
If you want to refer to 'matrix[7][42]' with 'x', then *( x + 7*100 + 42 )
The '100' comes from line 19.
Rationale:
2D-array 'arr[N][X]' is a continuous block. First X elements form the row 'arr[0]', next X elements form the row 'arr[1]', and so forth. You have to know X in order to calculate where each "row" starts within the block.
One can pass array as parameter void Raverage( float z[][100], constint rows, constint cols ); without specifying how many rows z has, and embedding the 'X' in the declaration of z.
When you do have float z[][K]
a dereference z[i][j]
does under the hood *( &(z[0][0]) + i*K + j )