#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
/* Sample Output:
input three different integers: 5 10 15
*/
int number1;
int number2;
int number3;
int smallest;
int largest;
cout << "enter 5" << endl;
cin >> number1;
cout << "enter value 10" << endl;
cin >> number2;
cout << "enter value 15" << endl;
cin >> number3;
largest = number1;
//Write a statement to determine if number2 is greater than largest
if (number2 > largest)
{
//number2 = largest;
largest = number2;
cout << "the number 2 value is the largest with a value of " << number2 << endl;
}
//Write a statement to determine if number3 is greater than largest
elseif (number3 > 0) {
largest = number3;
cout << "the number 3 value is the largest with a value of " << number3 << endl;
}
else {
cout << "the number 1 value is the largest with a value of " << number1 << endl;
}
return 0;
}
Correct, I am comparing number3 with largest.
to test to see if that statement was being read all, I also used " if number3 > 0" to force a output and I got nothing. This is leading me to believe that line of code below is not being evaluated at all...
I'm running Microsoft visual compile, not sure if that would make a difference
thanks again
Farrris
if (number3 > largest) {
(largest = number3);
cout << "the number 3 value is the largest with a value of " << number3 << endl;
}
#include <iostream>
int main()
{
// create a constant variable to avoid using "magic numbers"
constint MAX_SIZE { 3 };
// initialize a simple array to contain 3 values
// let's mix up the values
int numbers[MAX_SIZE] { 10, 5, 15 };
// initialize smallest to 10,000
// (10,000 is an arbitrary placeholder for comparisons, actual numbers will be smaller)
int smallest { 10000 };
// set largest to zero
int largest { };
for (int i { }; i < MAX_SIZE; i++)
{
// check to see if array value is is smaller than current smallest
// if it is assign it to smallest
if (numbers[i] < smallest)
{
smallest = numbers[i];
}
// check to see if array value is is larger than current largest
// if it is assign it to largest
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
std::cout << "Largest: " << largest << '\n' << "Smallest: " << smallest << '\n';
}
Largest: 15
Smallest: 5
Now you can change how many numbers you check for largest and smallest, with no need to change the core logic for finding them.
The original code starts by assuming number1 is largest.
Then it checks to see if number2 is larger than that. If number2 is larger, the program ENDS. It NEVER CHECK number3. number3 could be a million, wouldn't matter, because the original code STOPS CHECKING if number2 is larger than number1.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
/* Sample Output:
input three different integers: 5 10 15
*/
int number1;
int number2;
int number3;
//int smallest; // <--- Not used yet. Produces a warning.
int largest;
cout << "enter 5: ";
cin >> number1;
cout << "enter value 10: ";
cin >> number2;
cout << "enter value 15: ";
cin >> number3;
largest = number1;
//Write a statement to determine if number2 is greater than largest
if (number3 > largest)
{
largest = number3;
cout << "the number 3 value is the largest with a value of " << number3 << endl;
}
elseif (number2 > largest)
{
//number2 = largest;
largest = number2;
cout << "the number 2 value is the largest with a value of " << number2 << endl;
}
else
{
cout << "the number 1 value is the largest with a value of " << number1 << endl;
}
return 0;
}
You can use the gear icon to the right of the code block to check it out. It is now in the middle of the block.
Furry Guy,
Thank you, so much for the container code, it's been over ten years since i picked up c++ so i'm having to relearn how to code and this definitely helps.
Repeater,
So basically i'm working problems from a old lab book to get up to speed. For the life of me, I can't understand why the code stop checking at "number2" when the next line of code is a else if statement...go figure.
thanks for the assist
Instead of using a regular array I might use a std::vector so the number of values to be compared is not restricted to a certain number at compile time.
it's been over ten years since i picked up c++
C++ underwent a HUGE change and improvement with C++11. Having to relearn with C++11/14/17 makes it close to be a totally different programming language.
Maybe plowing through an online tutorial might be of benefit. There is one here at CPlusPlus, though it is a bit outdated. C++11 for the most part.
#include <Windows.h>
#include <iostream>
#include <vector>
usingnamespace std;
int find_largest(int n1, int n2, int n3, int &count) {
if (n1 > n2 && n1 > n3) { count = 1; return n1; }
elseif (n2 > n1 && n2 > n3) { count = 2; return n2; }
else { count = 3; return n3; }
}
int main()
{
/* Sample Output:
input three different integers: 5 10 15
*/
int number1;
int number2;
int number3;
int smallest;
int largest;
int count;
cout << "enter 5" << endl;
cin >> number1;
cout << "enter value 10" << endl;
cin >> number2;
cout << "enter value 15" << endl;
cin >> number3;
largest = find_largest(number1,number2,number3,count);
cout << "the number "<< count << " value is the largest with a value of " << largest << endl;
return 0;
}
I can't understand why the code stop checking at "number2" when the next line of code is a else if statement
Because that's what else means! An else block (or an else if block) will only evaluate if the previous if conditions aren't true. If number2 > largest then the block from lines 36 - 41 will be executed, and lines 44 - 53 will not be executed.
If number2 > largest is not true, only then will lines 44 - 53 be executed.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
/* Sample Output:
input three different integers: 5 10 15
*/
int number1;
int number2;
int number3;
int smallest;
int largest;
cout << "enter 5" << endl;
cin >> number1;
cout << "enter value 10" << endl;
cin >> number2;
cout << "enter value 15" << endl;
cin >> number3;
largest = number1;
//Write a statement to determine if number2 is greater than largest
if (number2 > largest && number2 > number3)
{
//number2 = largest;
largest = number2;
cout << "the number 2 value is the largest with a value of " << number2 << endl;
}
//Write a statement to determine if number3 is greater than largest
elseif (number3 > largest && number3 > number2) {
largest = number3;
cout << "the number 3 value is the largest with a value of " << number3 << endl;
}
else {
cout << "the number 1 value is the largest with a value of " << number1 << endl;
}
return 0;
But watch out if number2 is equal to number3 the code fails...