Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search on array index

Status
Not open for further replies.

LBryant777

IS-IT--Management
Mar 24, 2004
23
US
I had a directive to create an array, find the largest value, then find the index number of the largest value in the array. I have most of it written, but I'm stuck calcualaing and showing the index. Here is the code so far:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{

/* declare array */

int dollars[6] = {25, 30, 50, 20, 15, 25};
int i;


int high = dollars[0]; /* assign first element value to high */
int x = 1; /* begin search with second element */
while (x < 6)

{

if (dollars[x] > high) /* compare values */

high = dollars[x]; /* assign element value to high */

x = x + 1; /* update subscript */
}
{
for(int i = 0; i < x; i++)
if (dollars[x] == high) return i;

}


/* display highest value */

cout << "High: " << high;

cout << "Index Number: " << dollars;


}

It compiles successfully, but then I get an error in Visual C++ upon running. The output is currently High: 50; it should also show Index Number: 3.

Could someone give me some assistance? Thanks!!
 
Please use the code tags when posting code.

> int high = dollars[0];
So also do
int indexOfHigh = 0;

And when you do
> high = dollars[x];

Also do
indexOfHigh = x;

The rest should be easy.



--
 
I forgot about the code tags - my error.

I incorporated your suggestions and it worked. The current code:

Code:
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{

/* declare array */

  int dollars[6] = {25, 30, 50, 20, 15, 25};

  int high = dollars[0]; /* assign first element value to high */
  int indexOfHigh = 0;
  int x = 1;  /* begin search with second element */
  while (x < 6)

  {

   if (dollars[x] > high)  /* compare values */

	high = dollars[x];  /* assign element value to high */
	indexOfHigh = x;

    x = x + 1;   /* update subscript */
  }
 

  /* display highest value */

  cout << "High: " << high;

  cout << "Index Number: " << indexOfHigh;


}

My only other questions is that the array is sorted before the high number and the index is returned. I tried to research and couldn't really find this, but is there a way for the array not to be sorted for the search and that the index number to be shown as the original position in the array? For ex., the number 50 above would be considered in the index position 2 in the array. Thanks for your help so far...
 
I'm not sure what you mean?
{25, 30, 50, 20, 15, 25} doesn't look sorted to me.
 
I'm sorry I didn't make myself clear. Although the array isn't sorted in the code, the output from the program code listed above shows the following:

High: 50
Index Number: 5

Which caused me to believe (also found in my reading) that the numbers in the array are sorted to find the high number and the index number is outputted after the array is sorted. After researching some more, I found I needed to do a linear search of the array. My finished code:

Code:
#include <iostream>
using namespace std;

int LinearSearch(const int *Array, const int Size, const int ValToSearch)
{
	bool NotFound = true;
	int i = 0;

	while(i < Size && NotFound)
	{
		if(ValToSearch != Array[i])
			i++;
		else
			NotFound = false;
	}

	if( NotFound == false )
		return i;
	else
		return -1;
}

int main()
{
	int dollars[6] = {25, 30, 50, 20, 15, 25};
	int IndNum = sizeof(dollars) / sizeof(int);
	int high = dollars[0];

  int x = 1;  
  while (x < 6)

  {

   if (dollars[x] > high)

	high = dollars[x]; 
	IndNum = x;

    x = x + 1; 
  }
  int i = LinearSearch(dollars, IndNum, high);

		cout << high << " is at the " << i+1;
		if( i == 0 )
			cout<< "st position of the array\n\n";
		else if( i == 1 )
			cout<< "nd position of the array\n\n";
		else if( i == 2 )
			cout<< "rd position of the array\n\n";
		else
			cout<< "th position of the array\n\n";

	return 0;
}

Thanks for your help!
 
Shouldn't
Code:
if (dollars[x] > high)

   high = dollars[x]; 
   IndNum = x;
be:
Code:
if (dollars[x] > high)
{
   high = dollars[x]; 
   IndNum = x;
}
I'm also a little puzzled about why you need the LinearSearch() function?
 
cpjust,

Code:
if (dollars[x] > high)
{
   high = dollars[x];
   IndNum = x;
}

gives me the position number of 0 in the array. The LinearSearch() function searches for the variable 'high' to see where it is in the array. Once it finds it, it stops looping through the array and returns the position number. If there is a simplified way to do this, I'm very open to it. Thanks!!
 
First of all,

Code:
int IndNum = sizeof(dollars) / sizeof(int);

should be

Code:
int IndNum = 0;

to specify the initial position of possible highest value. Then you just do the loop:

Code:
int x = 1;  
while (x < (sizeof(dollars) / sizeof(dollars[0])))
{
   if (dollars[x] > high)
   {
      high = dollars[x]; 
      IndNum = x;
   }
   x = x + 1; 
}

after this, IndNum WILL contain the position of the highest member in the array.

------------------
When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top