Inserting the average into the matrix

Write a function called insertedAverage : void insertedAverage (int &N, int &M, double mt[51][51], int k)
The function will insert at position k a new line in the matrix, having on each column a rational number equal to the arithmetic mean of the elements on that column.

Restrictions:
The rows and columns of the matrix are numbered starting with 0;
At the end of the function the matrix must contain an extra line according to the statement;
The function will not return anything.


EX:
N=2, M=2;
mt[51][51]={{1,1}, {2,2}};
insertedAverage(N, M, mt, 1);

mt =
// 1 1
// 1.5 1.5
// 2 2


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void insertedAverage(int &N, int &M, double mt[52][52], int k) {
              double v[52];
              for(int i = n-1; i >= k; i++)
                      for(int j = 0;j < m; j++)
                           a[i+1][j]=a[i][j];
                for(i = 0;i < m; i++)
                     a[k][i]=v[i];
                  n++;

    int i, j;
     int sum = 0, column = 0, line = 0;

        for (j = 1; j <= M; j++) {
          for(i = 1; i <= N; i++ ) {
            sum += mt[line][column], column = j, line =i;
            double average = sum / M;
        }
          double average = (double)suma / M;
        }
              }
As formatted - what's the question?

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
void insertedAverage(int& N, int& M, double mt[52][52], int k)
{
	double v[52];

	for (int i = n - 1; i >= k; i++)
		for (int j = 0; j < m; j++)
			a[i + 1][j] = a[i][j];

	for (i = 0; i < m; i++)
		a[k][i] = v[i];

	n++;

	int i, j;
	int sum = 0, column = 0, line = 0;

	for (j = 1; j <= M; j++) {
		for (i = 1; i <= N; i++) {
			sum += mt[line][column], column = j, line = i;
			double average = sum / M;
		}

		double average = (double)suma / M;
	}
}

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>
using namespace std;

const int SIZE = 51;

void print( double A[SIZE][SIZE], int rows, int cols )
{
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ ) cout << A[i][j] << '\t';
      cout << '\n';
   }
}

void insertedAverage( int &N, int &M, double A[SIZE][SIZE], int k )
{
   for ( int j = 0; j < M; j++ )
   {
      double value = 0;
      for ( int i = 0; i < N; i++ ) value += A[i][j];
      value /= N;
      for ( int i = N; i > k; i-- ) A[i][j] = A[i-1][j];
      A[k][j] = value;
   }
   N++;
}

int main()
{
   int N = 2, M = 2;
   int k = 1;
   double A[SIZE][SIZE] = { { 1, 1 }, { 2, 2 } };
   print( A, N, M );   cout << '\n';
   insertedAverage( N, M, A, k );
   print( A, N, M );
}

Thanks @lastchance !!
Topic archived. No new replies allowed.