find the error in this program

samaira

samaira

@samaira-P4RsEf Oct 22, 2024
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],ch,c[3][3];
printf("\n enter the elements of first matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[j]);
printf("\t%d",a[j]);
}
printf("\n");
}
printf("\n enter the elements for the second matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[j]);
printf("\t%d",b[j]);
}
printf("\n");
}
printf("matrix operations\n1.addition\n2.subtraction\n enter ur choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[j]=a[j]+b[j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&c[j]);
printf("\t%d",c[j]);
}
printf("\n");
}
break;
case 2:
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[j]=a[j]-b[j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&c[j]);
printf("\t%d",c[j]);
}
printf("\n");
}
break;
}
getch();
}
the program is not executing

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Saandeep Sreerambatla

    Saandeep Sreerambatla

    @saandeep-sreerambatla-hWHU1M Jul 29, 2009

    remove these two scanf functions scanf("%d",&c[j]);


    I think you will be able to get the output.
  • tashirosgt

    tashirosgt

    @tashirosgt-7BsIre Jul 29, 2009

    The program executes when I run it.

    Why are you reading-in the values of c[][] with the statement
    scanf("%d",&c[j]);
    ?

    You already computed these values, so you don't need to read them.
  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Jul 29, 2009

    Yes, English-Scared is right. Removing the two scanf statement (each present inside case statement) will fix the issue.

    I have modified your code with above change and did some additional changes.

    #include "stdio.h"
    
    int main() {
      int a[3][3] = { 0 };
      int b[3][3] = { 0 };
      int c[3][3] = { 0 };
      int i = 0, j = 0;
      int ch = 0;
    
      printf("Enter the elements of first matrix:\n");
      for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
          scanf("%d", &a[i][j]);
          printf("\t%d", a[i][j]);
        }
        printf("\n");
      }
    
      printf("Enter the elements for the second matrix:\n");
      for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
          scanf("%d", &b[i][j]);
          printf("\t%d", b[i][j]);
        }
        printf("\n");
      }
    
      printf("Matrix operations\n1. Addition\n2. Subtraction\nEnter your choice: ");
      scanf("%d", &ch);
    
      switch(ch) {
      case 1:
        printf("Result:\n");
        for(i = 0; i < 3; i++) {
          for(j = 0; j < 3; j++) {
            c[i][j] = a[i][j] + b[i][j];
            printf("\t%d", c[i][j]);
          }
          printf("\n");
        }
        break;
    
      case 2:
        printf("Result:\n");
        for(i = 0; i < 3; i++) {
          for(j = 0; j < 3; j++) {
            c[i][j] = a[i][j] - b[i][j];
            printf("\t%d", c[i][j]);
          }
          printf("\n");
        }
        break;
    
      default:
        printf("Invalid choice\n");
      }
    
      return 0;
    }
    
    The additional changes i have done is:
    1. Removed the include file "conio.h" and function call getch as it was not necessary (don't have something in code till its not required).
    2. Initialized variables during declaration (a good practice to follow).
    3. Optimized the addition/subtraction logic by doing addition and printing result in same loop instead of different loops.
    4. Added a 'default' case to switch body (a good practice to follow).

    -Pradeep
  • kate_amar

    kate_amar

    @kate-amar-uECzE1 Jul 29, 2009

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i,j,a[3][3],b[3][3],ch,c[3][3];
    printf("\n enter the elements of first matrix: ");
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    scanf("%d",&a[i][j]);
    printf("\t%d",a[i][j]);//take another loop for printing
    }
    printf("\n");
    }
    printf("\n enter the elements for the second matrix: ");
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    scanf("%d",&b[i][j]);
    printf("\t%d",b[i][j]);//take another loop for printing
    }
    printf("\n");
    }
    printf("matrix operations\n1.addition\n2.subtraction\n enter ur choice: ");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    c[i][j]=a[i][j]+b[i][j];
    }
    }
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    //scanf("%d",&c[i][j]); {no need to scan}
    printf("\t%d",c[i][j]);
    }
    printf("\n");
    }
    break;
    case 2:
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    c[i][j]=a[i][j]-b[i][j];
    }
    }
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    scanf("%d",&c[i][j]);
    printf("\t%d",c[i][j]);
    }
    printf("\n");
    }
    break;
    }
    getch();
    }
    
    don't use scanf bcz u already scanning elements