Compare

Well, i would like to make a program which asks the user to enter four number and i will then display the smallest number. Sadly, im stuck and i do not know how to continue. Can anyone help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iosteam.h>
#include <conio.h>

void main()
{
   clrscr()
   int a, b, c, d;
   cout << "Please enter the first number: ";
   cin >> a;
   cout << "\n Please enter the second number: ";
   cin >> b;
   cout << "\n Please enter the third number: ";
   cin >> c;
   cout << "\n Please enter the fourth number: \n";
   cin >> d;

   if (a<b)


and im stuck... XD
You should use an array to store your numbers and then a for-loop to check which one is the smallest.
uh.....how???
This explains about arrays:
http://www.cplusplus.com/doc/tutorial/arrays.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main(){
   int nums[4]; //Creates an array of 4 integers
   int min; //A value that will store the minimum number
   for(int i=0; i<4; i++){ //For-loop to prompt user to enter values
      cout << "Enter " << i << " number: ";
      cin >> nums[i]; //Read from the console a number to the array
   }
   min = nums[0]; //Set the min to the first number
   for(int i=1; i<4; i++){
      if(num[i] < min) //Check if another value is smaller than min
         min = num[i];
   }
   cout << "Minimum number: " << min << endl; //Display the min
   return 0;
}


well, im having exam but, my lecturer restricts me from using arrays because he said he havent covered that chapter.
Then the easiest way to do it (atleast for me) is when you read a variable to check if it is the minimum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main(){
   int a, b, c, d, min;//Added one more variable to hold the minimum number
   cout << "Please enter the first number: ";
   cin >> a;
   min = a;//We assign as min the first number the user enters to have something to compare
   cout << " Please enter the second number: ";
   cin >> b;
   if(b < min) min = b;//We compare each number if it is smaller than the min
                       //If it is then assign to min the new variable
   cout << " Please enter the third number: ";
   cin >> c;
   if(c < min) min = c;
   cout << " Please enter the fourth number: ";
   cin >> d;
   if(d < min) min = d;
   cout << "Minimum number is: " << min << endl; //Print out the minimum
   return 0;
}
Last edited on
Oh, well then I guess he wants you to use logical operators:
Operator ! (not): if the operand is zero, the result is non-zero, otherwise, it is zero.

Operator && (and):
0 && 0 == 0
0 && !0 == 0
!0 && 0 == 0
!0 && !0 == !0

Operator || (or):
0 || 0 == 0
0 || !0 == !0
!0 || 0 == !0
!0 || !0 == !0

Operator ^ (xor):
Note: xor behaves slightly different (a difference I won't explain here), so it's best not to use it for logical operations.
0 ^ 0 == 0
0 ^ !0 == !0
!0 ^ 0 == !0
!0 ^ !0 == 0

Needless to say, an expression such as 1<2 is evaluated as !0. You fill the gaps.

EDIT: ^Damn. Beaten by seconds and by an even better method. Well, use whichever you prefer.
Last edited on
I have to admit that i am a little confused on how to use the logic operators in this case...
(just a typing mystake: Operator || is "or" not "and")
Thanks. Copy-pasting mistake.
Well, he'd have to use (a<b && a<c && ...). Like I said, your method is better. I do think that's what the guy wanted him to do.
I did't see your edit... :P
I believe it is just a simple example on how to use the if statement.
I was always getting confused with too many conditions in an if statement, so I prefer to use as less as possible.. :D
Oh, well then I guess you never used a Karnaugh's map (http://en.wikipedia.org/wiki/Karnaugh) to obtain the formulas generating truth tables. You can get some preeety complex expressions.
Well, I feel kinda verbose, so...
!a && !b == !(a || b)
!a || !b == !(a && b)
I used the Karnaugh's map very much for my electronic engineering class in Digital Circuits...
I understand logic operators very well, and they can be really complex as you sayed, but in programming I prefer to write code that I can read easier. So less conditions in one if makes me read it better... :D
Topic archived. No new replies allowed.