Simple Sort 2D Char & Binary Serch C++ Part 2

Ello Konrad Zuses of the of the C++ forums I have another issue for you guys to easily solve and simultaneously prove my skill is subpar. Simply put this program creates a bunch of random words (not real word) of a random length (4-7) sorts them, checks for duplicates, and then allows the user to lookup a word in the array. Im using Bloodshed Dev C++ on Wyndoze and/or Anjuta on Ubuntu depending on my mood. But I digress the issue is I lose a word everytime during the sort and reprint of the array and I dont think its searching for my words when I do a lookup. My professor seems cool but acts like a dick so help is hard.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
using namespace std;
#include<cstring>
#include<stdlib.h>


//RANDOM WORD GENERATOR DEFINITION
void randword(char arrayone[][7],int num){int len;
	for(int i = 0 ; i < num ; i++){
	//DETERMINES THE LENGTH OF THE WORD
		len = rand()3 + 4;
			for(int j = 0; j<= len ; j++){
				arrayone[i][j] = rand()% 26 + 'a';
					if (j == len)
						arrayone[i][j] ='\0';
		}
	}
}
//PRINT ARRAY FUNCTION DEFINITION 
void PrintArray(char Array[][7], int num)
{
	for( int i = 0; i < num ; i++)
	cout << Array[i]<< endl;
}
//SORTDATA FUNCTION DEFINITION 
void SortArray(char arraythree[][7],int num)
{char temp[7];
	for(int i = 0; i< num - 1 ; i++)
	for( int j = 0 ; j < num ; j++)
	{ if(strcmp(arraythree[j],arraythree[j+1]) == 1)
	{	strcpy(temp, arraythree[j+1]);
		strcpy(arraythree[j+1],arraythree[j]);
		strcpy(arraythree[j],temp);
		}
	}
}

void dub_search(char array5[][7], int num ){int counter=0;
	for( int j = 0 ; j < num ; j++)
	{ if(strcmp(array5[j],array5[j+1]) == 0){counter++;
		cout <<"there is a duplicate in word " << j << ".Replace it now --->" << endl;
		cin >> array5[j-1];
			PrintArray(array5,num);}
	}
	if(counter == 0)
	cout << "\n\nThere is no duplications that have been found." <<endl;}

void binSearch(char arrayfour[][7],int num, char searchkey[]){
	int first = 0;
	int last = num-1;
	int mid;
	while (first <= last){
		mid = (first + last) / 2;
			if( searchkey > arrayfour[mid])
				first = mid + 1;
			else if(searchkey < arrayfour[mid])
				last = mid - 1;
			else if (searchkey==arrayfour[mid])
				cout << "The word " << searchkey <<" you are searching for is in location number " << mid << " of the array." <<endl;
	}
}


//MAIN FUNCTION DEFINITIONS
int main()
{int wordnum;
	cout << "How many words do you want to generate?";
	cin >> wordnum;
	char WordArray[1000][7];
	char key[7];
	randword(WordArray, wordnum);
		cout <<"\n\n Thank you. \n Here are your words.\n"<<endl;
	PrintArray(WordArray,wordnum);
	cout << "\n\n\n Here they are after being sorted.\n" <<endl;
	SortArray(WordArray, wordnum);
	PrintArray(WordArray,wordnum);
	dub_search(WordArray, wordnum);
	cout << "\n\n\n Give me a word to search for?";
		cin >> key;
	binSearch(WordArray, wordnum, key);
    char ans[1];
    printf("press enter to exit.");
    cin >>ans;
return 0;}


Help I got more stuff for you coding Samaritans if you desire.

Domo
(Bubble sort)+(binary string copy swap)=waste.

Line 11: Missing modulo operator.
Line 29: Should be j < num-1
Line 30: Should be strcmp(...)>0
Last edited on
Thanx for the help with the bubble sort that was simply overlooked code which is the difference between being employed and unemployed. Does anybody have any suggestions as to why my binary search never seems to find anything. This is the funct definition below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void binSearch(char arrayfour[][7],int num, char searchkey[]){
	int first = 0;
	int last = num-1;
	int mid;
	while (first <= last){
		mid = (first + last) / 2;
			if( searchkey > arrayfour[mid])
				first = mid + 1;
			else if(searchkey < arrayfour[mid])
				last = mid - 1;
			else if (searchkey==arrayfour[mid])
				cout << "The word " << searchkey <<" you are searching for is in location number " << mid << " of the array." <<endl;
	}
}
Topic archived. No new replies allowed.