# Maximum PC

 It is currently Sat Apr 18, 2015 11:58 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Resolved:sorting variables c++Posted: Wed Oct 15, 2008 12:49 pm
 Klamath

Joined: Sat Jul 07, 2007 7:50 pm
Posts: 213
Here's my question. Given six int variables that cannot be in an array how do you sort those from smallest to largest. The only way I can think of is to do a god awful amount of if statements i.e.

CPP / C++ / C Code:
if (var1<var2){
temp==var1;
var1==var2;
var2==var1;
}
if(var1<var3){
temp==var1
var1==var3
var3==temp[
}Notice that this will take forever to come up with all the possible combinations. I'm learning arrays but I don't know how to sort them yet.

Last edited by scuzzo on Wed Oct 15, 2008 7:02 pm, edited 1 time in total.

Top

 Post subject: Posted: Wed Oct 15, 2008 3:42 pm
 Million Club - 5 Plus*

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
Couldn't you toss them in an array, sort them, and yank them back out?

Top

 Post subject: Posted: Wed Oct 15, 2008 5:51 pm
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24241
Location: Granite Heaven
smartcat99s wrote:
Couldn't you toss them in an array, sort them, and yank them back out?

You could if your teacher was letting you, but in this case, the goal of the exercise is (shockingly enough) to teach someone to sort an array!

Top

 Post subject: Posted: Wed Oct 15, 2008 7:01 pm
 Klamath

Joined: Sat Jul 07, 2007 7:50 pm
Posts: 213
I think I've resolved it. Here is the assignment:

Write a program that takes five numbers from the user and calls the following functions:
isEqual5() - determines if the 5 numbers are all identical.
isEqual4() - determines if any 4 numbers are identical.
isEqual3() - determines if any 3 numbers are identical.

No sorting is allowed in any of these functions including the main.

All comparisons must be performed within the functions that return answers, true or false.

The input and output must remain in the main function

And here is what I got:
Code:
#include <iostream>
using namespace std;
//prototype functions for each event
bool isEqual5(int,int,int,int,int);
bool isEqual4(int,int,int,int,int);
bool isEqual3(int,int,int,int,int);

int main()
{
//initialize variables
int n1,n2,n3,n4,n5;
bool three,four,five;
cin>>n1>>n2>>n3>>n4>>n5;
//function calls with variable assignments
five=isEqual5(n1,n2,n3,n4,n5);
four=isEqual4(n1,n2,n3,n4,n5);
three=isEqual3(n1,n2,n3,n4,n5);
//output statements for true functions
if (five==1)
cout<<"There are five matching numbers.";
else if (four==1)
cout<<"There are four matching numbers.";
else if (three==1)
cout<<"There are three matching numbers.";

system("pause");
return 0;
}
//comparison function for instance of five equal numbers
bool isEqual5(int x1,int x2, int x3,int x4,int x5){
int a=0;
if(x1==x2||x1==x3||x1==x4||x1==x5)
a=a+1;
if(x2==x1||x2==x3||x2==x4||x2==x5)
a=a+1;
if(x3==x1||x3==x2||x3==x4||x3==x5)
a=a+1;
if(x4==x1||x4==x2||x4==x3||x4==x5)
a=a+1;
if(x5==x1||x5==x2||x5==x3||x5==x4)
a=a+1;
if (a==5)
return true;
else
return false;
}
//comparison function for instance of four equal numbers
bool isEqual4(int x1,int x2, int x3,int x4,int x5){
int a=0;
if(x1==x2||x1==x3||x1==x4||x1==x5)
a=a+1;
if(x2==x1||x2==x3||x2==x4||x2==x5)
a=a+1;
if(x3==x1||x3==x2||x3==x4||x3==x5)
a=a+1;
if(x4==x1||x4==x2||x4==x3||x4==x5)
a=a+1;
if(x5==x1||x5==x2||x5==x3||x5==x4)
a=a+1;
if (a==4)
return true;
else
return false;
}
//comparison function for instance of three equal numbers
bool isEqual3(int x1,int x2, int x3,int x4,int x5){
int a=0;
if(x1==x2||x1==x3||x1==x4||x1==x5)
a=a+1;
if(x2==x1||x2==x3||x2==x4||x2==x5)
a=a+1;
if(x3==x1||x3==x2||x3==x4||x3==x5)
a=a+1;
if(x4==x1||x4==x2||x4==x3||x4==x5)
a=a+1;
if(x5==x1||x5==x2||x5==x3||x5==x4)
a=a+1;
if (a==3)
return true;
else
return false;
}

You think that solves the problem?

Top

 Post subject: Posted: Thu Oct 16, 2008 4:39 am
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24241
Location: Granite Heaven
Did your teacher not teach any sorting algorithms?

Top

 Post subject: Posted: Thu Oct 16, 2008 4:57 am
 Klamath

Joined: Sat Jul 07, 2007 7:50 pm
Posts: 213
He will, we just haven't gotten to that yet. If I was allowed to use an array I would do a bubble sort, but we're not allowed to sort so what are you going to do.

Top

 Post subject: Posted: Thu Oct 16, 2008 5:35 am
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24241
Location: Granite Heaven
Having read the assignment, I realise that I was wrong. You are told not to sort because it is a waste of time.

You need to sit and think about what you are doing before you start writing code. For instance:
Code:
bool isEqual5(int x1, int x2, int x3, int x4, int x5) {
if (x1 != x2) return false;  // if these two numbers are not equal, all five numbers are not equal
else if (x1 != x3) return false;
else if (x1 != x4) return false;
else if (x1 != x5) return false;
else return true;
}

This code was based on the simple fact that if any pair of numbers in the set of 5 are not equal, then the function must return false. By searching for non-equal pairs, we can shortcut the search as soon as we find one.

Next, we are looking for 4 out of 5 positive matches. This means that we are looking for a set of 5 numbers, 4 of which are equal.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 7 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets