You see, I\'ve self-taught myself C++ (not completely, I\'m still procrastinating -_-). So, now I started university and they\'re teaching C and they made us do a program of
Try this
int max_of_four(int a,int b,int c,int d){
int max=a;
if(b>max) max=b;
if(c>max) max=c;
if(d>max) max=d;
return max;
}
without if would be like this
int max_of_four(int a, int b, int c, int d) {
return ((a > b && a > c && a > d) ? a: ((b > c && b > d) ? b : (c > d ? c : d)));
}
As per the OP's condition
But seeing that all we've covered in class for now are loops and decision statements. Is there a more elegant way of doing this? One which uses fewer
if
s?
Only one if
and one else if
statement and one for
loop can do this task. Simple and short!
#include <stdio.h>
int main()
{
int num, max, min;
printf ("Enter four numbers: ");
scanf ("%d", &num);
max = min = num;
for (int i = 0; i < 3; i++)
{
scanf ("%d", &num);
if (max < num)
max = num;
else if (min > num)
min = num;
}
printf ("The smallest and largest of given four numbers are %d and %d respectively.\n", min, max);
return 0;
}
int max_of_four(int a, int b, int c, int d){
int res=a;
if(b/res)
res=b;
if(c/res)
res=c;
if(d/res)
res=d;
return res;
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
Please have at the following
private int GetLargerValue(int num1, int num2, int num3, int num4)
{
int largeValue = 0;
if (num1 > num2)
{
if (num1 > num3)
largeValue = (num1 > num4) ? num1 : num4;
else
largeValue = (num3 > num4) ? num3 : num4;
}
else if (num2 > num3)
largeValue = (num2 > num4) ? num2 : num4;
else
largeValue = (num3 > num4) ? num3 : num4;
return largeValue;
}
Do a "manual" merge sort, or well, just the second bit of it:
Conceptually, a merge sort works as follows
- Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
- Repeatedly merge sublists to produce new sublists until there is only 1 sublist remaining. This will be the sorted list.
Code:
int a = 5, b=4, c=7, d=9;
int min_ab, min_cd, min;
min_ab = a < b ? a : b;
min_cd = c < d ? c : d;
min = min_ab < min_cd ? min_ab : min_cd;
printf("%d", min);
.. and similarly for max.
If you prefer, you can expand the ternary operator into if (a < b) { min_ab = a; } else { min_ab = b; }
(spread over multiple lines for readability).
Merge sort has a complexity of O(n*log(n))
, so you should at most need O(n*log(n))
if
s (see the wikipedia article on merge sort). According to Wikipedia, "... These are all comparison sorts, and so cannot perform better than O(n log n) in the average or worst case" (source), so I think this shouldn't be too far off in terms of minimum number of if
s.. Though you could try to see if manually performing one of the other algorithms results in fewer if
s ;-).
#include <stdio.h>
int main(void) {
int int_1, int_2, int_3, int_4;
int pair_1_largest = 0, pair_1_smallest = 0;
int pair_2_largest = 0, pair_2_smallest = 0;
int quartet_largest = 0, quartet_smallest = 0;
printf("Example: 15 38 8 21\n");
printf("\nEnter four integers: ");
scanf("%d %d %d %d", &int_1, &int_2, &int_3, &int_4);
if(int_1 > int_2)
{
pair_1_largest = int_1;
pair_1_smallest = int_2;
}
else
{
pair_1_largest = int_2;
pair_1_smallest = int_1;
}
if(int_3 > int_4)
{
pair_2_largest = int_3;
pair_2_smallest = int_4;
}
else
{
pair_2_largest = int_4;
pair_2_smallest = int_3;
}
if(pair_1_largest > pair_2_largest)
quartet_largest = pair_1_largest;
else
quartet_largest = pair_2_largest;
if(pair_1_smallest < pair_2_smallest)
quartet_smallest = pair_1_smallest;
else
quartet_smallest = pair_2_smallest;
printf("The largest number is: %d\n", quartet_largest);
printf("The smallest number is: %d\n", quartet_smallest);
return 0;
}
Hey everybody! I'm a beginner in programming so don't be to harsh on me :) "Native C Programming" by K.N. King is of help!