LBryant777
IS-IT--Management
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!!
#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!!