Hye there..
I just want some ideas to make my program better..
Below is the code. Any comment or recommendation are appreciated. :)

/*
* Name: Isihan dan Carian [Sorting and Searching]
* version: 4.0.1
* Copyright ©
* Support: http://tempek.net78.net
* Updates: August 27, 2008
* Compiler : Dev-C++ 4.9.9.2
*/

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void random(int []);
void mergesorting(int [], int ,int );
void combine(int [], int , int ,int );
int const SIZE=100;
int search(int , int []);
int binary(int , int []);


int main()
{

    int num[SIZE],number,NUMBER,ans;
    char j;
    
    random(num);
    mergesorting(num,0,99);
    
    system("color 3");
    cout << "\n\n";
    
    cout << "100 integers ( Descending order ) : " << endl << endl;
    
    for(int index=0; index<100; index++)
    {
      cout << "Index " << index << "  : " << num[index] << endl;
    }
    
    do
    {
        cout << "\nEnter a number which you want to search from sorted list : ";
        cin >> NUMBER;
        cout << "\nEnter 1 for SEQUENTIAL SEARCH or 2 for BINARY SEARCH : " ;
        cin >> number;
    
        switch(number)
        {
             case 1 : ans=search(NUMBER,num);
            
             if(ans==-1)
                       cout << endl << endl << NUMBER << " is not in the sorted list." ;
             else
                       cout<< endl << endl << NUMBER <<" is in the sorted list which located in index " << ans;
            
             break;
             case 2 : ans=binary(NUMBER,num);
            
             if(ans==-1)
                        cout << endl << endl << NUMBER << " is not in the sorted list." ;
             else
                        cout<< endl << endl <<NUMBER <<" is in the sorted list which located in index " << ans;
            
             break;
         }
    
    cout <<"\n\n\n\nDo you want to continue (Y/N)? : ";
    cin >> j;
    
    if(j=='n' || j=='N')
              return 0;
    
    }while(j=='y' || j=='Y');

getchar();
return 0;
}


/* Randomize the number between 101-999 */
void random(int array[])
{
    srand((unsigned)time(0));
    int random_integer;
    int lowest=101, highest=999;
    int range=(highest-lowest)+1;

    cout <<"Unsorted 100 random integers : " << endl << endl;

    for(int x=0; x<100; x++)
    {
        random_integer = (range*rand()/(RAND_MAX + 1))+lowest;
        array[x] = random_integer;
        cout << "Index " << x << "  : " << array[x] <<endl;
    }
}



/* Merge sort funtion */
void mergesorting(int T[], int first,int last)
{
        int mid;
         if (first<last)
       {     mid = (first+last)/2;
              mergesorting(T, first, mid);
              mergesorting(T, mid+1, last);
              combine(T, first,mid, last );
         }
}

void combine(int T[], int first, int mid,int last)
{
     int ifirst, iTfana, imid, k;
     int Tfana[SIZE];
     ifirst=first;
     iTfana=first;
     imid=mid+1;

     while((ifirst<=mid) &&(imid<=last))
     {
        if (T[ifirst]>=T[imid])
        {   
               Tfana[iTfana]= T[ifirst];
               ++ifirst;
        }
        else
        {   
            Tfana[iTfana]= T[imid];
            ++imid;
        }
           ++iTfana;
     }

    if (ifirst>mid)
    {
       for (k=imid; k<= last; k++)
       {  
           Tfana[iTfana] = T[k];
           ++iTfana;
       }
    }
    
    else
    {
       for (k=ifirst; k<= mid; k++)
       {
           Tfana[iTfana] = T[k];
           ++iTfana;
        }
    }
    
    for (k=first; k<= last; k++) {
           T[k]=Tfana[k];
    }
    
    }




/*Sequantial search funtion*/
int search(int search_key , int NUM[])
{

int p;
int index =-1;

 for ( p = 0; p < SIZE; p++ )
  {
     if (search_key > NUM[p] )
     break;

     else if (search_key == NUM[p])
	{
           index = p;
           break;
	}
   }

system("CLS");
cout << endl << "Number of tests : " << p;
return index;
}



/*Binary search function*/
int binary( int  search_key, int NUM[] )
{
    bool found = false;
    int index = -1,a=1;
    int MIDDLE;
    int LEFT = 0;
    int RIGHT = SIZE-1;
    
    while (( LEFT<= RIGHT ) && (!found))
   	{
    	a++;
    	MIDDLE = (LEFT + RIGHT ) / 2;
    
    	if ( NUM[MIDDLE] == search_key)
   		{
    		    found = true;
                index = MIDDLE;
   		}
        else if (NUM[MIDDLE] > search_key)
               LEFT= MIDDLE+ 1;
    
        else
               RIGHT= MIDDLE - 1;
    
        }

system("CLS");
cout << endl << "Number of tests : " << a;
return index;

}

Recommended Answers

All 3 Replies

General comments:
1. The indentation needs work.
2. Lower level functions should just return results. Maybe I just want to search, WITHOUT print the result AND clear the screen.

> void random(int []);
Well named variables in the prototype will help document the API. What we've learnt so far is nothing. OK, big deal, it's an array. And....

Say
void random( int arrayToFill[] );

