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 391,944 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 3,889 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: 2947 | Replies: 11
Reply
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Sorting and Searching to get rid of Duplicate Numbers

  #1  
Dec 15th, 2004
I am having a problem sorting my numbers after my binary search. Can you tell me what is wrong with my code?

#include <iostream>
#include <cstdlib>
using namespace std;

void pick_three(int p3arr[]);
void pick_four(int p4arr[]);
void lotto(int lottoarr[]);
void selection_sort(int a[], int size);
int lottobsearch(int a[], int size);

int main()
{
	int choice;
	int p3arr[3];
	int p4arr[4];
	int lottoarr[6];

	cout << "Welcome to the Maryland Lottery!\n";
	do
	{
		cout << endl
			 << "Choose 1 for Pick 3.\n"
			 << "Choose 2 for Pick 4.\n"
			 << "Choose 3 for Lotto.\n"
			 << "Choose 4 for Mega Millions.\n"
			 << "Choose 5 to Exit.\n"
			 << "Enter choice and press Enter: ";
		cin >> choice;

		switch(choice)
		{
			case 1:
				pick_three(p3arr);
				break;
			case 2:
				pick_four(p4arr);
				break;
			case 3:
				lotto(lottoarr);
				break;
			case 5:
				cout << "End of Maryland Lottery.\n";
				break;
			default:
				cout << "Not a valid choice.\n"
					 << "Choose again.\n";
		}
	}
	while(choice != 5);
	return 0;
}

void pick_three(int p3arr[])
{
	const int arrsize = 3;
	p3arr[arrsize];
	int rand_int = 0;
	int tickets;

	cout << "How many tickets?:\n";
	cin >> tickets;
	cout << endl;

		if(tickets >=1 && tickets <=5)
		{
			while(tickets != 0)
			{	
				for(int i=0; i<3; i++)
				{
					rand_int = rand()%10;
					p3arr[i] = rand_int;
					selection_sort(p3arr, arrsize);
					cout << p3arr[i] << endl;
				}
				cout << endl;
				tickets = tickets - 1;
			}
		}
		else
		{
			cout << "Ticket number too large.\n";
		}
	
	return;
}

