Guys can you help me ?
I'm novice in C but I have a lot of will to master C. Each gurney starts with first step
O.K. My problem is to compare two integers entered by user
I know how to set up conditions and problem is how to print correct value (grater number)
// This program compares two integers and prints which number is
// greatest number from given input
#include <stdio.h>
int main(void)
{
int a, b; // two integers
// Program shell ask user for input and store input in comp. memory
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
// Tests which is greater using if statements
if ( a > b && b < a || b > a && a < b)
printf("Greatest: %d\n", a);
else
printf("Smallest: %d\n", b);
getchar();
getchar();
return 0;
}
I know problem is red marked area. How to correct that ?
Here are two ways of doing it
// Tests which is greater using if statements
if ( a > b)
printf("Greatest: %d\n", a);
else
printf("Greatest: %d\n", b);
// IIF ternary operator
printf("Greatest: %d\n", a>b?a:b);
1 Like
Hi Solaris User,
The problem is not with your understanding of "how to print" something. I think you are a bit confused about how to set up "conditions." The easiest way is as follows:
if ( a > b)
printf("Greatest: %d\n", a);
else
printf("Greatest: %d\n", b);
1 Like
Note that the "if" condition will always be true if "a" and "b" are distinct integers. If a > b, then the red part is true:
if ( a > b && b < a || b > a && a < b)
And if a < b, then the other red part is true:
if ( a > b && b < a || b > a && a < b)
So the control will go to the "if" branch as long as "a" and "b" are distinct (because of the "||" operator), and you'll see "Greatest: <n>".
If "a" and "b" are the same, then the control will go to "else" part, so you'll see "Smallest: <n>".
$
$ cat greater.c
// This program compares two integers and prints which number is
// greatest number from given input
#include <stdio.h>
int main(void)
{
int a, b; // two integers
// Program shell ask user for input and store input in comp. memory
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
// Tests which is greater using if statements
if ( a > b && b < a || b > a && a < b)
printf("Greatest: %d\n", a);
else
printf("Smallest: %d\n", b);
getchar();
getchar();
return 0;
}
$
$ # Control goes to "if" branch for the next two cases
$ ./greater
Enter two integers: 10 99
Greatest: 10
$
$ ./greater
Enter two integers: 99 10
Greatest: 99
$
$ # Control goes to "else" branch for the next case
$ ./greater
Enter two integers: 99 99
Smallest: 99
$
$
tyler_durden
2 Likes
Thanks guys on your help. I understand better logical expressions. I'm trying to resolve simple programming exercise from my handbook.
I need to read four integers (author says input test will be introduced in next chapter) so step by step.
This is new source
me@my-pc:~/Desktop$ cat comparation.c
// Compares four integers and prints which is greatest and which is smallest
#include <stdio.h>
int main(void)
{
int a, b, c, d;
// Prompts user to input four integers and stores in variables a, b
// c and d
printf("Enter four integers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
// to be the largest number among the numbers entered this number must
// be strictly greater than all numbers entered
// analogous for the smallest
// Test for number stored in variable a
if ( (a > b && a > c && a > d) || ( a < b && a < c && a < d) )
printf("Greatest: %d\n", a);
else
printf("Smallest %d\n", a);
// Test for number stored in variable b
if ( (b > a && b > c && b > d) || ( b < a && b < c && b < d) )
printf("Greatest: %d\n", b);
else
printf("Smallest %d\n", b);
// Test for number stored in variable c
if ( (c > a && c > b && c > d) || ( c < a && c < b && c < d) )
printf("Greatest: %d\n", c);
else
printf("Smallest %d\n", c);
// Test for number stored in variable d
if ( (d > a && d > b && d > c) || ( d < a && d < b && d < a) )
printf("Greatest: %d\n", a);
else
printf("Smallest %d\n", a);
return 0;
}
Output
me@my-pc:~/Desktop$ ./test
Enter four integers: 25 12 34 52
Smallest 25
Greatest: 12
Smallest 34
Greatest: 25
But I want to output like
Enter four integers: 1 2 3 4
Greatest 4
Smallest 1
:wall:
You want something like this -
$
$ cat -n comparison.c
1 // Compares four integers and prints which is greatest and which is smallest
2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 int a, b, c, d;
8
9 // Prompts user to input four integers and stores in variables a, b
10 // c and d
11
12 printf("Enter four integers: ");
13 scanf("%d %d %d %d", &a, &b, &c, &d);
14
15 // to be the largest number among the numbers entered this number must
16 // be strictly greater than all numbers entered
17 // analogous for the smallest
18
19 // Test for number stored in variable a
20 if ( a > b && a > c && a > d )
21 printf("Greatest: %d\n", a);
22 else if ( a < b && a < c && a < d )
23 printf("Smallest %d\n", a);
24
25 // Test for number stored in variable b
26 if ( b > a && b > c && b > d )
27 printf("Greatest: %d\n", b);
28 else if ( b < a && b < c && b < d )
29 printf("Smallest %d\n", b);
30
31 // Test for number stored in variable c
32 if ( c > a && c > b && c > d )
33 printf("Greatest: %d\n", c);
34 else if ( c < a && c < b && c < d )
35 printf("Smallest %d\n", c);
36
37 // Test for number stored in variable d
38 if ( d > a && d > b && d > c )
39 printf("Greatest: %d\n", d);
40 else if ( d < a && d < b && d < c )
41 printf("Smallest %d\n", d);
42
43 return 0;
44 }
45
$
$
That is, you perform the greatest/smallest test for each of the 4 numbers.
$
$ ./comparison
Enter four integers: 1 2 3 4
Smallest 1
Greatest: 4
$
$
$ ./comparison
Enter four integers: 18 44 7 31
Greatest: 44
Smallest 7
$
$
Alternatively, you may want to implement any of the standard sorting algorithms. (Although I suspect it might be a bit too overwhelming at this stage.)
tyler_durden
1 Like