943,976 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1339
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 16th, 2009
0

Problem in understandting Multi dimensional array and nested loops

Expand Post »
hey guys i know its kinda noobish not to understand them since i been coding for 1 month but there rlly some stuff which confuses me in multidimensional array and nested loops like here in this program
I wanted to know if i sum up like in the array for example num[5][10] i wanted to know which get summed first if i did total+=num[x][i]; but here in this program i made i got weird error which i think shouldn't produce it so please someone enlighten me because till now i understand all stuff pointers memory management and hard stuff in C but i don't understand these multi and nested loops
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int num[2][3]= {
  5. {1,1,1},{1,1,1}
  6. };
  7. int i;
  8. int x;
  9. int total=0;
  10. for(i=0;i<2;i++)
  11. for(i=0;i<3;i++) {
  12. total+=num[i][x];
  13. }
  14. printf("%d",total);
  15. return getchar();
  16. }
shouldnt it produce 6 ? please someone enlighten me in understanding this ....
Similar Threads
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Jul 16th, 2009
0

Re: Problem in understandting Multi dimensional array and nested loops

you got both loops fighting over the same index 'i'
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 16th, 2009
0

Re: Problem in understandting Multi dimensional array and nested loops

the error is probably due to the fact that 'x' isn't initialized, so it's value is something like negative eleventy thousand, causing your program to crap the bed when it uses it as one of the array indices.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 16th, 2009
-1

Re: Problem in understandting Multi dimensional array and nested loops

You have an (i) problem.
You need to use two different variables within a nested loop, such as i and j.

  1. for(i=0;i<2;i++)
  2. // for(i=0;i<3;i++) {
  3. for(x = 0; x < 3; x++ ) {
  4. total+=num[i][x];
  5. }
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 16th, 2009
0

Re: Problem in understandting Multi dimensional array and nested loops

Whats with return getchar() ? You got several other ways to pause the output.Use one of them rather than this because even though you return some random character as the return of main and exit the OS thinks that the process didn't terminate properly because of a non zero return value.Which of course doesn't cause any problem here but would definitely cause great problems in future when you write some important code.
So better correct it now.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 17th, 2009
-1

Re: Problem in understandting Multi dimensional array and nested loops

Find the corrected code.nothing to wierd about this.try to find out values of each elemnts manuall before
writing the code.

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int num[2][3]= {
  5. {1,2,3},{4,5,6}
  6. };
  7. int i;
  8. int x;
  9. int total=0;
  10. for(i=0;i<2;i++)
  11. [RED] for(i=0;i<3;i++)[/RED] //here i SHOULD BE x
  12. {
  13. total+=num[i][x];
  14. }
  15. printf("%d",total);
  16. return getchar();
  17. }
  18.  
  19. int num[2][3]= {
  20. {1,2,3},{4,5,6}
  21. };
Value assinged like this:
===========================
num[0][0]=1;
num[0][1]=2;
num[0][2]=3;
num[1][0]=4;
num[1][1]=5;
num[1][2]=6;

assign the range of loop variables according to this.1st do a dry run in case of multiD arrays.

Thanks,
DP
Last edited by Dream2code; Jul 17th, 2009 at 4:05 am.
Reputation Points: 22
Solved Threads: 12
Junior Poster
Dream2code is offline Offline
144 posts
since Jun 2009
Jul 17th, 2009
0

Re: Problem in understandting Multi dimensional array and nested loops

yah i know now but what if i wanna count like row by row like
{1,3,4} {3,5,6} it will count 1 + 3 and 3+5 and 6 + 4 ?
should i switch variables x first then i 2nd ?
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Jul 17th, 2009
0

Re: Problem in understandting Multi dimensional array and nested loops

Click to Expand / Collapse  Quote originally posted by csurfer ...
Whats with return getchar() ? You got several other ways to pause the output.Use one of them rather than this because even though you return some random character as the return of main and exit the OS thinks that the process didn't terminate properly because of a non zero return value.Which of course doesn't cause any problem here but would definitely cause great problems in future when you write some important code.
So better correct it now.
well for getchar() coz sometimes i use dev when testing a program its a habbit of me to put return getchar(); rather than writing small thing since its a program made for learning
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Jul 17th, 2009
0

Re: Problem in understandting Multi dimensional array and nested loops

Click to Expand / Collapse  Quote originally posted by MrNoob ...
well for getchar() coz sometimes i use dev when testing a program its a habbit of me to put return getchar(); rather than writing small thing since its a program made for learning
Too lazy to just split them up like this?
  1. getchar();
  2. return 0;
It's better to return a fixed value when your program has exited successfully because in your current program, the return value depends on what key the user has pressed, not a very good habit IMO.
If you just write it like: return getchar(); , how can you know then whether your program has exited successfully or not?
Last edited by tux4life; Jul 17th, 2009 at 9:53 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 17th, 2009
0

Re: Problem in understandting Multi dimensional array and nested loops

how can i chk if it exited successfully is there a way to chk program return value ?
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: strcat
Next Thread in C Forum Timeline: histogram in c





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


Follow us on Twitter


© 2011 DaniWeb® LLC