Thread: How do I print the row and column number in an array????

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    10

    How do I print the row and column number in an array????

    I'm looking to modify my function for a 10 x 20 array that finds the max value in the array. I'm trying to print the row and column number of the element that is the max value but I always get 10 and 20 no matter where the max value is. Can someone help me figure out why or point me to another thread. I searched but couldn't find anything pertaining to this topic. I modified it for a smaller array until I get it figured out.

    Here's the code:

    #include <stdio.h>
    void findmax(int[2][20];
    int main()
    {
    int numbers[2][20] = {1,4,5,8,3,2,15,227,45,87,6,92,23,21,45,85,95,187, 34,67,56,4,2,7,0,9,6,78,94,300,486,867,917,45,1,66 6,911,56,41,35};

    findmax(numbers);
    return 0;
    }

    void findmax(int num[2][20])
    {
    int i,j,max;
    for(i=0;i<2;i++)
    for (j=0;j<20;j++)
    if (num[i][j] > max)
    max = num [i][j];
    printf("\nThe maximum array value is %d\n",max);
    return;
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Use code tags.

    All you need to do is have ti store the row and column of the maximum value when you find it.
    Code:
    max = num [i][j];
    maxrow = i;
    maxcol = j;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    all you'd have to do is after your first printf statement make another that uses i and j.
    Code:
    printf("Max found in array element [%d][%d]",i+1,j+1);

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Originally posted by Draco
    all you'd have to do is after your first printf statement make another that uses i and j.
    Code:
    printf("Max found in array element [%d][%d]",i+1,j+1);
    That wouldn't work, because i and j are out of scope by then.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I don't see why he couldn't put that statement in the findmax function

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Here's the code indented properly:
    Code:
    #include <stdio.h>
    void findmax(int[2][20];
    int main()
    {
      int numbers[2][20] = {1,4,5,8,3,2,15,227,45,87,6,92,23,21,45,85,95,187,
    			34,67,56,4,2,7,0,9,6,78,94,300,486,867,917,45,1,66
    			6,911,56,41,35};
    
      findmax(numbers);
      return 0;
    }
    
    void findmax(int num[2][20])
    {
      int i,j,max;
      for(i=0;i<2;i++)
        for (j=0;j<20;j++)
          if (num[i][j] > max)
    	max = num [i][j];
      printf("\nThe maximum array value is %d\n",max);
      return;
    }
    Now do you see why it wouldn't work?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    yeah. It's confusing when they don't put in loop braces....I didn't even know you could do that.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If you don't use braces, it will only include the next line of code in the loop/if statement/whatever.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I know that..but I thought for loops needed them.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Nope.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    Thanks for the help Xsquared.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. some help needed
    By juststartedC in forum C Programming
    Replies: 2
    Last Post: 10-22-2007, 11:28 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Comment this Program
    By kishorepalle in forum C Programming
    Replies: 11
    Last Post: 10-05-2004, 06:41 AM
  5. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM