Thread: searching an array

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    searching an array

    I have a program that creates an array and prompts for ints to fill the array. Then my program determines how many ints were entered, what the min and max are and calculates the average.

    Now what I need to be able to do is determine if any of the values entered are higher than the average and if so, print the index number and the value stored there.

    Here is the bit of code I have so far:
    Code:
     for (i = 0; i < max_input; i++)
        {
     	if (nums[input] > avg)
     		cout << endl << "Index " << i <<  " value " << nums[input] << endl;
     		return(0);
        }
    The output I am getting now is usually "Index 0 value 323546786" or some crazy number which is not even close to any of the test ints I enter.

    Brian

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Your FOR loop should use the iterator value, 'i', as the array index.

    Ex.: nums[i]

    Whatever 'input' is, nums[input] is being displayed, not the value that you want to test from your array. Remember that built-in arrays will let you overread and overwrite their limits which is why you're picking up total garbage with nums[input].

    Try this instead:
    Code:
    for (i = 0; i < max_input; i++)
        {
     	if (nums[i] > avg)
     		cout << endl << "Index " << i <<  " value " << nums[i] << endl;
     		return(0);
        }
    Also, double-check your use of 'max_input'. It should be a counter of the number of integers entered into your array. Otherwise, you may have yet another bug in this code snippet.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Also, recheck that return(0) you have. That will quit the current function you are in, so you will only loop through 1 element.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    working now

    Thanks for the help.

    It is working now, I think both the return (0) and the i as iterator needed to be changed.

    Brian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. searching thru a c++ class array
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 05-29-2002, 09:15 PM