find the error in this program
#include
#include
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
#include
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
-
Saandeep Sreerambatlaremove these two scanf functions scanf("%d",&c[j]);
I think you will be able to get the output. -
tashirosgtThe 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_agrawalYes, 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
#include
don't use scanf bcz u already scanning elements#include 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(); }
You are reading an archived discussion.
Related Posts
There are many threads that speak in positive terms about ideas for projects. But there must also be people who know how project disasters happen. Can those people tell us...
A UFO sighting claim has once again been made in the UK, with a mystery object now being photographed hovering above a magnificent stately home in Hertfordshire.
The picture of...
PLS GUYS HELP ME GUYS I M 2009 PASS OUT B TECH IN COMPUTER SCIENCE
I DON'T HAV JOB:sad:
I DON'T HAV ANY FUTURE😔
I DON'T HAV HOPE😔
WHEN I...
So what does everyone think of the upcoming merger with Microsoft acquiring Yahoo? Is it good? Will they be successful at taking on Google?
Hello everyone,
I am a computer engineer and kinda venturing out to work on a different project. I am attempting to build a lift that raises a platform when someone...