Solved

Could someone give me an idea of how this is supose to work?

Posted on 2001-09-07
6
484 Views
Last Modified: 2012-06-27
I have this programming problem
Write a program that will measure the run time of different sorting algorithms. The sorting algorithms MUST include Insertion Sort, Quick Sort, Radix sort, and any other sort method of your choice excluding any variations of the selection sort, bubble sort and shell sort. You MUST use at least one enumeration data type and pointers to operate arrays in your program.

This is the part I do not understand:

You MUST use at least one enumeration data type and pointers to operate arrays in your program.

Does this mean the I would just fill a enum and input it into the sort like
enum Numberstobesorted{1,4,7,8,5,44,55,88,90} and then just input it into the sort algorithm? I also don't understtand "pointers to operate arrays in your program"
could someone explain that or show me a example.
0
Comment
Question by:awjackin35
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:awjackin35
ID: 6466211
Here is my client file so far. What would I use the enumeration type for and am I useing the pointes correctly for the problem.
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<cstdio>
#include"sortImplementation.cpp"
//void CopyArray( int p[],  int q[], int vec);
const int vSize = 8;
using namespace std;





void CopyArray( int p[],  int q[], int vec);//this is the function call.

int main()
{

     
     int vec[vSize]={100,200,300,400,500,600,700,800};

     clock_t start,finish;
     clock_t startAll;

     int loBound=0;
     int hiBound=vSize-1;

     quick qq;
    double duration;
     int j=0;
     int *p, *q;//p and q are the pointers to arrays.
     int iSize = 0;

cout<<"     "<<"Bubble Sort"<<"    "<<"Insertion"<<"    "<<"Quick"<<"    "<<"Selection"<<endl;




for(int i=0; i<vSize; i++)
{
     iSize = vec[i];
     p = new int[iSize];
     q = new int[iSize];
     for(int k = 0; k<vec[i]; k++)
     {
          p[k]=rand();
     }


     for(int ii=1; ii<=4;ii++)
     {
          memcpy(q, p, iSize*4);
          start=clock();
         
         
          switch (ii)
          {

          case 1:
               hiBound = iSize - 1;
               qq.Quicksort(q,loBound,hiBound);
               break;
          case 2:
               qq.BubbleSort(q, iSize );
               break;
          case 3:
               qq.SelectionSort(q, iSize);
               break;
          case 4:
               qq.Insertionsort(q, iSize);
               break;
          default: cout<<"Error"; break;
          }//end of switch function.

          finish=clock();
          duration=(double)(finish-start)/CLOCKS_PER_SEC;
          cout<<"            "<<setw(4)<<duration;
         
     }
          //Delete the array for the next sorting function.
          cout<<endl;
          delete[] p;
          delete[] q;
         
     }
          //cout<<duration<<endl;
          finish=clock();
          duration=(double)(finish-start)/CLOCKS_PER_SEC;
          cout<<"Total Duration"<<duration<<endl;
     
     





return 0;
}//end of main
//////////////////////////////////////////////////////////////////////
void CopyArray(int p[],int q[],int vec)//this is the function call.
{
     int i;

     for(i=0;i<vSize;i++)
p[i]=q[i];
}
0
 
LVL 1

Expert Comment

by:glebspy
ID: 6466572
Hi,
 I reckon "you must use at least one enum type" is expecting you to use an enum to switch on the sort type,
ie  

enum Sort_Type{BUBBLE_SORT_TYPE,SHELL_SORT_TYPE,
QUICK_SORT_TYPE,...}sort_Type;

then later

switch(Sort_Type){
case BUBBLE_SORT_TYPE:
...
}

As for pointers I think that just means, use pointer types rather than array types, so
void CopyArray(int*p,int*q,int vec)

and not int p[] etc.
0
 

Author Comment

by:awjackin35
ID: 6467367
So why is my switch not working. I am getting a error here
switch (Sort_Type)
error:'Sort_Type' : illegal use of this type as an expression


#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<cstdio>
#include<fstream>
#include"sortImplementation.cpp"
//void CopyArray( int p[],  int q[], int vec);
const int vSize = 3;
using namespace std;





void CopyArray( int p[],  int q[], int vec);//this is the function call.

int main()
{


     const int Size = 10;    
     int vec[vSize]={6250,12500,25000};//,50000//,100000,200000,400000};
     enum Sort_Type{Quicksort,BubbleSort,SelectionSort,Insertionsort};

     clock_t start,finish;
     clock_t startAll;

     int loBound=0;
     int hiBound=vSize-1;

     quick qq;
     int Numberstobesorted[] = {3,7,8,9,70,45,12,5,66,87};
     int Numbers=0;
    double duration;
     int j=0;
     int *p, *q;//p and q are the pointers to arrays.
     int iSize = 0;
     int i=0;
     int* a;
//          Size = Numberstobesorted[i];
     a = new int[Size];

     ifstream inFile;
    ofstream outFile;

     inFile.open("input1.txt");
    outFile.open("output1.txt");

          for(i =0; i<Size; i++)
     {
          qq.BubbleSort(a,iSize );

         


     }


     for(i =0; i<Size; i++)
     {
          qq.BubbleSort(a,iSize );

          outFile<<Numberstobesorted[i]<<" ";


     }



cout<<"     "<<"Bubble Sort"<<"    "<<"Insertion"<<"    "<<"Quick"<<"    "<<"Selection"<<endl;


//return 0;
//}

for(i=0; i<vSize; i++)
{
     iSize = vec[i];
     p = new int[iSize];
     q = new int[iSize];
     for(int k = 0; k<vec[i]; k++)
     {
          p[k]=rand();
     }


     for(int ii=0; ii<=3;ii++)
     {
          memcpy(q, p, iSize*4);
          start=clock();
         
         
          switch (Sort_Type)
          {

          case 0:
               hiBound = iSize - 1;
               qq.Quicksort(q,loBound,hiBound);
               break;
          case 1:
               qq.BubbleSort(q, iSize );
               break;
          case 2:
               qq.SelectionSort(q, iSize);
               break;
          case 3:
               qq.Insertionsort(q, iSize);
               break;
          default: cout<<"Error"; break;
          }//end of switch function.

          finish=clock();
          duration=(double)(finish-start)/CLOCKS_PER_SEC;
          cout<<"            "<<setw(4)<<duration;
         
     }
          //Delete the array for the next sorting function.
          cout<<endl;
          delete[] p;
          delete[] q;
         
     }
          //cout<<duration<<endl;
          finish=clock();
          duration=(double)(finish-start)/CLOCKS_PER_SEC;
          cout<<"Total Duration"<<duration<<endl;
     
     





return 0;
}//end of main
//////////////////////////////////////////////////////////////////////
//void CopyArray(int p[],int q[],int vec)//this is the function call.
//{
//     int i;

