You can define a 2D array like this:
Code:int two_d[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
You can define a 2D array like this:
Code:int two_d[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks,
Thanks!
I was wondering if I want to do this for a bigger array like a 10-by-20 array, I will have to define all the 200 elements (10 lines and 20 columns)? Or is there an easier way to do it like a function of something?
Thanks for your help.
That was defining and intitializing an array at the same time. If you don't need to initialize it by hand, or have some other way of doing it, then you can simply do:
Then, say you want to initialize them so each one has an incremented number...Code:sometype arrayname[ somesize1 ][ somesize2 ] = {0}; //initialize all to zero
It just depends on what you want to stick in each array, and if you care if they're initialized to specific values that are easier to type them by hand at the beginning, or to have a formulae do it, or what not.Code:z = 0; for( y = 0; y < somesize1; y++ ) for( x = 0; x < somesize2; x++ ) arrayname[ y ][ x ] = z++;
Quzah.
Hope is the first step on the road to disappointment.
Quzah,
Ok I think I'm understanding the problem I had. I'll try this later, once my little munchkin is in bed otherwise I'll have problems typing anything.
I'll let you guys know how it's going.
Ok I'm not getting this at all.
I've tried using a "small" 2d array and I'm still getting a message when running debug.
The program I did as :
I'm getting this messagePHP Code:
#include "stdafx.h"
#include <stdio.h>
int findMax(int[], int ); /* prototype*/
int main()
{
int nums [3][3]={{3, 6, 10},
{2, 21, 1},
{4, 8, 11}};
printf("The maximum value is %d ", findMax(*nums, 200));
return 0;
}
int findMax(int vals[], int numEls) /*find the maximum value*/
{
int i, *nums, max = vals [0];
for (i=1; i < 3; ++i, ++ vals);
if (max,vals[i])
max = vals[i];
return(max);
}
PHP Code:
The program '[1328] aaa.exe: Native' has exited with code 0 (0x0).
Isn't it supposed to exit with code 0?
I think it has to do with how you pass the array as a parameter to findMax().
Code:#include <stdio.h> int findmax(int (*array)[3]); int main(void) { int nums[3][3] = { {2, 4, 3}, {9, 5, 6}, {7, 4, 5} }; printf("Max: %i\n", findmax(nums)); return 0; } int findmax(int (*array)[3]) { int x, y, max = 0; for(x = 0; x < 3; x ++) { for(y = 0; y < 3; y ++) { if(array[x][y] > max) max = array[x][y]; } } return max; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
What's your fascination with the number 200? Did you just pull this out of your ass or what? No. 200 = bad. Try actually passing the size of the row instead of some made up number.Code:printf("The maximum value is %d ", findMax(*nums, 200)); return 0; } int findMax(int vals[], int numEls) /*find the maximum value*/ { int i, *nums, max = vals [0]; for (i=1; i < 3; ++i, ++ vals);
Next, why aren't you using numEls? You pass it, even if it's wrong, but you don't do anything with it. Don't you pay attention to your compiler's messages? You should be doing:
Furthermore, you still keep putting ; at the end of your loops. Stop. Also, arrays start at zero not one.Code:for (i=0; i < numEls; ++i, ++ vals)
Also, your function takes a single dimension array, but you're trying to force feed it a multi-dimension array. That's wrong too. If you did want to use your two dee array, you'd do this:Code:for (i=1; i < 3; ++i, ++ vals);
Such as:Code:findMax( nums[ NUMBEROFTHEROWYOUWANTTOPASS ], LENGTHOFTHEROWYOUAREPASSING )
If you're just passing a single dimension array, you can simply do:Code:findMax( nums[ 0 ], 3 )
Like so.Code:function( int foo[], size_t s ) { ... } int array[ 5 ]; function( array, 5 );
Quzah.
Last edited by quzah; 07-23-2005 at 07:01 PM.
Hope is the first step on the road to disappointment.
dwks,
Thanks! I don't know what the result should be with a 2d array.
I only have and example with a simple array and that does give me the maximum number.
I will try this.
quzah, Well.... Sorry that I seem to be getting you upset, but you don't have to be so impolite with me. I am trying to learn and do make mistakes. the 200 is one I forgot to correct. Learning C Programming just with a book isn't the easiest for everyone. So I 'm doing my best. I do want to understand what are the mistakes I make, so thank you.
I've learned something about the findMax function.
So if I want it to find the maximum value out of all the rows, can I do that??Also, your function takes a single dimension array, but you're trying to force feed it a multi-dimension array. That's wrong too. If you did want to use your two dee array, you'd do this:
I'll try everything you guys have said and let you know.
Last edited by flicka; 07-23-2005 at 07:45 PM.
Nah. I don't usually get upset, long term anyway. That's just how I am. I knew where the 200 came from. I was trying to get you to think about what you're actually putting down. It's hard to fix a problem if you don't pay attention to what you're doing.Originally Posted by flicka
You just go through each row and each colum. Your pair of loops you started with had the right idea. You just need to make sure you have the right number for each counter's max, and that you don't have a ; on the end of them.
Quzah.
Hope is the first step on the road to disappointment.
quzah
Ok I'll pay more attention to what I'm doing.
I'll work on this tonight and see if I can get this running.
Thanks for the help and the "kick in the butt". I'll admit I'm starting to lake a little sleep lately but want to do this.
I've tried out this:
The output is:Code:#include <stdio.h> int findMax(int[], int ); /* prototype*/ int main() { int nums [10][20], i, j, m = 0; for (i=0; i < 10; ++i) for (j=0; j < 20; ++j) nums[i][j] = m++; printf("The maximum value is %d \n", findMax((int *)nums, 10 * 20) ); return 0; } int findMax(int *vals, int numEls) { /*find the maximum value*/ int i, max = *vals; for (i=0; i < numEls; ++i) if (max < *(vals + i)) max = *(vals + i); return max; }
The maximum value is 199
Hope it's helpful to you.
Sofaraway,
Thanks
It's going to be very helpfull.
is the same asCode:*(vals + i)
andCode:vals[i]
is the same asCode:*vals
Oh, and you don't need m. Just use i*20+j.Code:vals[0]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.