By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Online lab help please.

P: 64
Hello, I been working at this for about 30 minutes now and can't seem to get it. This part is called One-Way selection. Here is the question...

Given the integer variables x , y , and z , write a fragment of code that assigns the smallest of x , y , and z to another integer variable min .

Assume that all the variables have already been declared and that x , y , and z have been assigned values).
Here is the code the provide

Expand|Select|Wrap|Line Numbers
  1. 1 int main() {
  2. 2    int x, y, z, min;
  3.  
Your Code here...

Expand|Select|Wrap|Line Numbers
  1. 6    return 0;
  2. 7 }
  3.  

Now my question is how wold you find which one has the lowest amount?
Feb 20 '07 #1
Share this Question
Share on Google+
8 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hello, I been working at this for about 30 minutes now and can't seem to get it. This part is called One-Way selection. Here is the question...



Here is the code the provide

Expand|Select|Wrap|Line Numbers
  1. 1 int main() {
  2. 2    int x, y, z, min;
  3.  
Your Code here...

Expand|Select|Wrap|Line Numbers
  1. 6    return 0;
  2. 7 }
  3.  

Now my question is how wold you find which one has the lowest amount?
How would you find the lowest amount? Without a computer program?
Feb 20 '07 #2

P: 64
How would you find the lowest amount? Without a computer program?
I would first look at all the numbers and see which one is the lowest. Or I would do if A<B and A<C then A is the lowest. If not then etc etc.
Feb 20 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
I would first look at all the numbers and see which one is the lowest. Or I would do if A<B and A<C then A is the lowest. If not then etc etc.
So you would compare one to the other, store the smaller of the two, compare it to the next, and keep doing that until you have completed the program.

You have your algorithm right there, where are you stuck? On the comparisons?
Feb 20 '07 #4

P: 64
So you would compare one to the other, store the smaller of the two, compare it to the next, and keep doing that until you have completed the program.

You have your algorithm right there, where are you stuck? On the comparisons?
Well, when I try this

Expand|Select|Wrap|Line Numbers
  1. if (x<y && x<z)
  2.     min= x;
  3. else
  4. if (y<x && y<z)
  5.     min= y;
  6. else
  7. if (z<x && z<y)
  8.     min= z;
  9.  
I get the error
Suggestion: You are not assigning anything to min.
Feb 20 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
Well, when I try this

Expand|Select|Wrap|Line Numbers
  1. if (x<y && x<z)
  2.     min= x;
  3. else
  4. if (y<x && y<z)
  5.     min= y;
  6. else
  7. if (z<x && z<y)
  8.     min= z;
  9.  
I get the error
I would try removing the else statements, see what that does.
Feb 20 '07 #6

P: 64
I would try removing the else statements, see what that does.
No luck darn.
Feb 20 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677
No luck darn.
If that didn't work, I would try each one by itself.
Feb 20 '07 #8

Expert
P: 83
It looks fine, with the else statements. The problem is that the compiler can't tell that in all cases one of the three must be true, so it thinks that in some cases, min might be unassigned. Make it shut up by declaring min=0 at the top of your program. That way, it's satisfied because min will always be defined.
Feb 20 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.