Hi could someone please helping in starting this program? I am not asking anyone to do my work for me i just want to know how i can start the program and steps to follow please( i am new to c++) thanks
write a program that reads an n*n matrix file in the form(for the 2*2 case)
1 2
3 4
and then overwrites this file by replacing each element by the average of its four adjacent elements(right,left,up and down). The first line of the file indicates the number of row and column elements of the matrix. If an adjacent element is outside the matrix boundary, its value is taken to zero. You can close and open the file more than once.

5 Years
Discussion Span
Last Post by dkalita
using namespace std;
int main()
   int *matrix;
   int dimension;
   /*read dimension first and initialise "dimension" and then allocate apropriate memory to "matrix" and then read the matrix into this array. This can also be done inside a function which will make your code more usable*/

   /*process the matrix, better write a function and pass the matrix to it and do the processing inside that.*/

  /*write back the new matrix data into the same file. This can also be put inside a function.*/

This is how you can start.
Write the fileRead, matrixProcess, and fileWrite method seperately and test them thoroughly before writing the next function.


by using dimension first do i use a for loop like (dimension = 0; dimension < 1000; dimension++)
then would i allocate the proper memory space to the matrix by writing: int matrix[1000]; am i on the right track?


or wait did you mean something like this?
int M[2][2] = {1,0,1,2,3,4};
for (int r = 0; r < n; i++)
for (int c = 0; c < m; c++)

where c is the column and r the row.


This is fine as well. But you do not know the dimension of the matrix before reading the file. So how do you declare the int array to store the matrix.

if the dimension you read from the input file is say N, you can allocate memory to the matrix variable as:

int *matrix = (int *)malloc(N*N*sizeof(int));

To access matrix[j], do:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.