> srand((unsigned)time(0));
This should be called ONCE, in main().
Calling it multiple times adds nothing. Calling it in a loop would be a disaster.

> for(int x=0; x<100; x++)
And all the trouble you went to to declare SIZE.
It would be a lot better if you passed size as a parameter to all these functions receiving an array.

The entire "do..while" in main could be a separate function. main is starting to look busy.

What about this one? Any comments?
Maybe some ideas to make some modifications on this? :)

/*
* Name: Isihan dan Carian [Sorting and Searching]
* version: 4.0.1
* Copyright ©
* Support: http://tempek.net78.net
* Updates: August 27, 2008
* Compiler : Dev-C++ 4.9.9.2
*/

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int const SIZE=100;
int search(int , int []);
int binary(int , int []);
void random(int arrayToFill[]);
void mergesorting(int [], int ,int );
void combine(int [], int , int ,int );
int doWhile(int num[]);



int main()
{
	int num[SIZE];
	random(num);
	mergesorting(num,0,99);
	doWhile(num);
	
	

	getchar();
	return 0;
}


int doWhile(int num[])
{
	char j;
	int NUMBER,number,ans;
	
	do
	{
		cout << "\n\n100 integers ( Descending order ) : " << endl << endl;

		for(int index=0; index<SIZE; index++)
			cout << "Index " << index << "  : " << num[index] << endl;
	
		cout << "\nEnter a number which you want to search from sorted list : ";
		cin >> NUMBER;
		cout << "\nEnter 1 for SEQUENTIAL SEARCH or 2 for BINARY SEARCH : " ;
		cin >> number;

		switch(number)
		{
		case 1 : ans=search(NUMBER,num);
	    
			if(ans==-1)
				cout << endl << endl << NUMBER << " is not in the sorted list." ;
			else
				cout<< endl << endl << NUMBER <<" is in the sorted list which located in index " << ans;
			break;

		case 2 : ans=binary(NUMBER,num);

			if(ans==-1)
				cout << endl << endl << NUMBER << " is not in the sorted list." ;
			else
				cout<< endl << endl <<NUMBER <<" is in the sorted list which located in index " << ans;
			 break;
		 }
		
		cout <<"\n\n\n\nDo you want to continue (Y/N)? : ";
		cin >> j;

		if(j=='n' || j=='N')
			return 0;

	}while(j=='y' || j=='Y');
}
	

/* Randomize the number between 101-999 */
void random(int array[])
{
	srand((unsigned)time(0));
	int random_integer;
	int lowest=101, highest=999;
	int range=(highest-lowest)+1;

	cout <<"Unsorted 100 random integers : " << endl << endl;

	for(int x=0; x<SIZE; x++)
	{
		random_integer = (range*rand()/(RAND_MAX + 1))+lowest;
		array[x] = random_integer;
		cout << "Index " << x << "  : " << array[x] <<endl;
	}

}



/* Merge sort funtion */
void mergesorting(int T[], int first,int last)
{
	int mid;
	if (first<last)
	{
		mid = (first+last)/2;
		mergesorting(T, first, mid);
		mergesorting(T, mid+1, last);
		combine(T, first,mid, last );
	}
}

void combine(int T[], int first, int mid,int last)
{
	int ifirst, iTfana, imid, k;
	int Tfana[SIZE];
	ifirst=first;
	iTfana=first;
	imid=mid+1;

	while((ifirst<=mid) &&(imid<=last))
	{
		if (T[ifirst]>=T[imid])
		{   
			Tfana[iTfana]= T[ifirst];
			++ifirst;
		}
		else
		{   
			Tfana[iTfana]= T[imid];
			++imid;
		}
		++iTfana;
	}


	if (ifirst>mid)
	{
		for (k=imid; k<= last; k++)
		{  
			Tfana[iTfana] = T[k];
			++iTfana;
		}
	}

	else
	{
		for (k=ifirst; k<= mid; k++)
		{
			Tfana[iTfana] = T[k];
			++iTfana;
		}
	}


	for (k=first; k<= last; k++)
		T[k]=Tfana[k];

}




/*Sequantial search funtion*/
int search(int search_key , int NUM[])
{
	int p;
	int index =-1;

	for ( p = 0; p < SIZE; p++ )
	{
		if (search_key > NUM[p] )
			break;

		else if (search_key == NUM[p])
		{
			index = p;
			break;
		}
	}

	system("CLS");
	cout << endl << "Number of tests : " << p;
	return index;
}



/*Binary search function*/
int binary( int  search_key, int NUM[] )
{
	bool found = false;
	int index = -1,a=1;
	int MIDDLE;
	int LEFT = 0;
	int RIGHT = SIZE-1;

	while (( LEFT<= RIGHT ) && (!found))
	{
		a++;
		MIDDLE = (LEFT + RIGHT ) / 2;

		if ( NUM[MIDDLE] == search_key)
		{
			found = true;
			index = MIDDLE;
		}

		else if (NUM[MIDDLE] > search_key)
			LEFT= MIDDLE+ 1;

		else
			RIGHT= MIDDLE - 1;
	}


	system("CLS");
	cout << endl << "Number of tests : " << a;
	return index;

}

Good luck :icon_rolleyes:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.