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)
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
}
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.
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