Jainam Jhaveri
Jainam Jhaveri

Reputation: 1561

C error : "Subscripted value is neither array nor pointer" in row echelon form

#include <stdio.h>

// Printing matrix

void printmatrix(float a[]){
int i, j;

printf("\nOutput matrix: \n");
for(i=0; i<3; i++){
    for(j=0; j<4; j++){
        printf("%f ", a[i][j]);
    }
    printf("\n");
}
}


void main(){
int i, j, k, l, count;
float temp, value;
float a[3][4];

printf("Enter a matrix: \n");

// Taking matrix input from the user

for(i=0; i<3; i++){
    for(j=0; j<4; j++){
        scanf("%f", &a[i][j]);
    }
}

printmatrix(a);

// Checks 1 in 2nd and 3rd row and replaces the corresponding row with 1st row

for(i=0, count =0; i<3; i++){
    if(a[i][0] == 1 && i != 0){
        for(j=0; j<4; j++){  // swapping two rows
            temp = a[i][j];
            a[i][j] = a[0][j];
            a[0][j] = temp;
        }
        break;
    }
    count++;
    // If there is no 1 in any row then divide 1st row by a[0][0]
    if(count == 3){
        k = 0;
        value = a[0][0];
        for(j=0; j<4; j++){
            a[k][j] = a[k][j]/value;
        }
    }
}

// Subtract 2nd and 3rd row from 1st row to make a[1][0]=a[2][0]=0

for(i=1; i<3; i++){
    value = a[i][0];
    for(j=0; j<4; j++){
        a[i][j] = a[i][j] - value * a[0][j];
    }
    printf("\n");
}

printmatrix(a);

// Checks 1 in 3rd row and replaces 3rd row and 2nd row

for(i=1, count =0; i<3; i++){
    if(a[i][0] == 1 && i != 1){
        for(j=0; j<4; j++){ // swapping two rows
            temp = a[i][j];
            a[i][j] = a[0][j];
            a[0][j] = temp;
        }
        break;
    }
    count++;
    // If there is no 1 in any row then divide 2nd row by a[1][1]
    if(count == 2){
        k = 1;
        value = a[1][1];
        for(j=0; j<4; j++){
            a[k][j] = a[k][j]/value;
        }
    }
}

// Subtract 3rd row from 1st row to make a[2][1]=0

for(i=2; i<3; i++){
    value = a[i][1];
    for(j=0; j<4; j++){
        a[i][j] = a[i][j] - value * a[1][j];
    }
    printf("\n");
}

printmatrix(a);

}

Upvotes: 2

Views: 702

Answers (2)

John Bode
John Bode

Reputation: 123558

You've declared printmatrix to take a 1-D array as a parameter, but you're trying to index that parameter as a 2-D array. That won't work.

Some options:

Since you're assuming you're always working with a 3x4 matrix, you can simply declare it as such in the function parameter list:

void printmatrix( float a[3][4] )
{
  ...
}

However, be aware that within the context of a function parameter declaration, any declaration of the form T a[] or T a[N] will be treated as T *a; as a result, the declaration above will be interpreted as

void printmatrix( float (*a)[4] )
{
  ...
}

Edit - had the type of a wrong; that's what I get for posting without testing

In general, you should not hardcode your array dimensions in the printmatrix function; what if you want to display 2 matrices with different dimensions? A better idea is to pass the array dimensions as separate arguments.

If you're using a C99 compiler or a C2011 compiler that supports variable-length arrays, this is easy:

void printmatrix( size_t rows, size_t cols, float (*a[rows]) )
{
  size_t i, j;

  printf("\nOutput matrix: \n");
  for(i=0; i<rows; i++){
    for(j=0; j<cols; j++){
      printf("%f ", a[i][j]);
  }
  printf("\n");
}   

and you would call it as

printmatrix( 3, 4, matrix );

If you're not using a C99 compiler or a C2011 compiler that supports variable-length arrays, you'll have to do something a little hackish. You can explicitly pass the address of the first element, and then manually compute the 2D offset, like so:

void printmatrix( size_t rows, size_t cols, float *a )
{
  size_t i, j;
  for ( i = 0; i < rows; i++ )
  {
    for ( j = 0; j < cols; j++ )
      printf("    %f", a[i * rows + j] );
    putchar( '\n' );
  }
}

and you would call it as

printmatrix( 3, 4, &matrix[0][0] );

Upvotes: 2

Vagish
Vagish

Reputation: 2547

Argument to void printmatrix(float a[]) is not correct.

Replace function prototype with:

void printmatrix(float a[][])

Upvotes: 0

Related Questions