Quantcast

Maximum PC

It is currently Thu Jul 24, 2014 3:11 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Resolved:sorting variables c++
PostPosted: Wed Oct 15, 2008 12:49 pm 
Klamath
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
  Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 3:42 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 5:51 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
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
  Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 7:01 pm 
Klamath
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;
   cout<<"Please enter five numbers."<<endl;
   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
  Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 4:39 am 
Java Junkie
Java Junkie
User avatar

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


Top
  Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 4:57 am 
Klamath
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
  Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 5:35 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
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
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group