| | |
Problem in understandting Multi dimensional array and nested loops
Thread Solved |
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
shouldnt it produce 6 ? please someone enlighten me in understanding this ....
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
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int num[2][3]= { {1,1,1},{1,1,1} }; int i; int x; int total=0; for(i=0;i<2;i++) for(i=0;i<3;i++) { total+=num[i][x]; } printf("%d",total); return getchar(); }
You have an (i) problem.
You need to use two different variables within a nested loop, such as i and j.
You need to use two different variables within a nested loop, such as i and j.
C Syntax (Toggle Plain Text)
for(i=0;i<2;i++) // for(i=0;i<3;i++) { for(x = 0; x < 3; x++ ) { total+=num[i][x]; }
Whats with
So better correct it now.
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"....
Find the corrected code.nothing to wierd about this.try to find out values of each elemnts manuall before
writing the code.
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
writing the code.
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int num[2][3]= { {1,2,3},{4,5,6} }; int i; int x; int total=0; for(i=0;i<2;i++) [RED] for(i=0;i<3;i++)[/RED] //here i SHOULD BE x { total+=num[i][x]; } printf("%d",total); return getchar(); } int num[2][3]= { {1,2,3},{4,5,6} };
===========================
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.
•
•
•
•
Whats withreturn 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
C Syntax (Toggle Plain Text)
getchar(); return 0;
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."
![]() |
Similar Threads
- Multi-dimensional array help (C++)
- Constructing a multi-dimensional array using a dilimited string input. (PHP)
- pointers and multi-dimensional arrays (C++)
- Problem with multiDimensional array? (C)
- Passing multi-dimensional array (C)
- multi dimensional array search xml parser (PHP)
- Need help passing a multi-dimensional array (C++)
Other Threads in the C Forum
- Previous Thread: strcat
- Next Thread: histogram in c
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






