Okay, it's not even a program that I need to write, it's just the If statement.
This is the problem: Write an If structure that outputs the least of three values, count1, count2, and count3. If more than one variable has the same lowest value, then output the value as many times as it is found in those variables.
I know that this if statement will work when outputting the lowest variable but, it's the second part of the problem I am confused with. I know I'm probably going to need || or some other logical operator.
Your professor has taught you the "shorthand" way:
usingnamespace std;
If you DON'T do the above (there are reasons not to, but they aren't important for an introductory course), then you have to specify the namespace. cout and endl are declared in the std namespace, so if I don't do the above, I have to write "std::cout" and 'std::endl" instead.
Okay so now this is the if statement I have for my problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if (count1 < count2 && count1 < count3)
cout << "The lowest value is " << count1 << endl;
elseif (count2 < count1 && count2 < count3)
cout << "The lowest value is " << count2 << endl;
elseif (count3 < count1 && count3 < count2)
cout << "The lowest value is " << count3 << endl;
elseif (count1 == count2 && count2 == count3)
cout << “The lowest value is ” << count1 << count2 << count3 << endl;
elseif (count1 == count2)
cout << “The lowest value is ” << count1 << count2 << endl;
elseif (count2 == count3)
cout << “The lowest value is ” << count2 << count3 << endl;
elseif (count1 == count3)
cout << “The lowest value is ” << count1 << count3 << endl;
It seems pretty long, and I'm probably doing something wrong, but this will print out the lowest value and if the user puts in two of the same lowest value, it will print both of them out then. Right?
For more information, please study namespaces. They aren't anything like classes or structs. It simply places objects, functions, and everything else into a categorry which can be accessed using the <namespace>:: syntax.
Yea it does seem long but it kinda needs to be. An easier more english like way of doing this is creating the variable 'int smallest' which was the exact same question someone else asked today/yesterday only he didn't have to do if 2 numbers were the same.
While your code works it actually prints more than one statement. So what you need to do is have breaks throughout your statements to exit once the correct formula is found.
I will show you how only because you say it's not part a program you have to write. I actually did the same when I first started learning I actually got the exact same example of enter 3 numbers and determine the smallest, I tried to expand this to show numbers that are the same as well. Took me ages, and I think I eventually gave up after a few hours. :P
I just included the functions I'm using rather than the whole std namespace as you can see below in lines 3 and 4.
include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int num1, num2, num3, smallest;
cin >> num1 >> num2 >> num3;
// initialize smallest variable it will be used later.
smallest = num1;
// I guess we could start with the first statement if all equal
if ((num1==num2) &&(num1==num3)
{
cout << "all numbers are equal";
break; // include break so we don't get multiple prints to screen
}
elseif ((num1==num2) && (num1<num3))
{
cout << "the smallest 2 numbers are: " << num1 << endl;
break;
}
elseif ((num1==num3) && (num1<num2))
{
cout << "The smallest 2 numbers are: " << num1 << endl;
break;
}
elseif ((num2==num3) && (num1>num2))
{
cout << "The smallest 2 numbers are: " << num2 << endl;
break;
}
// now we just need to check the smallest if only 1 exists.
//using the smallest varable which is currently the value of num1.
elseif ((num1 < num2) && (num1<num3))
{
cout << "Smallest is: " << num1;
break;
}
elseif ((num2 < num1) && (num2<num3))
{
cout << "Smallest is: " <<num2;
break;
}
else
cout << "smallest is: " << num3;
return 0;
}
Edit: Ok i realized halfway through writting that code that smallest variable is pretty useless in this scenario :P Had to retype last little bit :-)
To show what I mean though, I was going to include a temp variable "smallest"
I guess this code could replace 39-50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
else
{
smallest = num1;
if (num2< smallest)
{
smallest = num2;
}
if (num3< smallest)
{
smallest = num3;
}
cout << "Smallest is: " << smallest;
}
ah, you're is very clean, haha. Thank you for doing that though. I understand how it's being broken down now; however, one question I have.
The problem states: Write an If structure that outputs the least of three values, count1, count2, and count3. If more than one variable has the same lowest value, then output the value as many times as it is found in those variables.
So, in your example, it's not printing out the value of the smallest twice. So would mine work correctly according to the problem?
well in my code I'm simply stating the smallest 2 numbers are ...
you can adjust the cout statements as you need. But that's how I interpreted the problem, seems printing out the numbers twice is redundant. Lets say num1 and num2 are both 45
Yeah, the whole printing out the numbers twice is quite ridiculous, but heaven knows that if I do not do that there goes my homework grade, haha. Yeah, I understood your program, it's a lot cleaner and less redundant than what the homework wants, but thank you so much for helping me!
in that case you might want to check line 17, I haven't compiled this code but I can see a bracket missing on that line, and revising over my code I don't think breaks are neccessary I simply included them just in case. In your case however:
1 2 3 4 5 6
elseif (count1 == count2)
cout << “The lowest value is ” << count1 << count2 << endl;
elseif (count2 == count3)
cout << “The lowest value is ” << count2 << count3 << endl;
elseif (count1 == count3)
cout << “The lowest value is ” << count1 << count3 << endl;
if all 3 numbers were the same then this would print out 3 times.
Once you know that, for example, count2 is lowest, do the following:
1) output count2;
2) if count1 equals count2, output count1;
3) if count3 equals count2 output count3;
Repeat in a similar fashion in the cases where count1 is lowest and count3 is lowest.