Theory5 Posted January 5, 2010 Report Share Posted January 5, 2010 Hey, I am in C programming this trimester. This is the second programming assignment we have had, and this one stumps me. I have to write a program into my first programming assignment that says which of three numbers is the largest and the smallest, and it has to say which ones are which. In the book all the examples I can find result in 0 (false) or 1 (true). Could somebody help me? ------------------------------------// Milandon Foley CSIS 1020// 12/15/09 Tuesdays and Thurdays #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]){ int sum, avg, prod; int x, y, z; int min, max; printf ("Enter Three Numbers> "); scanf("%d%d%d", &x, &y, &z); // store values of integers sum = x + y + z; //sum avg = (x + y + z)/3; //average prod = x * y * z; // product this is the part I am having trouble with if (x < y < z); printf("%d is greatest", z); else if (x > y > z); printf("%d is greatest", x); else (x < y > z); printf ("%d is greatest", y); printf("The Sum of these numbers is %dn", sum); printf("The Average of these numbers is %dn", avg); printf("The Product of these numbers is %dn", prod); return 0;} Quote Link to comment Share on other sites More sharing options...
Tormod Posted January 5, 2010 Report Share Posted January 5, 2010 Exactly what is it that you're having problems with in that part of the code? It seems fairly straightforward. Quote Link to comment Share on other sites More sharing options...
Theory5 Posted January 5, 2010 Author Report Share Posted January 5, 2010 Exactly what is it that you're having problems with in that part of the code? It seems fairly straightforward. The compiler is telling me its wrong, and I dont know how to complete the rest of the assignment I would appreciate some help with that as well.Im using Microsoft Visual Studio 2008 c++ compiler. Quote Link to comment Share on other sites More sharing options...
Donk Posted January 5, 2010 Report Share Posted January 5, 2010 this is the part I am having trouble with if (x < y < z); printf("%d is greatest", z); else if (x > y > z); printf("%d is greatest", x); else (x < y > z); printf ("%d is greatest", y);I'm no expert, but shouldn't the second "else" be "else if"? edit: forget that: C's moved on a way since I last used it - I should have checked before posting :Glasses: Quote Link to comment Share on other sites More sharing options...
BrianG Posted January 5, 2010 Report Share Posted January 5, 2010 I agree with Donk, there seems to be a discrepancy between if, else and else if. Quote Link to comment Share on other sites More sharing options...
Pyrotex Posted January 5, 2010 Report Share Posted January 5, 2010 Both Donk and Brian are correct. In addition, you don't perform all possible tests, so about half the timeyour code will not work even though it has no code errors. The code should look like this: if (x < y < z); printf("%d is greatest", z, " %d is least", x); else if (y < x < z); printf("%d is greatest", z, " %d is least", y); else if (z < x < y); printf("%d is greatest", y, " %d is least", z); else if (x < z < y); printf("%d is greatest", y, " %d is least", x); else if (y < z < x); printf("%d is greatest", x, " %d is least", y); else if (z < y < x); printf("%d is greatest", x, " %d is least", z); else; printf("Error in IF statements"); Quote Link to comment Share on other sites More sharing options...
Theory5 Posted January 6, 2010 Author Report Share Posted January 6, 2010 Both Donk and Brian are correct. In addition, you don't perform all possible tests, so about half the timeyour code will not work even though it has no code errors. The code should look like this: if (x < y < z); printf("%d is greatest", z, " %d is least", x); else if (y < x < z); printf("%d is greatest", z, " %d is least", y); else if (z < x < y); printf("%d is greatest", y, " %d is least", z); else if (x < z < y); printf("%d is greatest", y, " %d is least", x); else if (y < z < x); printf("%d is greatest", x, " %d is least", y); else if (z < y < x); printf("%d is greatest", x, " %d is least", z); else; printf("Error in IF statements"); Thanks, but for some reason I get a whole lot of errors when I use that, alex.The ones that repeat over and over are "illegal else without matching if" and "'<' unsafe use of type 'bool' operation"when I was playing around with it before the compiler had problems with three different integer's with the greater than or less than thing. it doesn't do it with (x < y) though. just (x < y > z) or something like that. Quote Link to comment Share on other sites More sharing options...
Buffy Posted January 6, 2010 Report Share Posted January 6, 2010 (x < y < z) is not a boolean expression which is why you're having a problem. "<" is a *binary* operator, and the way the compiler evaluates this is as ((x < y) < z), and (x < y) evaluates to a boolean and a (boolean < int) is meaningless. You need to express this as ((x < y) && (y < z)). There are a few very small incompatible changes - I really doubt most people will ever run into them, :)Buffy Quote Link to comment Share on other sites More sharing options...
Buffy Posted January 6, 2010 Report Share Posted January 6, 2010 Oh a follow up: if this were a *real* C compiler you were using, (x < y) would evaluate to either -1 or 0, and you would not get a compiler error like you do with the C++ compiler (which is much more strongly typed), but you'd get some pretty hard to explain results! :) Through every rift of discovery some seeming anomaly drops out of the darkness, and falls, as a golden link into the great chain of order, Buffy Quote Link to comment Share on other sites More sharing options...
modest Posted January 6, 2010 Report Share Posted January 6, 2010 I agree with Buffy, but I'm wondering if this, The ones that repeat over and over are "illegal else without matching if"... is not caused by the semicolons I highlighted red,if (x < y < z)[color="Red"];[/color] printf("%d is greatest", z); else if (x > y > z)[color="Red"];[/color] printf("%d is greatest", x); else (x < y > z)[color="Red"];[/color] printf ("%d is greatest", y);which, though I am a novice especially with C, I'm pretty sure you need to lose. Try, if ((x < y) && (y < z)) printf("%d is greatest", z, " %d is least n", x); else if ((y < x) && (x < z)) printf("%d is greatest", z, " %d is least n", y); else if ((z < x) && (x < y)) printf("%d is greatest", y, " %d is least n", z); else if ((x < z) && (z < y)) printf("%d is greatest", y, " %d is least n", x); else if ((y < z) && (z < x)) printf("%d is greatest", x, " %d is least n", y); else if ((z < y) && (y < x)) printf("%d is greatest", x, " %d is least n", z); else printf("Error in IF statements n"); or, if ((x < y) && (x < z)){ if (y < z) printf("%d is least and %d is greatest n", x, z); else printf("%d is least and %d is greatest n", x, y); } else if ((y < x) && (y < z)){ if (x < z) printf("%d is least and %d is greatest n", y, z); else printf("%d is least and %d is greatest n", y, x); } else { if (x < y) printf("%d is least and %d is greatest n", z, y); else printf("%d is least and %d is greatest n", z, x); } ~modest Quote Link to comment Share on other sites More sharing options...
sanctus Posted January 6, 2010 Report Share Posted January 6, 2010 Modest, that is what i thought too. A cool thing, I think, in C++ is that you can leave out the double brackets {} if it is just a one-line condition... Quote Link to comment Share on other sites More sharing options...
Theory5 Posted January 6, 2010 Author Report Share Posted January 6, 2010 Thanks modest this one worked:if ((x < y) && (y < z)) printf("%d is greatestn", z,"%d is leastn", x);else if ((y < x) && (x < z))printf("%d is greatestn", z,"%d is leastn", y);else if ((z < x) && (x < y))printf("%d is greatestn",y ,"%d is leastn", z);else if ((x < z) && (z < y))printf("%d is greatestn", y,"%d is leastn", x);else if ((y < z) && (z < x))printf("%d is greatestn", x, "%f is leastn", y);else if ((z < y) && (y < x))printf("%d is greatestn", x,"%f is leastn", z);else printf("Error in IF statementsn"); I used the third one, it worked better.Thanks for everybody's help! Quote Link to comment Share on other sites More sharing options...
alexander Posted January 6, 2010 Report Share Posted January 6, 2010 or you can expand on that program (wait was i the only one that would write code that would do more then the specs "required" it to) void quickSort(int *arr, int elements) { #define MAX_LEVELS 1000 int piv, beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R ; beg[0]=0; end[0]=elements; while (i>=0) { L=beg[i]; R=end[i]-1; if (L<R) { piv=arr[L]; if (i==MAX_LEVELS-1) return NO; while (L<R) { while (arr[R]>=piv && L<R) { R--; if (L<R) { arr[L++]=arr[R]; } } while (arr[L]<=piv && L<R) { L++; if (L<R) { arr[R--]=arr[L]; } } } arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L; } else { i--; } } return; } throw as many values as they want to into an array, sort them and whatever... Quote Link to comment Share on other sites More sharing options...
Theory5 Posted January 7, 2010 Author Report Share Posted January 7, 2010 or you can expand on that program (wait was i the only one that would write code that would do more then the specs "required" it to) void quickSort(int *arr, int elements) { #define MAX_LEVELS 1000 int piv, beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R ; beg[0]=0; end[0]=elements; while (i>=0) { L=beg[i]; R=end[i]-1; if (L<R) { piv=arr[L]; if (i==MAX_LEVELS-1) return NO; while (L<R) { while (arr[R]>=piv && L<R) { R--; if (L<R) { arr[L++]=arr[R]; } } while (arr[L]<=piv && L<R) { L++; if (L<R) { arr[R--]=arr[L]; } } } arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L; } else { i--; } } return; } throw as many values as they want to into an array, sort them and whatever... nice, but my professor might be a little suspicious if I turn in code that we haven't learned yet :-) so far I know how to do everything that is in both assignments, and thats it. :-)When I had taught myself Liberty Basic code I would love adding things in that I probably didn't need. Such as the option to find square roots, cube roots, etc up to the fifth one. Quote Link to comment Share on other sites More sharing options...
alexander Posted January 7, 2010 Report Share Posted January 7, 2010 lol never stopped me, my first hello world for the second C++ clourse used a visualization library and a sound library and displayed a 3d model of "hello world" that would turn, jump, flip and switch colors depending on what the music was doing... Quote Link to comment Share on other sites More sharing options...
Theory5 Posted January 7, 2010 Author Report Share Posted January 7, 2010 lol never stopped me, my first hello world for the second C++ clourse used a visualization library and a sound library and displayed a 3d model of "hello world" that would turn, jump, flip and switch colors depending on what the music was doing... sounds like you were a bit ahead of the class :-P Quote Link to comment Share on other sites More sharing options...
alexander Posted January 7, 2010 Report Share Posted January 7, 2010 that would be a bit of an under-statement, but at the time i've already been exposed to a dozen languages, and i was picking up python... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.