Jump to content
Science Forums

C Programming "if else" help!


Recommended Posts

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;

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

Both Donk and Brian are correct.

In addition, you don't perform all possible tests, so about half the time

your 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");

Link to comment
Share on other sites

Both Donk and Brian are correct.

In addition, you don't perform all possible tests, so about half the time

your 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.

Link to comment
Share on other sites

(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

Link to comment
Share on other sites

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, :detective:

Buffy

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...