GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Nov-2004, 00:30
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

Here is a questions about detecting the smallest and the largest integers


Here is a question I am trying to answer and it is confusing to me.

I wonder if someone can read it and understand it.

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 my answer which I know is not right.

CPP / C++ / C Code:
if(x<y && y<z)
    {
min = min + x;
    }
      if(y<z && z<x)
    {
min = y;
    }
    if(z<y && y<x)
    {
min = z;
    }

Any clues for me?
  #2  
Old 23-Nov-2004, 02:41
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
How about this ? Its a pseudo code.

CPP / C++ / C Code:
int minimum (x,y,z) {
if x<y then min=x else min=y
if min<z then min else min=z
return min
}

int maximum (x,y,z) {
if x>y then max=x else max=y
if max>z then max else  max=z
return max
}


Try to write a functions and see if works.

you can call these functions in main() as

CPP / C++ / C Code:
int min,max;

read x,y and z values from user or assign the values manually.

min=minimum(x,y,z);
max=maximum(x,y,z);

print min
print max

  #3  
Old 23-Nov-2004, 02:42
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by jenmaz
Here is a question I am trying to answer and it is confusing to me.

I wonder if someone can read it and understand it.

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 my answer which I know is not right.

CPP / C++ / C Code:
if(x<y && y<z)
    {
min = min + x;
    }
      if(y<z && z<x)
    {
min = y;
    }
    if(z<y && y<x)
    {
min = z;
    }

Any clues for me?
Sure. Start min and max at x
Then test y with min and max and change them if necessary
Repeat with z
Voila!
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #4  
Old 23-Nov-2004, 12:27
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Thanks Walt,

I only need to find the min of the 3 variables. On that note was I in the ballpark with my if then statements? I do not really understand your answer. Sorry and thanks in advance,

Jennifer
  #5  
Old 23-Nov-2004, 15:08
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Hi there,

I have been working on thisi and came to this conclusion.

int maximum ( int x, int y, int z) {

int max = 0;

if ( x < min) min = x;
if ( y < min) min = y;
if ( z < min) min= z;

return min;
}

But I am getting feedback that tells me that I not assigning anything to min.


Here is the question again...Any clues? A lot of you are giving me examples of max and I am not sure why?

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).
  #6  
Old 23-Nov-2004, 15:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by jenmaz
Here is a question I am trying to answer and it is confusing to me.

I wonder if someone can read it and understand it.

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 my answer which I know is not right.

CPP / C++ / C Code:
if(x<y && y<z)
    {
min = min + x;
    }
      if(y<z && z<x)
    {
min = y;
    }
    if(z<y && y<x)
    {
min = z;
    }



Any clues for me?

In the first place, I think there is a typo here. I think you meant:

CPP / C++ / C Code:
if(x<y && y<z) 
    {
min = x;
    }
      if(y<z && z<x)
    {
min = y;
    }
    if(z<y && y<x)
    {
min = z;
    }

(Since "min = min + x" at the first comparison makes no sense at all.)
Now, the problem is, that this will give the right answer some of the time, but not all of the time. You obviously tested it for a few cases, otherwise why would you even think this is close?

Let's do a "think" problem; no computer required.

First of all, if we come up with a proposed solution, there is a way to see if we are right: test all possible paths through the program.

Here's the first lesson: If you write a program, you should think about how you are going to test it. One way is to supply lots of different inputs and see if it gives you the right answer. I can hear you now, saying, "Now, wait a minute!" if I knew the right answer, I wouldn't need no stinkin' program. And in fact, lots of times we don't know an exact answer, but if we don't have any way to check for a reasonable answer, we shouldn't take any computer output (or any other output) as absolute truth. At any rate, we should have some way to verify the reasonableness of the program's output.

In this case, we can come up with a methodology that tests all paths through the program. (There are only 27 different possibilities.)

