0

i have to add two matrices and i am having this error:
"error array subscript is not an integer" line 24

here are my codes:

#include <stdio.h>
void inputarray(int arg[][3], int rows);
void printarray (int arg[][3], int rows);
void inputarray2(int arg[][3], int rows);
void printarray2(int arg[][3], int rows);
int main ()
{
int M[3][3];
int action, sum=0;
inputarray(M,3);
printarray(M,3);
inputarray2(M,3);
printarray2(M,3);

printf("1:Add the matrices");
printf("2:Subtract the matrices");
printf("3:Multiply the matrices");
printf("4:EXIT");
printf("Please enter your choice(1-4): ");
scanf("%d", &action);

switch(action){
    case 1:{
         sum[M][3] = inputarray[M][3] + inputarray2[M][3];

   printf("Sum of entered matrices:-\n");
   printf("%d", sum[M][3]);
   printf("\n");
      break;
   }}



return 1;
}
void inputarray(int arg[][3], int rows)
{
int row,col;
for(row=0;row<rows;row++)
{
printf("3 numbers for Row %d for first matrix : ",(row+1));
for(col=0;col<3;col++)
scanf("%d",&arg[row][col]);
}
}
void printarray (int arg[][3], int rows)
{
int x, y;
for (x=0; x<rows; x++)
{
for(y=0;y<3;y++)
printf("%d\t",arg[x][y]);
printf("\n");
}
printf("\n");
}
void inputarray2(int arg[][3], int rows)
{
int row,col;
for(row=0;row<rows;row++)
{
printf("3 numbers for Row %d of second matrix : ",(row+1));
for(col=0;col<3;col++)
scanf("%d",&arg[row][col]);
}
}
void printarray2 (int arg[][3], int rows)
{
int x, y;
for (x=0; x<rows; x++)
{
for(y=0;y<3;y++)
printf("%d\t",arg[x][y]);
printf("\n");
}
printf("\n");
}

Edited by nitish.mohiputlall: missed some information

2
Contributors
1
Reply
22
Views
3 Years
Discussion Span
Last Post by Schol-R-LEA
2

in the line in question:

 sum[M][3] = inputarray[M][3] + inputarray2[M][3];

You are treating inputarray() and inputarray2() as arrays, rather than calling them as functions. The correct syntax would be:

 sum[M][3] = inputarray(M, 3) + inputarray2(M, 3);

BTW, I strongly recommend you get into the practice of indenting your code suitably. The usual practice is to indent one level - three to eight spaces, depending on your indent style - every time you have a block of code that is inside a loop or an if statement. This practice, called 'nesting', is very important. The goal of formatting code is to make the nesting levels explicit in the layout of the program. While in C, it is purely for the benefit of the programmer, it should make reading and editing the code easier, which is why it is important to get into the habit as soon as you can. I recommend reading this article on the subject for further clarification.

Now, as this Wikipedia entry explains, there are several different styles which one can use to indent their code; what is important is not the specific style, but consistency in applying your chosen style. As long as you follow the main rule - indent inside a block, de-dent after the end of a block - you should be able to use the style you are comfortable with, **so long as you are consistent in appyling it*. those two rules are the real key; as long as your code makes the nesting express, and you don't change styles in the middle of a program, your good.

Fortunately, there exist several editors and styling programs that will format code automatically for you. I recommend AStyle as a simple one you can download and run on your computer. By default, it formats to Allman Style with an indent of four, but it is configurable to several different styles. If you download Code::Blocks, it includes AStyle as a plugin which can be called from the editor.

Here is your code agin, but formatted with AStyle:

#include <stdio.h>

void inputarray(int arg[][3], int rows);
void printarray (int arg[][3], int rows);
void inputarray2(int arg[][3], int rows);
void printarray2(int arg[][3], int rows);
int main ()
{
    int M[3][3];
    int action, sum=0;
    inputarray(M,3);
    printarray(M,3);
    inputarray2(M,3);
    printarray2(M,3);

    printf("1:Add the matrices");
    printf("2:Subtract the matrices");
    printf("3:Multiply the matrices");
    printf("4:EXIT");
    printf("Please enter your choice(1-4): ");
    scanf("%d", &action);

    switch(action) {
    case 1: {
        sum[M][3] = inputarray(M, 3) + inputarray2(M, 3);

        printf("Sum of entered matrices:-\n");
        printf("%d", sum[M][3]);
        printf("\n");
        break;
    }
    }



    return 1;
}
void inputarray(int arg[][3], int rows)
{
    int row,col;
    for(row=0; row<rows; row++)
    {
        printf("3 numbers for Row %d for first matrix : ",(row+1));
        for(col=0; col<3; col++)
            scanf("%d",&arg[row][col]);
    }
}
void printarray (int arg[][3], int rows)
{
    int x, y;
    for (x=0; x<rows; x++)
    {
        for(y=0; y<3; y++)
            printf("%d\t",arg[x][y]);
        printf("\n");
    }
    printf("\n");
}
void inputarray2(int arg[][3], int rows)
{
    int row,col;
    for(row=0; row<rows; row++)
    {
        printf("3 numbers for Row %d of second matrix : ",(row+1));
        for(col=0; col<3; col++)
            scanf("%d",&arg[row][col]);
    }
}
void printarray2 (int arg[][3], int rows)
{
    int x, y;
    for (x=0; x<rows; x++)
    {
        for(y=0; y<3; y++)
            printf("%d\t",arg[x][y]);
        printf("\n");
    }
    printf("\n");
}

Edited by Schol-R-LEA

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.