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:
#include <iostream>
#include <stdlib.h>
usingnamespace 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;
}
#include <iostream>
#include <stdlib.h>
usingnamespace 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;
}
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:
#include <iostream>
#include <stdlib.h>
usingnamespace 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;
}
#include <iostream>
#include <stdlib.h>
usingnamespace 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;
}
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...