Jump to content

Find The Smallest No

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Ajiz Zone

Ajiz Zone

    Newbie

  • Members
  • Pip
  • 4 posts
hi codecall.net users

i want to ask help from u guyz...
please check the code below is correct or no to fine smallest no.
this was me and my friend's first program after learn c++ in our class...

Quote

int x, y, z;
{
cout << "enter no 1: " << endl;
cin >> x;

cout << "enter no 2: " << endl;
cin >> y;

cout << "enter no 3: " << endl;
cin >> z;
}


if (x >= y )
{
cout << "The smallest value is: " << y << endl;
}

else if ( x <= z)
{
cout << "The smallest value is: " << x << endl;
}

else
{
cout << "The smallest value is: " << z << endl;
}


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
You won't know what number is smallest until you have compared all three of them

by using if-conditions, you would need to first compare two numbers, and then do the second comparison. There is many ways of doing this of course. this is one way:

[highlight="C++"]
int min;
if (x < y) {
if (z < x) {
min = z;
} else {
min = x;
} else {
if (z < y) {
min = z;
} else {
min = y;
}
cout << "The smallest number is " << min << endl;
[/highlight]
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
I would use some logic operators there to test your results. Because this code will appear to work but under certain circumstances could give false reads.

Something more like this is what I would do.


if ( x >= y && y <= z )

{ cout << "smallest value is: " << y << endln; }


else if ( y >= x && x <= z )

{ cout << "smallest value is: " << x << endln; }


else

{ cout << "smallest value is: " << z << endln; }


Hope this helps.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
When testing a program like this, you want to test for all possible orders:
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1

In addition, you want to check for duplicates:
1,1,2
1,2,1
2,1,1
1,2,2
2,1,2
2,2,1
2,2,2

For each possibility, your code should produce the correct result. Your code fails 3,2,1
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
kiddies

kiddies

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
thanks for you logic

#6
Ajiz Zone

Ajiz Zone

    Newbie

  • Members
  • Pip
  • 4 posts
thanks Masters...
every comment is helping me to make this program perfect....
it just a testing program for me to improve the c++ skill....
Thanks.

#7
Mathematix

Mathematix

    Programmer

  • Members
  • PipPipPipPip
  • 112 posts
You only need two variables!


int main()

{

	int x, y;


	cout << "enter no 1: " << endl;

	cin >> x;


	cout << "enter no 2: " << endl;

	cin >> y;

	if(x > y) // Is y smaller than x?

		x = y;


	cout << "enter no 3: " << endl;

	cin >> y;

	if(x > y) // Is y smaller than the current value of x?

		x = y;


	cout << "The smallest valuse is: " << x << endl;


	return 0;

}


:)