Now, as math professors sometimes say, "without loss of generality", lets suppose that x, y, and z can only take on values of 1, 2, or 3. If we do this, then we see that we can test every path through our program by simply letting x go from 1 to 3, let y go from 1 to 3, and let z go from 1 to 3.

Don't know how to write a program to do that? Well, that might be a good exercise in itself, just to see how loops work.

But for now, forget the program, lets go through your proposed algorithm with a few values of x, y, and z (Pencil and paper and/or brainpower only; no computer required.)

Suppose x = 1, y = 2, and z = 3.

Then we see that x <y and y < z, so your very first test passes and sets min to a value of 1 (The right answer. Yay)

Suppose we let x = 3, y = 1, and z = 2

Now the first test fails (since x is not less than y)
The second test passes, since y is less than z and z is less than x, so
we set min to a value of 1 (The right answer: two in a row! We're on a roll).

Suppose we let x = 2, y = 1, and z = 3

Well, the first test fails, since x is not less than y
The second test fails since z is not less than x
The third test fails since z is not less than y (Bummer: min is not set to anything, since we have apparently not tested all possibilities.)

Now, once again forget the computer for a minute and think how you would to this without a computer.

Here is one way:

See if x is less than y. If it is then do this:

We know that y is not the min, so we must test x against z to see
which is smaller. The winner of the x,z comparison is the min, and
we are done.


If x is not less than y, we know x is not the min, so we have only to
test y versus z. Whoever wins that will be the min.



Here is a way to do that in C
CPP / C++ / C Code:
/* simple test for minimum of value of x, y, z */

  if(x < y) {
    if (x < z) {
      min = x;
    }
    else {
      min = z;
    }
  }
  else {
    if (y < z) {
      min = y;
    }
    else {
      min = z;
    }
  }

Now, this is a very specialized test for the case where we have only three things to compare. If I had to find a minimum among, say 1000 items, I would used some slightly more advanced mechanism, but this does show something about programming procedures for problem solving.

Regards,

Dave
Last edited by davekw7x : 23-Nov-2004 at 16:21.
  #7  
Old 23-Nov-2004, 17:45
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Dave,

Thank you sooo much for taking the time to explain this to me. I understand this and am working on 2 more problems like this right now! Why do you do this? This is great! Thanks again.

Jennifer
  #8  
Old 23-Nov-2004, 21:33
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by jenmaz
Hi there,

I have been working on thisi and came to this conclusion.

int maximum ( int x, int y, int z) {

int max = 0;

if ( x < min) min = x;
if ( y < min) min = y;
if ( z < min) min= z;

return min;
}

But I am getting feedback that tells me that I not assigning anything to min.


Here is the question again...Any clues? A lot of you are giving me examples of max and I am not sure why?

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).
This is sooooooooooooooooooo close!!!
This minor change will fix it:
CPP / C++ / C Code:
int maximum ( int x, int y, int z) 
{
  int min;  // you defined max, not min...
  min = x;  // start by assuming x is the minimum
  if ( y < min) min = y; 
  if ( z < min) min= z;

  return min;
}

Don't forget code tags around your code...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #9  
Old 24-Nov-2004, 09:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by WaltP
This is sooooooooooooooooooo close!!!
This minor change will fix it:
CPP / C++ / C Code:
int maximum ( int x, int y, int z) 
{
  int min;  // you defined max, not min...
  min = x;  // start by assuming x is the minimum
  if ( y < min) min = y; 
  if ( z < min) min= z;

  return min;
}

Don't forget code tags around your code...

Sorry, my rather long-winded response was a reply to a previous post that wasn't very close. It took so long for me to figure out what I wanted to say, that this particular problem had been pretty much resolved, except for Walt's tweak.

This method is obviously more elegant and also easily extended by making a loop so that the minimum element in an array of any size can be found. (And, I claim, the way that I would have done it myself.) My previous post was more about an approach to problem solving with a computer program than about finding the minimum value of three ints.

Regards,

Dave
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:00.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.