Problem in understandting Multi dimensional array and nested loops

Thread Solved

Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Problem in understandting Multi dimensional array and nested loops

 
0
  #1
Jul 16th, 2009
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 ....
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,606
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Problem in understandting Multi dimensional array and nested loops

 
0
  #2
Jul 16th, 2009
you got both loops fighting over the same index 'i'
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,606
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Problem in understandting Multi dimensional array and nested loops

 
0
  #3
Jul 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Problem in understandting Multi dimensional array and nested loops

 
-1
  #4
Jul 16th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Problem in understandting Multi dimensional array and nested loops

 
0
  #5
Jul 16th, 2009
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.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 139
Reputation: Dream2code is an unknown quantity at this point 
Solved Threads: 11
Dream2code's Avatar
Dream2code Dream2code is offline Offline
Junior Poster

Re: Problem in understandting Multi dimensional array and nested loops

 
-1
  #6
Jul 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Problem in understandting Multi dimensional array and nested loops

 
0
  #7
Jul 17th, 2009
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 ?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Problem in understandting Multi dimensional array and nested loops

 
0
  #8
Jul 17th, 2009
Originally Posted by csurfer View Post
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Problem in understandting Multi dimensional array and nested loops

 
0
  #9
Jul 17th, 2009
Originally Posted by MrNoob View Post
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Problem in understandting Multi dimensional array and nested loops

 
0
  #10
Jul 17th, 2009
how can i chk if it exited successfully is there a way to chk program return value ?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC