Least but Best
This morning, we had a laboratory exercise in my algorithm’s class. There are two (2) sets of problems, and we must draw the flowchart in Visio, and code it in C++. The first problem is tricky, while the second one is very easy (so I won’t mention it here). Here’s what the problem says:
Make the user input 3 integers, and determine which has the least value.
Ok, this sounds very easy. Come to think of it, it is. Well, actually it asks for the variable with the least integer value. So a simple if-else statement won’t do. By the way, my flowchart is already done, and it was so messy. There are 14 pairs of On-Page Connectors (makes it 28 ‘A’-'N’ circles), good thing I didn’t upload it so you can’t see how bad it is. Heh.
Anyway, on a normal if-else statement, its not hard. Here’s what it should look like:
#include <iostream>
template <class T>
void rcin(T &input) {
std::cin >> input;
std::cin.ignore();
}
int main () {
int a, b, c;
std::cout << std::endl
<< "\tEnter 3 numbers:" << std::endl
<< std::endl
<< "\t\tA) "; rcin(a);
std::cout << "\t\tB) "; rcin(b);
std::cout << "\t\tC) "; rcin(c);
std::cout << std::endl;
if (a == b && b == c) {
std::cout << "\tAll numbers are equal.";
} else if (a == b) {
if (c < a) {
std::cout << "\tC has the integer with the least value: " << c;
} else {
std::cout << "\tA and B has the integer with the least value: " << a;
}
} else if (b == c) {
if (a < b) {
std::cout << "\tA has the integer with the least value: " << a;
} else {
std::cout << "\tB and C has the integer with the least value: " << b;
}
} else if (a == c) {
if (b < a) {
std::cout << "\tB has the integer with the least value: " << b;
} else {
std::cout << "\tA and C has the integer with the least value: " << a;
}
} else if (a < b) {
if (c < a) {
std::cout << "\tC has the integer with the least value: " << c;
} else {
std::cout << "\tA has the integer with the least value: " << a;
}
} else if (b < c) {
std::cout << "\tB has the integer with the least value: " << b;
} else {
std::cout << "\tC has the integer with the least value: " << c;
}
std::cin.get();
return 0;
}
NOTE: Nevermind my rcin() function for now, I’ll discuss that on my future post.
So yeah, it’s bad for the eyes. But it works fine, though, the answer is simpler and easier to understand. I just need to pull things out of my head, and not focus on the flowchart. We had this rule of Resist the temptation to code. And to be honest, I can’t follow it. My mind works better if I’m in front of an IDE or editor. Anyway here’s my solution:
#include <iostream>
#define SIZE 3
template <class T>
void rcin(T &input) {
std::cin >> input;
std::cin.ignore();
}
int main () {
int nums[SIZE], smallest, var;
std::cout << std::endl
<< "\tEnter 3 numbers:" << std::endl
<< std::endl;
for (int i = 0; i < SIZE; i++) {
std::cout << "\t\t" << i + 1 << ") ";
rcin(nums[i]);
smallest = (i == 0) ? nums[i] : (nums[i] < smallest) ? nums[i] : smallest;
}
std::cout << std::endl
<< "\tThe smallest value is " << smallest << std::endl;
std::cin.get();
return 0;
}
I used nested shorthand if statements to make it simpler. And about the function, nevermind it and use std::cin instead. It was just my practice.
Anyway, that’s how simple it is. And from that, I will create a flowchart. Heh. That’s how my mind works.