How would I print the largest number in an array and then move it to the end of the sequence?

I have written some code that asks the user how many numbers they want to input. The program then prints that number of statements asking for a number in each new line and then prints out the array at the end. How would I print the largest number and then on new line print the array sequence with that number at the end? Something like this for example:

How many numbers would you like to enter? 6

Please enter number 1: 10

Please enter number 2: 8

Please enter number 3: 3

Please enter number 4: 5

Please enter number 5: 2

Please enter number 6: 9

The largest number is: 10

New List: 8, 3, 5, 2, 9, 10

Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <iostream>
#include <stdlib.h>

using namespace std;
int main ()
{
    int i, count = 0;
    int numArray[4000];

    cout << "How many numbers would you like to enter? : ";
    cin >> count;

    for (i = 0; i < count; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i];
    }


    //cout << "The largest number is: " << largestNumber << endl;
    cout << "New list: ";
    for (i=0; i< count; i++)
    {
        cout << numArray[i];
        if (i < count - 1)
            cout << ", ";
    }
    cout << endl;
    return 0;
}
Hi,
Try this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <stdlib.h>

using namespace std;
int main ()
{
    int i, count = 0;
    int numArray[4000];
    int largest = 0, largest_idx = 0;
    cout << "How many numbers would you like to enter? : ";
    cin >> count;
    for (i = 0; i < count; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i];
    }

    if(count > 0)
    largest = numArray[0];
    for (i = 1; i < count; i++)
    if(numArray[i] > largest)
    {
        largest = numArray[i]; 
        largest_idx = i;
    }

    int t = numArray[count - 1];
    numArray[count - 1] = largest;
    numArray[largest_idx] = t;

    cout << "The largest number is: " << largest << endl;
    cout << "New list: ";
    for (i=0; i< count; i++)
    {
        cout << numArray[i];
        if (i < count - 1)
            cout << ", ";
    }
    cout << endl;
    return 0;
}
Does that help you? :)
We could pretend we were using C++ and that it had facilities to aid us...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> numbers;

    std::cout << "Enter numbers (non-number to stop).\n";
    int num;
    while (std::cin >> num) numbers.push_back(num);

    if (numbers.empty()) {
        std::cout << "Error: empty sequence.\n";
        return 0;
    }

    std::swap(*std::max_element(numbers.begin(), numbers.end()), numbers.back());

    for (auto n : numbers)
            std::cout << n << ' ';
    std::cout << '\n';
}
That worked! Thanks :)
Glad it helped :)
@closed account 5a8Ym39o6 One more thing. How would I find the min and average?
So I would print the min, max, and average of the numbers in the array that the user entered. Here is the code again that I have that you helped me with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <stdlib.h>

using namespace std;
int main ()
{
    int i, count = 0;
    int numArray[4000];

    cout << "How many numbers would you like to enter? : ";
    cin >> count;

    for (i = 0; i < count; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i];
    }

    cout << "The numbers you entered: ";
    for (i=0; i< count; i++)
    {
        cout << numArray[i];
        if (i < count - 1)
            cout << ", ";
    }
    cout << endl;
   return 0;
}
Last edited on
Hi,
Follow this example :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
    int i, count = 0;
    int numArray[4000];
    int lowest, highest;
    int total = 0, average;
    cout << "How many numbers would you like to enter? : ";
    cin >> count;
    for (i = 0; i < count; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i];
    }
    lowest = numArray[0];
    highest = numArray[0];
    for (i = 0; i < count; i++)
    {
        if(numArray[i] < lowest) lowest = numArray[i]; 
        if(numArray[i] > highest) highest = numArray[i]; 
        total += numArray[i];
    }
    average = total / count;
    cout << "The numbers you entered: ";
    for (i=0; i< count; i++)
    {
        cout << numArray[i];
        if (i < count - 1)
            cout << ", ";
    }
    cout << endl;
    cout << "The maximum number: " << highest << endl;
    cout << "The minimum number: " << lowest << endl;
    cout << "The average number: " << average << endl;

   return 0;
}
Last edited on
(Does that help you?) :)
Instead of using two loops

12
13
14
15
16
17
18
19
20
21
22
23
24
 for (i = 0; i < count; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i];
    }
    lowest = numArray[0];
    highest = numArray[0];
    for (i = 0; i < count; i++)
    {
        if(numArray[i] < lowest) lowest = numArray[i]; 
        if(numArray[i] > highest) highest = numArray[i]; 
        total += numArray[i];
    }


you can incorporate the second loop inside first and save some processor, although this wont make much difference for small loops but for a big loop this could save some time and IMO it is good coding habits...
Last edited on
@closed account 5a8Ym39o6 It says largest is not declared
I edited my solution. Go ahead and compile it.

And let us know your output outcome :)
closed account 5a8Ym39o6 Ya that works now. I see what you did, thanks again!
Glad it helped :)
Topic archived. No new replies allowed.