hello i need help, i have to input 10 integers and find the maxinum of the numbers.

#include <iostream>
using namespace std;

int main()
{
    const int MAX_NUM_INTEGERS = 10;
    int integers[0]; 
    int largest = integers[0];
cout << "Enter 10 intergers: ";
cin >> integers[i]
    for (int i=0; i < MAX_NUM_INTEGERS; i++){
        if (integers[i]> largest){
        largest = integers[i];
        }
}
cout << "Largest integer = " << largest<<"\n";
system("pause");
return 0;
}

> int integers[0];
Use your constant to set the maximum number of elements in the array

> int largest = integers[0];
You can only do this after you've read in some values....

> cin >> integers
Your search loop is good.
But this too needs to be in a loop if you want to read in 10 values.

> how can i loop it so it can read 10 integers?
In pretty much the same way that you loop to compare them

Hi, I'm still pretty new to programming, but maybe this will help.

#include <iostream>

using namespace std;

int main()
{
    
    const int MAX_NUM_INTEGERS = 10;
    long int integers[10];
    long int i = 0, maximumValue = 0;

    cout << "Please enter 10 integers : \n\n";

    while ( i < MAX_NUM_INTEGERS)
    {
        cin >> integers[i];
        i++;
    
        if (integers[i - 1] > maximumValue)
        {
            maximumValue = integers[i-1];
        }
    }

    cout << "\nThe largest number you have entered is " << maximumValue << "\n\n";

    system("pause");
    return 0;
}

This seems to work for positive numbers, at least

*edit changed MAX_NUM_INTEGERS to 10

If you couldn't turn this code you had

for (int i=0; i < MAX_NUM_INTEGERS; i++){
        if (integers[i]> largest){
        largest = integers[i];
        }

Into this code you wanted

for (int i=0; i < MAX_NUM_INTEGERS; i++){
        cin >> integers[i];
        }

Then I think you need to reappraise your ambitions of being a programmer.

Is using the for loop better than using the while loop that I used? If so, why? It does the same thing. Is there a specific reason to use a for loop in this situation? Should I have put i++ after the if statement?

A good way of finding the highest of X number of integers is to use vectors, and to use the sort algorithm. They are both from the Standard Template Library STL. I'll give you an example of how to use it, but you will need to modify loops and such for your use.

This is a function that returns the value of the highest of 10 integers.

#include <vector>     // for vectors
#include <algorithm>  // for sort
#include <iostream>

using namespace std;

int max_of_ten_ints()
{
  int x;
  vector<int> myVector;

  for (int i = 0; i < 10; i++)  // This loop gets 10 integers from the user
  {
    cin >> x;
    myVector.push_back(x);     // Puts x in a new vector element
  }

  sort(myVector.begin(), myVector.end());

  return (myVector[9]); // Returns the 10th element of the sorted list.
}
This article has been dead for over six months. Start a new discussion instead.