943,971 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2237
  • C RSS
Nov 2nd, 2004
0

A Small Problem ....plz Help Me Out....

Expand Post »
hi
"this program is for adding 2 tables with 3 rows and 4 columns" ....
so i have done like this......
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][4],b[3][4],c[3][4];
int i,j;
clrscr();
printf("\nENTER AN ARRAY:");
printf("\n\nFIRST TABLE:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nENTER AN ARRAY:");
printf("\n\nSECOND TABLE:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nENTER AN ARRAY:");
printf("\nSUM OF TABLES:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%4d",c[i][j]);
c[i][j]=a[i][j]+b[i][j];
printf("\n");
}
}
getch();
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
galmca is offline Offline
47 posts
since Oct 2004
Nov 2nd, 2004
0

Re: A Small Problem ....plz Help Me Out....

But When I Run This Code.....then Here I Have To Print The Sum Of 2 Tables...
First Table:
1 2 3 4
5 6 7 8
9 10 11 12

Second Table:
10 11 12 13
14 15 16 17
18 19 20 21

Then It Prints The Sum.....exactly..the Way It Should....but Not In That Format Of Table.....
But Like....
11
13
15
17
19
21
23
25
27
29
31
33
In A Single Line......now What Do I Do....?to Make It Print Like That The Other 2 Tables Are....in 3 Rows And 4 Columns....plz Help Me Out....and Guide Me Here/////
Reputation Points: 10
Solved Threads: 0
Light Poster
galmca is offline Offline
47 posts
since Oct 2004
Nov 2nd, 2004
0

Re: A Small Problem ....plz Help Me Out....

I think you should move the printf ("\n"); outside of one of the 2 for loops:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][4],b[3][4],c[3][4];
int i,j;
clrscr();
printf("\nENTER AN ARRAY:");
printf("\n\nFIRST TABLE:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nENTER AN ARRAY:");
printf("\n\nSECOND TABLE:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nENTER AN ARRAY:");
printf("\nSUM OF TABLES:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%4d ",c[i][j]);
c[i][j]=a[i][j]+b[i][j];
}
printf("\n");
}
getch();
}
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Nov 2nd, 2004
0

Re: A Small Problem ....plz Help Me Out....

Greetings,

There are a few issues with the program. Nothing major though.

Firstly, frrossk showed the first issue. The new line should only take place after the first loop is continuing, not the second loop. Also, the lines of:
  1. printf("%4d ",c[i][j]);
  2. c[i][j]=a[i][j]+b[i][j];
Try flipping the two codes around:
  1. c[i][j]=a[i][j]+b[i][j];
  2. printf("%4d ",c[i][j]);
It would do no good to display a variable and then set it.

- Stack Overflow
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Nov 2nd, 2004
0

Re: A Small Problem ....plz Help Me Out....

Quote originally posted by frrossk ...
I think you should move the printf ("\n"); outside of one of the 2 for loops:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][4],b[3][4],c[3][4];
int i,j;
clrscr();
printf("\nENTER AN ARRAY:");
printf("\n\nFIRST TABLE:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nENTER AN ARRAY:");
printf("\n\nSECOND TABLE:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nENTER AN ARRAY:");
printf("\nSUM OF TABLES:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%4d ",c[i][j]);
c[i][j]=a[i][j]+b[i][j];
}
printf("\n");
}
getch();
}
Also try to use \t instead of %4d. I think it will give a better look
Reputation Points: 10
Solved Threads: 0
Light Poster
jigvesh is offline Offline
44 posts
since Oct 2004
Nov 2nd, 2004
0

Re: A Small Problem ....plz Help Me Out....

Right, I missed it. Thx, Stack...jigvesh too
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Nov 2nd, 2004
0

Re: A Small Problem ....plz Help Me Out....

thx guys.....although it was just a small mistake of putting my("\n") outside the j loop but still it was worth telling me that...........thx alot..........my program is running absolutely fine now.........
Reputation Points: 10
Solved Threads: 0
Light Poster
galmca is offline Offline
47 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: code explanation required
Next Thread in C Forum Timeline: Duplicate lines when writing to output file. One formatted and on unformatted.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC