Array index

Jul 12, 2010 at 7:35pm
Hi my fellow members. I am having trouble outputting the index of the largest number.Ive gotten to this point but, now I'm stuck. Any help is greatly appreciated. My code is

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
int numbers=5, element;
vector<int> myvec;
int count = 0;
vector<int>:: iterator MyIterator1;
vector<int>:: const_iterator iter;

while (count != numbers)
{
count ++;
cout << "Enter numbers: ";
cin >> element;
myvec.push_back(element);
}


for(MyIterator1 = myvec.begin(); MyIterator1 < myvec.end(); ++ MyIterator1)
{
Jul 12, 2010 at 7:45pm
Create a variable to store your 'largest' and set it to zero. Then in the loop every time you find one bigger, set it to that one.
Jul 12, 2010 at 8:05pm
That is not going to work if you only pass negative numbers. I'd just initialize a variable with the first element then run a loop comparing it to the remaining elements and reassigning as needed.
Last edited on Jul 12, 2010 at 8:13pm
Topic archived. No new replies allowed.