Thread: Help! Programming Problem

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  2. #17
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    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.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    Code:
    sometype arrayname[ somesize1 ][ somesize2 ] = {0}; //initialize all to zero
    Then, say you want to initialize them so each one has an incremented number...
    Code:
    z = 0;
    for( y = 0; y < somesize1; y++ )
        for( x = 0; x < somesize2; x++ )
            arrayname[ y ][ x ] = z++;
    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.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    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.

  5. #20
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    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 :
    PHP Code:
    #include "stdafx.h"
    #include <stdio.h>
    int findMax(int[], int ); /* prototype*/
    int main()
    {
        
    int nums [3][3]={{3610},
                         {
    2211},
                         {
    4811}};
    printf("The maximum value is %d "findMax(*nums200));
    return 
    0;
    }
    int findMax(int vals[], int numEls/*find the maximum value*/
    {
        
    int i, *numsmax vals [0];
        for (
    i=13; ++i, ++ vals);
        if (
    max,vals[i])
           
    max vals[i];
           
        return(
    max);

    I'm getting this message
    PHP Code:
    The program '[1328] aaa.exe: Native' has exited with code 0 (0x0). 

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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);
    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.

    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:
    Code:
        for (i=0; i < numEls; ++i, ++ vals)
    Furthermore, you still keep putting ; at the end of your loops. Stop. Also, arrays start at zero not one.
    Code:
        for (i=1; i < 3; ++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:
    findMax( nums[ NUMBEROFTHEROWYOUWANTTOPASS ], LENGTHOFTHEROWYOUAREPASSING )
    Such as:
    Code:
    findMax( nums[ 0 ], 3 )
    If you're just passing a single dimension array, you can simply do:
    Code:
    function( int foo[], size_t s ) { ... }
    
    int array[ 5 ];
    
    function( array, 5 );
    Like so.

    Quzah.
    Last edited by quzah; 07-23-2005 at 07:01 PM.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    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.
    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:
    So if I want it to find the maximum value out of all the rows, can I do that??

    I'll try everything you guys have said and let you know.
    Last edited by flicka; 07-23-2005 at 07:45 PM.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by flicka
    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??

    I'll try everything you guys have said and let you know.
    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.

    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.

  10. #25
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    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.

  11. #26
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    I've tried out this:

    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 output is:
    The maximum value is 199

    Hope it's helpful to you.

  12. #27
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Sofaraway,
    Thanks
    It's going to be very helpfull.

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    *(vals + i)
    is the same as
    Code:
    vals[i]
    and
    Code:
    *vals
    is the same as
    Code:
    vals[0]
    Oh, and you don't need m. Just use i*20+j.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM