Thread: Help! Programming Problem

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    45

    Help! Programming Problem

    I'm learning how to program with C and I have a problem that I have never had before and don't know how to solve. I hope someone will be able to help me out.
    I did this program:
    PHP Code:
     #include <stdio.h>
    int findMax(int[], int ); /* prototype*/
    int main()
    {
    int nums [10][20], ij;
    for (
    i=0200; ++i);
    for (
    j=0200; ++j);
    nums[i][j] = 2*i
    printf("The maximum value is %d \nin row $i column $j"findMax(*nums200), ij);
    return 
    0;
    }
    int findMax(int vals[], int numEls/*find the maximum value*/
    {
        
    int i, *numsmax vals [0];
        for (
    i=1200; ++i);
        if (
    max,vals[i])
           
    max vals[i];
           
        return(
    max);

    When I run debug on my complier it gives me the following message that I have never encountered before:
    PHP Code:
    Windows has triggered a breakpoint in Assignment 4A-2-a.exe.

    This may be due to a corruption of the heap, and indicates a bug in Assignment 4A-2-a.exe or any of the DLLs it has loaded.

    The output window may have more diagnostic information 
    I understand that I must have something not completely right but can't figure out what? COuld you guys help me out.
    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int nums [10][20], i, j;
        for (i=0; i < 200; ++i);
            for (j=0; i < 200; ++j);
                nums[i][j] = 2*i;
    Um hello? You just ran merrily off the end of your array. Why do you go to 200 when your
    valid indexes are 0 - 9 and 0 - 19?


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

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Quzah, the example I based my program on was saying:
    PHP Code:
    for (1i<numEls; ++i, ++vals
    So I assumed numEls was the number of elements. In a 10-by-20 I counted 200.

    But I'll try with what you gave me.

    Thanks.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    And what is this?

    Code:
    printf("The maximum value is %d \nin row $i column $j", findMax(*nums, 200), i, j);
    The format string for the call to printf() specifies one argument, yet you are passing three.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, not to mention this:
    Code:
    for (i=0; i < 200; ++i);
    for (j=0; i < 200; ++j); 
    Which really means when you do the next line:
    Code:
     nums[i][j] = 2*i;
    You're actually doing this:
    Code:
     nums[200][200] = 2*i;



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

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Kermit,

    I want the result to display the following answer:

    The maximum value is .... in row .... column ....

    So it gives me in which row and column the answer is.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, but wait, there's more! I actually just stopped at the first error. Like your compiler. But looking further...
    Code:
     for (i=1; i < 200; ++i);
        if (max,vals[i])
    Which really gives you this:
    Code:
    if( max )
    Of course, that's after it tries to access vals[200]...


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

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by quzah
    Oh, but wait, there's more! I actually just stopped at the first error. Like your compiler. But looking further...
    Code:
     for (i=1; i < 200; ++i);
        if (max,vals[i])
    Which really gives you this:
    Code:
    if( max )
    Of course, that's after it tries to access vals[200]...


    Quzah.
    IIRC you use gcc, right? Mine says this

    Code:
    find_max.c:16: warning: left-hand operand of comma expression has no effect
    I was thinking this meant that it used vals[i] and not max?

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by flicka
    Kermit,

    I want the result to display the following answer:

    The maximum value is .... in row .... column ....

    So it gives me in which row and column the answer is.
    Does it? That's funny - not on my machine...

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Quzah,

    Ok I get the picture but even if I change 200 and put:
    int nums [10][20], i, j;
    for (i=0; i < 9; ++i);
    for (j=0; i < 19; ++j);
    nums[i][j] = 2*i;
    I still get the same "break" message on the compiler.

    doesn't
    nums[i][j] = 2*i;
    mean assigne the value 2*i to the number in row 1, column j?

    I'm starting to get more lost now than before...

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Kermit,

    I'm using Microsoft Visual C++.
    This is what I get:

    HEAP[Assignment 4A-2-a.exe]: HEAP: Free Heap block 132f78 modified at 133ad0 after it was freed
    Windows has triggered a breakpoint in Assignment 4A-2-a.exe.

    This may be due to a corruption of the heap, and indicates a bug in Assignment 4A-2-a.exe or any of the DLLs it has loaded.

    The output window may have more diagnostic information
    The program '[832] Assignment 4A-2-a.exe: Native' has exited with code 0 (0x0).
    but when I click on continue it does give me a value. it is just that I have to click on continue which I wish not to have to.

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Look at where your semi colons are.

    Code:
     for (i=0; i < 9; ++i);
    for (j=0; i < 19; ++j);
    nums[i][j] = 2*i;
    if you are just starting out in C then think on what putting semi-colons at the end of the for conditional has the effect of doing. The last statement is only executed once.

    Code:
     for (i=0; i < 9; ++i)
     {
          for (j=0; i < 19; ++j)
          {
                  nums[i][j] = 2*i;
           }
     }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    bigtamscot

    Yes I'm just starting out and it'sa not always easy. I think I'm so affraid to forget the semi colons that I put some everywhere.

    Thanks for your help.

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You might want to try getting your program running with a single array, rather than a 2d one. After you get that working nicely, modify it by adding a second dimension.

    Regarding your printf() statement - probably the smallest of your problems, however I was just making you aware of it. You might want to try something like:

    Code:
        printf("The maximum value is %d in row %d column %d\n", findMax(*nums, 200), i, j);
    Last edited by kermit; 07-23-2005 at 07:13 AM.

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Kermit,

    I did a similar program with a single array, but since it was a simple array I defined the array with numbers:

    PHP Code:
    int main() 

    int nums[5] = {21812716}; 
    printf("The maximum value is %d"findMax(nums,5)); 
    return 
    0;

    Now I am trying to pass into to a 2d array. Should I do the same thing and define all the numbers in the array?? or can I define it the way I was?

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