//     for(i=0;i<vSize;i++)
//p[i]=q[i];
//}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:daniehol
ID: 6467844
Dude, why don't you ask your teacher
0
 
LVL 1

Accepted Solution

by:
glebspy earned 40 total points
ID: 6467915
Be careful!

This is what I wrote:

enum Sort_Type{BUBBLE_SORT_TYPE,SHELL_SORT_TYPE,
           QUICK_SORT_TYPE,...}sort_Type;

There is a "Sort_Type" at the beginning, with a capital 'S'.

There is a "sort_Type" at the end, with a small 's'.

Sorry that this is confusing. It's just one of the conventional ways of doing this:

Sort_Type (capital) is like a typedef.

sort_Type (small) is the variable,
  which has type "enum Sort_Type" (capital).



Of course you have to switch on the variable, not on its type, so

switch(sort_Type){  //small 's'




0
 

Author Comment

by:awjackin35
ID: 6467929
I got it working, Here is the code: I am working on the for loops know.
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<cstdio>
#include<fstream>
#include"sortImplementation.cpp"
//void CopyArray( int p[],  int q[], int vec);
const int vSize = 3;
using namespace std;





void CopyArray( int p[],  int q[], int vec);//this is the function call.

int main()
{

int Sort;
     enum {Quicksort,BubbleSort,SelectionSort,Insertionsort}
    Sort_Type;
     
     Sort= Quicksort;
     
     //Sort_Type

     const int Size = 10;    
     int vec[vSize]={6250,12500,25000};//,50000//,100000,200000,400000};

     clock_t start,finish;
     clock_t startAll;

     int loBound=0;
     int hiBound=vSize-1;

     quick qq;
     int Numberstobesorted[] = {3,7,8,9,70,45,12,5,66,87};
     int Numberssorted[Size];
     int Numbers=0;
    double duration;
     int l;
     int j=0;
     int *p, *q;//p and q are the pointers to arrays.
     int iSize = 0;
     int i=0;
     int* a;
//          Size = Numberstobesorted[i];
     a = new int[Size];

     ifstream inFile;
    ofstream outFile;

     inFile.open("input1.txt");
    outFile.open("output1.txt");

          for(i =0; i<Size; i++)
          {
               inFile>>Numberstobesorted[Size];
          qq.BubbleSort(a,iSize );

         
          }
     


     for(i =0; i<Size; i++)
     {
         
outFile<<Numberssorted[Size]<<" ";

     }
         
         

cout<<"     "<<"Bubble Sort"<<"    "<<"Insertion"<<"    "<<"Quick"<<"    "<<"Selection"<<endl;


//return 0;
//}

for(i=0; i<vSize; i++)
{
     iSize = vec[i];
     p = new int[iSize];
     q = new int[iSize];
     for(int k = 0; k<vec[i]; k++)
     {
          p[k]=rand();
     }


     for(int ii=0; ii<=3;ii++)
     {
          memcpy(q, p, iSize*4);
          start=clock();
         
         
          switch (Sort)
          {

          case 0:
               hiBound = iSize - 1;
               qq.Quicksort(q,loBound,hiBound);
               break;
          case 1:
               qq.BubbleSort(q, iSize );
               break;
          case 2:
               qq.SelectionSort(q, iSize);
               break;
          case 3:
               qq.Insertionsort(q, iSize);
               break;
          //case 4:
          //     qq.radixLSD(a, l, iSize);
               //break;
          default: cout<<"Error"; break;
          }//end of switch function.

          finish=clock();
          duration=(double)(finish-start)/CLOCKS_PER_SEC;
          cout<<"            "<<setw(4)<<duration;
         
     }
          //Delete the array for the next sorting function.
          cout<<endl;
          delete[] p;
          delete[] q;
         
     }
          //cout<<duration<<endl;
          finish=clock();
          duration=(double)(finish-start)/CLOCKS_PER_SEC;
          cout<<"Total Duration"<<duration<<endl;
     
     





return 0;
}//end of main
//////////////////////////////////////////////////////////////////////
//void CopyArray(int p[],int q[],int vec)//this is the function call.
//{
//     int i;

//     for(i=0;i<vSize;i++)
//p[i]=q[i];
//}
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question