Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Find The Smallest No


  • Please log in to reply
6 replies to this topic

#1 Ajiz Zone

Ajiz Zone

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 11 July 2009 - 12:53 AM

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

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;
}


  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2,918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 11 July 2009 - 01:17 AM

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:


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;

  • 0

I'm a System developer at Redpill Linpro mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 SolidState

SolidState

    CC Regular

  • Member
  • PipPipPip
  • 37 posts

Posted 11 July 2009 - 01:20 AM

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

#4 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 17,156 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 July 2009 - 01:49 AM

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
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 kiddies

kiddies

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 129 posts

Posted 11 July 2009 - 03:29 PM

thanks for you logic
  • 0

#6 Ajiz Zone

Ajiz Zone

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 11 July 2009 - 06:32 PM

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

#7 Mathematix

Mathematix

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 103 posts

Posted 12 July 2009 - 07:48 AM

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;
}

:)
  • 0




Powered by binpress