Click here to Skip to main content
15,889,574 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, c, notmin, min, max;
    printf("enter first integer\n");
    scanf("%d", &a);
    printf("enter second integer\n");
    scanf("%d", &b);
    printf("enter third integer\n");
    scanf("%d", &c);
    (a > b) ? (a = notmin) : (b = notmin);
    (c > notmin) ? (c = max) : (notmin = max);
    printf("%d", max);
    return 0;
}


What I have tried:

I tried to write a programm that prints the highest out of 3 integers. However the output is always a random 7-digit integers. Where is the problem?
Posted
Updated 4-Oct-20 21:34pm
v3
Comments
KarstenK 4-Oct-20 13:10pm    
you should see it when using the debugger :-O

You have not set any values in notmin, min or max.
 
Share this answer
 
Comments
Member 14955513 4-Oct-20 13:20pm    
But when I do set a value in the beginning it just gets printed out, not the number a, b, c
jeron1 4-Oct-20 14:07pm    
To set the value of max, it has to be on the left side of the assignment operation, as in

max = 5;
Richard MacCutchan 5-Oct-20 3:58am    
But these numbers are not constants. You should start by setting max to zero, and min to the largest value possible. Then for each number entered you need to compare with each of these numbers. If the entered number is greater than max, then set max to this new value. If the entered number is less than min then set min to this new value. I have no idea what "notmin" is for.
Have a look at your code, and ask your =self several questions.
1) what do notmin, min, and max start with?
2) what changes max?
3) why are you using x ? y : z instead of if ... else?

If you want the biggest of three numbers, it's easy:
Is a bigger than b?
If it is, max becomes a, otherwise it becomes b;
Is c bigger than max?
If it is, max becomes c.
What do you need min and notmax for?
 
Share this answer
 
Quote:
I tried to write a programm that prints the highest out of 3 integers. However the output is always a random 7-digit integers. Where is the problem?

One of the problems is that you don't seems to know how to save a value in a variable, the problem is that this is C 101 course.
First, try to replace:
C++
(a > b) ? (a = notmin) : (b = notmin);
(c > notmin) ? (c = max) : (notmin = max);

with:
C++
(a > b) ? (notmin = a) : (notmin = b);
(c > notmin) ? (max = c) : (max = notmin);

then the correct syntax is:
C++
notmin = (a > b) ? a : b;
max = (c > notmin) ? c : notmin;

Advice: learn properly C/C++
 
Share this answer
 
If you really like the C ternary operator, the try
C
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,c,max;
    printf("enter first integer\n");
    scanf("%d", &a);
    printf("enter second integer\n");
    scanf("%d", &b);
    printf("enter third integer\n");
    scanf("%d", &c);

    max = a > b ? ((a > c) ? a : c) : ((b > c) ? b : c);

    printf("%d\n", max);

    return 0;
}


However, for the sake of clarity, I would suggest you to use the if...else statement[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900