Hey guys, I'm a total noob to C Programming and I'm trying to get my head around why this set of code won't work. I need to find the largest of three user-defined floating point numbers and then print "The largest number is X" where X is that number. Anyone can help? When I compile and type in three numbers, the program just closes and quits. #include <stdio.h> int main() { float A; float B; float C; printf("Please enter three floating-point numbers (e.g. 3.12):\n"); scanf("%d %d %d", &A, &B, &C); if (A > B && A > C) printf("\nThe largest floating-point number entered is %d", A); if (B > A && B > C) printf("\nThe largest floating-point number entered is %d", B); if (C > A && C > B) printf("\nThe largest floating-point number entered is %d", C); if (C == A && C == B) printf("\nThe numbers entered are equal."); scanf("%d"); return 0; }
The reason it just closes and quits is because its run entirely and then closes, all before the blink of an eye. Add system("PAUSE"); before the return statement so that you can see the result. Also, your algorithm doesn't look very efficient. I haven't really been bothered to look at it much but for starters you should use "if else" statements, that way if A is the largest it won't bother testing the others. If you want to rewrite it more even more efficiently Id suggest using a loop statement.
Well, I placed the scanf("%d") in there at the end to mimic the system(pause) since our lecturer hasn't moved that far into the course. Also, I just tried it again and the printed result is 0, not the value of X What gives?
I'm glad I could help. Still, I don't know how far into the language you are, but if you want to impress you could, as i said, do this more efficiently using loops and I'd be more than willing to help. Though I'd understand if you haven't done loops yet and don't want to use something you haven't covered in class.
The tutorial asks for if/ ifelse statements so I can't use loops, although I'm sure I'll learn them soon enough.. I need to get my head around all these new variables like char and how to initialise it. This is only my second attempt at programming! :X
Yes and while he's at it, why doesn't he implement a bubble sort algorithm? Seriosuly though, just from glancing at your code, it looks like there may be a slight problem if two of the numbers are equal and larger than the third. Tried it?
Yeah, thats nothing a few >= wouldnt solve though, as he is looking for the highest number not the biggest individual variable..
Yeah, I did think about that problem with the variable equalling another..! I'm now trying to sort out this next problem where I've got to receive an input from the user stating which arithmetic operation to complete (x, /, + or -), then two floating point numbers which will then have that operation completed on them. Here is my code: #include <stdio.h> int main() { float A, B; char op = '\0'; printf("Please enter the operator (+, -, * or /):\n"); scanf("%c", &op); printf("Please enter two numbers:\n"); scanf("%f%f", &A,&B); if (op = '+') printf("Answer: %f %c %f = %f", A, op, B, A+B); if (op = '-') printf("Answer: %f %c %f = %f", A, op, B, A-B); if (op = '/') printf("Answer: %f %c %f = %f", A, op, B, A/B); if (op = '*') printf("Answer: %f %c %f = %f", A, op, B, A*B); scanf("%f"); return 0; } I have scrounged around my notes and there isn't any bit about declaring char variables. My if statements seem useless in the program because all printf statements are run, which leads me to believe that I did not initialise "char op" correctly..
First you need to compare in your if statements with '==' not '=' (eg if(op == '+') ). Second you should be using if-else statements or a switch statement (but I'm guessing you haven't learned about those yet). Code: if(op == '+') printf() else if(op == '-') printf() ... You also don't need to assign to a char when you declare it. You can just do: char c; Also, once you feel comfortable with input/output in C, you should write your own input functions as scanf is susceptible to buffer overflow attacks.
I'd go into great detail about why system('PAUSE') is a bad idea, but it works just fine for what you're doing. Congrats.
Thanks a lot for that Sometimes I get really annoyed at my syntax errors and hopefully more exposure to the stuff will make me improve.
Don't worry it will. Id say it happens to all of us, I know it did to me, but over time those kinds of errors will become less and less common.