void pick_four(int p4arr[])
{
	const int arrsize = 4;
	p4arr[arrsize];
	int rand_int = 0;
	int tickets;

	cout << "How many tickets?:\n";
	cin >> tickets;
	cout << endl;

		if(tickets >=1 && tickets <=5)
		{
			while(tickets != 0)
			{	
				for(int i=0; i<4; i++)
				{
					rand_int = rand()%10;
					p4arr[i] = rand_int;
					selection_sort(p4arr, arrsize);
					cout << p4arr[i] << endl;
				}
				cout << endl;
				tickets = tickets - 1;
			}
		}
		else
		{
			cout << "Ticket number too large.\n";
		}
	
	return;
}
void lotto(int lottoarr[])
{
	const int arrsize = 6;
	lottoarr[arrsize];
	int rand_int = 0;
	int tickets;

	cout << "How many tickets?:\n";
	cin >> tickets;
	cout << endl;

		if(tickets >=1 && tickets <=5)
		{
			while(tickets != 0)
			{	
				for(int i=0; i<6; i++)
				{
					rand_int = rand()%49+1;
					lottoarr[i] = rand_int;
					selection_sort(lottoarr, arrsize);
					lottobsearch(lottoarr, arrsize);
					cout << lottoarr[i] << endl;
				}
				cout << endl;
				tickets = tickets - 1;
			}
		}
		else
		{
			cout << "Ticket number too large.\n";
		}
	
	return;
}
void selection_sort(int a[], int size)
{
	int temp, i, j;

	for(i=0; i<size; i++)
		for(j=i+1; j<size; j++)
		{
			if (a[i] > a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	return;
}
int lottobsearch(int a[], int size)
{
	for ( int i = 0; i < size; ++i)
	{
		a[i] = rand()%49+1;
        for (int j = 0; j < i; ++j)
 		{
			if (a[i] == a[j])
 			{
				a[j] = a[i];
 			}
		}
	}
	return 0;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Tucson, Az
Posts: 107
Reputation: prog-bman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #2  
Dec 15th, 2004
Reply With Quote  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #3  
Dec 15th, 2004
thanks. i didnt' know.
Reply With Quote  
Join Date: Nov 2004
Location: Tucson, Az
Posts: 107
Reputation: prog-bman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #4  
Dec 15th, 2004
What do you mean by you are having trouble sorting your numbers after a binary search, the basis of a binary search is that the numbers are already sorted.
Reply With Quote  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #5  
Dec 15th, 2004
Originally Posted by prog-bman
What do you mean by you are having trouble sorting your numbers after a binary search, the basis of a binary search is that the numbers are already sorted.

They are already suppose to be sorted then i got in for the binary search but when my output comes up they are not sorted.
Reply With Quote  
Join Date: Nov 2004
Location: Tucson, Az
Posts: 107
Reputation: prog-bman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #6  
Dec 15th, 2004
Well first off in all you lotto statements you have a lottoarr[size]; which is doing absoulty nothing. I recommend that you turn on all your compiler's warnings that will help out with things like that probably complier options -> commands to compiler add the switch -Wall.
As for your problem
int lottobsearch(int a[], int size)
{
	for ( int i = 0; i < size; ++i)
	{
		//a[i] = rand()%49+1; here is your problem
        for (int j = 0; j < i; ++j)
 		{
			if (a[i] == a[j])
 			{
				a[j] = a[i];
 			}
		}
	}
	return 0;
}
Your assigning a new value to your array during your binary search which is giving you unsorted numbers. Also another thing I noticed is that you get negative numbers sometimes also
Reply With Quote  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #7  
Dec 15th, 2004
Originally Posted by prog-bman
Well first off in all you lotto statements you have a lottoarr[size]; which is doing absoulty nothing. I recommend that you turn on all your compiler's warnings that will help out with things like that probably complier options -> commands to compiler add the switch -Wall.
As for your problem
int lottobsearch(int a[], int size)
{
	for ( int i = 0; i < size; ++i)
	{
		//a[i] = rand()%49+1; here is your problem
        for (int j = 0; j < i; ++j)
 		{
			if (a[i] == a[j])
 			{
				a[j] = a[i];
 			}
		}
	}
	return 0;
}
Your assigning a new value to your array during your binary search which is giving you unsorted numbers. Also another thing I noticed is that you get negative numbers sometimes also


I never received a negative number, they might be my logic. What is the new value? I am trying to see what you are saying, but I don't see it at all.
Reply With Quote  
Join Date: Nov 2004
Location: Tucson, Az
Posts: 107
Reputation: prog-bman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #8  
Dec 15th, 2004
Ok first you are sorting a list of numbers than when you call your search(which is really flawed btw) everytime you loop through your setting a new value to a[i] which is really lottoarr[i].
Reply With Quote  
Join Date: Nov 2004
Location: Tucson, Az
Posts: 107
Reputation: prog-bman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #9  
Dec 15th, 2004
Heres a little program I whipped together for you check it out hope it will help you.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


void Sort(int numbers[], int size)
{
  int i, j;
  int min, temp;
  //selection sort
  for (i = 0; i < size-1; i++)
  {
    
    min = i;
    
    for (j = i+1; j < size; j++)
    {
      
      if (numbers[j] < numbers[min])
      {
        
        min = j;
        
      }
        
    }
    
    temp = numbers[i];
    numbers[i] = numbers[min];
    numbers[min] = temp;
    
  }
  
}

void checkForDuplicate(int numbers[], int size)
{
  
  int i, j; 
  //probably someway to make this more efficent but you get the idea
  for(i = 0; i < size; i++)
  {
     for(j = i + 1; j < size; j++)
     {
       
       if(numbers[i] == numbers[j])
       {
         
         numbers[j] = rand() % 10;         
         
       }
       
     }
     
   }
   
   
   
}

int main(void)
{
  int i;
  const int size = 3;
  int array[size];
  srand((unsigned int)time(NULL));//makes the numbers based on time
  
  for(i = 0; i < size; i++)
  {
    
    array[i] = rand() % 10;
    
  }
  
  checkForDuplicate(array,size);//call check for duplicate before you sort them
  //so you don't have to sort twice
  Sort(array,size);
  
  for(i = 0; i < size; i++)
  {
            
    cout<<array[i]<<endl;
    
  }
  
  cin.get();
  
  return 0;
  
}
Reply With Quote  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

  #10  
Dec 15th, 2004
thanks! that helped me very much with my sort. I really appreciate it.
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

Other Threads in the C++ Forum

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