User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,627 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,378 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 330 | Replies: 8
Reply
Join Date: Nov 2007
Posts: 46
Reputation: manzoor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
manzoor manzoor is offline Offline
Light Poster

smallest largest

  #1  
May 11th, 2008
how do you find the smallest and largest number of 3 numbers

without using logical operators and else statement only if and relational operator ?

is there any other way without checking every variable against each other using if statements ?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,698
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 25
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: smallest largest

  #2  
May 11th, 2008
You could always have them in an array and use min_element and max_element
From site:
  1. // min_element/max_element
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. // snip
  7.  
  8. int main () {
  9. int myints[] = {3,7,2,5,6,4,9};
  10.  
  11. // using default comparison:
  12. cout << "The smallest element is " << *min_element(myints,myints+7) << endl;
  13. cout << "The largest element is " << *max_element(myints,myints+7) << endl;
  14.  
  15. // Snip
  16.  
  17. return 0;
  18. }
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Nov 2007
Posts: 46
Reputation: manzoor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
manzoor manzoor is offline Offline
Light Poster

Re: smallest largest

  #3  
May 11th, 2008
i know there are other ways but can you tell me is there any other way to find it using relational operators and if statement only not else statement without checking each number in if statement against each other ?
Reply With Quote  
Join Date: May 2008
Posts: 84
Reputation: n1337 is on a distinguished road 
Rep Power: 1
Solved Threads: 7
n1337 n1337 is offline Offline
Junior Poster in Training

Re: smallest largest

  #4  
May 11th, 2008
Errr...well, if you are dealing with positive integers only, you could construct a loop, then on each iteration through the loop, subtract one from each of the numbers, and the first one to reach 0 is the smallest; the last one to reach 0 is the largest...

hahaha it models the recursive definition of a natural number, but don't do this...lol
Reply With Quote  
Join Date: Apr 2008
Location: Newcastle, UK.
Posts: 170
Reputation: Black Magic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: smallest largest

  #5  
May 12th, 2008
I guess you could use "Bubble-Sort"
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote  
Join Date: Nov 2007
Posts: 46
Reputation: manzoor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
manzoor manzoor is offline Offline
Light Poster

Re: smallest largest

  #6  
May 12th, 2008
n1337 your solution might fit :p thanks
everyone else
Reply With Quote  
Join Date: May 2008
Posts: 348
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 3
Solved Threads: 59
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: smallest largest

  #7  
May 12th, 2008
> using relational operators and if statement only not else statement without
> checking each number in if statement against each other
You've put in too many restrictions. You have to test each number against the other at some point to find the maximum. Using only relational operators and if without else can be done, but as soon as you said "without checking each number in if statement against each other", you make the problem impossible to solve. This is what Edward thought you wanted, but now I'm not sure:
int MaxOf3(int a, int b, int c)
{
  if (a >= b) {
    if (a >= c)
      return a;

    if (c > a)
      return c;
  }

  if (b > a) {
    if (b > c)
      return b;

    if (c > b)
      return c;
  }
}
Last edited by Radical Edward : May 12th, 2008 at 8:08 am.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 347
Reputation: williamhemsworth has a spectacular aura about williamhemsworth has a spectacular aura about williamhemsworth has a spectacular aura about 
Rep Power: 3
Solved Threads: 36
williamhemsworth's Avatar
williamhemsworth williamhemsworth is offline Offline
Posting Whiz

Re: smallest largest

  #8  
May 12th, 2008
Heres a way of doing it without < > <= >=

#include<iostream>
using namespace std;

int min3(int a, int b, int c) {
	for (int i = 0;; i++) {
		if (i == a) return a;
		if (i == b) return b;
		if (i == c) return c;
	}
	return 0;
}

int main() {
	cout << min3(77,24,879);  // output 24
	cin.ignore();
	return 0;
}
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 347
Reputation: williamhemsworth has a spectacular aura about williamhemsworth has a spectacular aura about williamhemsworth has a spectacular aura about 
Rep Power: 3
Solved Threads: 36
williamhemsworth's Avatar
williamhemsworth williamhemsworth is offline Offline
Posting Whiz

Re: smallest largest

  #9  
May 12th, 2008
Or without any logical operators:

int min3(int a, int b, int c) {
	for (int i = 0;; i++) {
		if (!(a - i)) return a;
		if (!(b - i)) return b;
		if (!(c - i)) return c;
	}
	return 0;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC