Thread: Doing a search

  1. #1
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question Doing a search

    I am trying to search an array that the user inputs to determine the largest number entered. The Array is entered no problem, but I am having problems with my search.

    What I have is a table format array, and I think my logic for the search will not work on a table. I am trying to use pointers for my search. perhaps it is not the easiest way, but since I am currently learning arrays and pointers I figured by using them it was the best way to actually learn them. Can anyone advise me as to what I might do to correct this? The code below is the function I am calling from the main.

    Code:
    int Table (void)
    {
    int array[4][4] = {0};
    int row;
    int col;
    int *large;
    int *end;
    int *mover;
    
    printf("Enter  4 sets of 4 integers, for a total of 16 numbers\n");
    printf("Please use the following format: nnn nnn nnn nnn <enter>.\n");
    
    /* Enter Data into Table */
    for (row = 0; row < 4; row++)
    {
    	printf("\nEnter one set of numbers: ");
    	for (col = 0; col < 4; col++)
    	{
    		scanf("%d", &array[row][col]);
    	}
    }
    
    /*Find and print largest number in the array entered*/
    end= array [4][4];
    for (large = array, mover= array +1; mover<= end; mover++)
    	if (*mover> *large)
    		end= mover;
    
    printf("The biggest number was: %d\n", *large);
    
    return 0;
    }
    As always any help, advise and guidance is greatly appreciated.

    Thanks,
    DD
    "aut vincere aut mori"

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Like this:
    Code:
    int Table (void)
    {
       int array[4][4] = {0};
       int i;
       int row;
       int col;
       int large;
       int *end;
       int *current;
    
       printf("Enter  4 sets of 4 integers, for a total of 16 numbers\n");
       printf("Please use the following format: nnn nnn nnn nnn <enter>.\n");
    
       /* Enter Data into Table */
       for (row = 0; row < 4; row++)
       {
          printf("\nEnter one set of numbers: ");
          for (col = 0; col < 4; col++)
         {
            scanf("%d", &array[row][col]);
         }
       }
    
       /*Find and print largest number in the array entered*/
       large = array[0][0];
       for(i = 0; i < 4; i++)
       {
          end = &array[i][4];
          for (current = &array[i][0]; current < end; current++)
    	      if (*current> large)
    		      large = *current;
       }
    
       printf("The biggest number was: %d\n", large);
    
       return 0;
    }

  3. #3
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Lightbulb

    Makes sense, by adding (i) I am able to take into account the other part of the table. But when I print Large, I am getting the bad data (I think the address and not the actual number). If I try to dereference (i.e. *large) instead of large I get an error while compiling. Maybe I need do dereference somewhere else, not sure here. So where did I go wrong? Ideas, thoughts, opinions?

    Thanks again for the help.
    DD
    "aut vincere aut mori"

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    If you want to use large as a pointer do this:
    Code:
       /*Find and print largest number in the array entered*/
       large = &array[0][0];
       for(i = 0; i < 4; i++)
       {
          end = &array[i][4];
          for (current = &array[i][0]; current < end; current++)
    	      if (*current > *large)
    		      large = current;
       }
    
       printf("The biggest number was: %d\n", *large);
    Notice in your code:
    large = array is incorrect because of their levels of indirection (you'll probably get a warning)
    large is type int* and array is int(*)[4]

    In your original code you initialized large but never wrote to it when you encountered a larger value.

  5. #5
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question Almost there

    OK makes sense. And thanks for pointing out that I missed writting to large the first time, I had not noticed that. Opps.

    But Even by making corrections and trying either way you suggested, I am not getting the actual number that is I entered that is largest. Instead I am getting some really off the wall number.

    Ideas?

    Thanks,
    DD
    "aut vincere aut mori"

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Just copy my code. It works.

    Or you can debug your program by watching the value of large as you step through your array. You will figure out where you went wrong.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Almost there

    >But Even by making corrections and trying either way ....
    still having troubles? I suggest posting the latest version of your function for us to have a look at.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Talking Thanks

    Got it finally. I typoed somewhere along the line, so just had to sit there and walk though each line about five times before I saw the typo. Learning is a slow and painful process, but at least I think I am learning. I do appreciate all the help eveyone out here has given me so far.

    Thanks Again,
    DD
    "aut vincere aut mori"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM