/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>
usingnamespace std;
int main()
{
int counter,number,largest,large2;
counter = 0;
largest = 0;
number = 0;
large2 = 0;
while ( counter <= 5 )
{
cout << "Enter a number: ";
cin >> number;
if ( number > largest )
{
largest = number;
}
if ( number > large2 )
{
large2 = number;
}
counter++;
}
cout << endl;
cout << "The largest numbers is: " << largest << " and " << large2;
return 0;
}
Ok you have tested the input to see if it is larger than the largest number - good.
So test to see what number (less than largest) is the large2.
First test - number must be less than largest.
Second test - is number greater than large2, if number is larger than large2 then large2 must be equil to number.
That should solve it.
Oh just one other thing. If you want to input 10 numbers then you need to change this or else you will get 11 numbers while (counter < 10)
There is only a need for one test. The rest is just assignment. The incredibly tricky thing seems to be which variable to assign the value of the second largest value to. We don't have a lot of variables, it shouldn't be that hard to work out which one .........
how can you see more than the 5 posts they have on their profile?
1 2 3 4
if (number > largest)
int temp = largest; //store it so we can use it
largest = number; //if we hadn't stored it in tem, we wouldn't know what the number of the int largest was
number = temp; //and without knowing that, we wouldn't be able to set number to what largest was, inother words we are swaping these two numbers
there was an article about how there are 2 groups of programming studnts, those that failed to graps theconcept of how variables worked would rarely pass, whereas the opposite was true for those that could.
sorry about spelling, not at my pc, and pretend those lines of code are inside the if statement block,even without curly brackets
int largest1 = 0, largest2 = 0;
// Do some lines here
// Loop to find the largest 2 values
for (int i = 0; i < arraySize; i ++)
// Number was larger than largest1
if (myArray[i] > largest1) {
// largest2 should get the old largest value
largest2 = largest1;
// largest1 should now get the new largest value
largest1 = myArray[i];
}
// Number was equal to largest1 or less than
// But greater than largest 2
elseif (myArray[i] > largest2)
largest2 = myArray[i];
largest1 will contain the largest value while largest2 will contain the second largest value.
#include <iostream>
int main() {
int largest1 = 0, largest2 = 0;
// Create a constant max size for the array
constint arraySize = 20;
// Create our array
int myArray[arraySize];
// Loop to find the largest 2 values
for (int i = 0; i < arraySize; i ++) {
// Prompt user
std::cout << "Please enter number " << (i + 1) << ": ";
// Wait for input
std::cin >> myArray[i];
// Number was larger than largest1
if (myArray[i] > largest1) {
// largest2 should get the old largest value
largest2 = largest1;
// largest1 should now get the new largest value
largest1 = myArray[i];
}
// Number was equal to largest1 or less than
// But greater than largest 2
elseif (myArray[i] > largest2)
largest2 = myArray[i];
}
return 0;
}
#include <iostream>
int main() {
int largest1 = 0, largest2 = 0;
int number, maxNum;
std::cout << "How many numbers would you like to enter? ";
// Assume the user is going to enter an integer greater than 0
std::cin >> maxNum;
// Loop to find the largest 2 values
for (int i = 0; i < maxNum; i ++) {
// Prompt user
std::cout << "Please enter number " << (i + 1) << ": ";
// Wait for input
std::cin >> number;
// Number was larger than largest1
if (number > largest1) {
// largest2 should get the old largest value
largest2 = largest1;
// largest1 should now get the new largest value
largest1 = number;
}
// Number was equal to largest1 or less than
// But greater than largest 2
elseif (number > largest2)
largest2 = number;
}
return 0;
}
There is two completely working examples, one using an array, and the other using a basic variable.
how can you see more than the 5 posts they have on their profile?
Go backwards through the pages on the beginners forum until you find a thread written by the person concerned.
If you read the responses , you will see that there is a lot of vague requests for more info, but with almost no effort provided by the OP to help themselves. Look at this thread, and read all the hopeless replies.
This poster is a SERIAL TROLL, and I have reported it to admin as such. I have learnt my lesson, don't waste your effort on these oxygen thieves. There you go, I have given him what he wants (assuming male gender)
The trouble is, they just create a new user name - I have suspicions about 5 or so other users.
Maybe admin can go troll hunting and find all the users with same email address, although they just get another email - it would be a start