Ravi149
January 16th, 2003, 02:37 PM
Hi All,
This is Ravi. I was trying one searching and sorting problem using classes, functions and objects. I am not getting any errors but i am getting some warnings. Please check my problem when you feel free. Please help me in this regard. Please let me know where did i mistake? I'll be appreciated your help in this regard. Take care.Bye for now.
with best regards
Ravi.
Below the problem is :
// Searching and Sorting Array Elements Using Classes and Objects:
#include <iostream.h>
#include <iomanip.h>
class searchSort {
private:
int choice;
int choiceCreate;
int i, size;
public:
void CreateMainMenu();
void IntArray();
void FloatArray();
void DoubleArray();
void CharArray();
void ArraySearch();
void ArraySort();
void ArrayExit();
int get_size();
int linearSearch(int[], int, int);
};
void searchSort::CreateMainMenu()
{
cout << "1 Creates an array depends on its data type" << endl;
cout << "2 Search for the elements of an array" << endl;
cout << "3 Sorting an exsting array" << endl;
cout << "4 Exit from the program" << endl;
int choice;
cin >> choice;
if(choice = 1)
{
int choiceCreate ;
cout << "\t\t Array Menu" << endl;
cout << "\t\t1 create int array" << endl;
cout << "\t\t2 create float array" << endl;
cout << "\t\t3 create double array" << endl;
cout << "\t\t4 create char array" << endl;
cout << "\t\t Enter Selection :\t";
cin >> choiceCreate;
//checking choice bounding
if(choiceCreate <1 || choiceCreate>4)
{
cerr << "Invalid entry \n";
//return 1;
}
}
}
void searchSort::IntArray()
{
if(choiceCreate == 1)
cout << endl << "You entered integer values" << endl;
int *x;
size=get_size();
x = new int[size];
for(i=0; i<size; i++)
{
cout << " Enter Element " << i+1 <<":\t";
cin >> x[i];
}
for(i=0; i<size; i++)
cout << x[i] << "\t ";
cout << "The size of the array is =" << size << endl ;
delete [] x;
}
void searchSort::FloatArray()
{
if(choiceCreate == 2)
cout << endl << "You entered float values" << endl;
float *y;
size=get_size();
y = new float[size];
for(i=0; i<size; i++)
{
cout << " Enter Element " << i+1 <<":\t";
cin >> y[i];
}
for(i=0; i<size; i++)
cout << y[i] << "\t ";
cout << "The size of the array is =" << size << endl ;
delete [] y;
}
void searchSort::DoubleArray()
{
if(choiceCreate == 3)
cout << "You entered double values" << endl;
double *z;
size=get_size();
z = new double[size];
for(i=0; i<size; i++)
{
cout << " Enter Element " << i+1<<":\t";
cin >> z[i];
}
for(i=0; i<size; i++)
cout << z[i] << "\t ";
cout << "The size of the array is =" << size << endl ;
delete [] z;
}
void searchSort::CharArray()
{
if(choiceCreate == 4)
cout << "You entered character values" << endl;
char *c;
size=get_size();
c = new char[size];
for(i=0; i<size; i++)
{
cout << " Enter Element " << i+1<<":\t";
cin >> c[i];
}
for(i=0; i<size; i++)
cout << c[i] << "\t ";
cout << "The size of the array is =" << size << endl ;
delete [] c;
}
void searchSort::ArraySearch()
{
if(choice = 2)
{
int *z;
int size,searchKey;
cout << "Search for an element in the array:" << endl;
size=get_size();
z= new int[size];
for(int i=0; i<size; i++)
{
cout << " Enter Element " << i+1<<":\t";
cin >> z[i];
}
cout << "Enter integer search key:" << endl;
cin >> searchKey;
int element = linearSearch(z, searchKey, size);
if(element >0)
cout << "Found value at index \t" << element << endl;
else
cout << "Value not found" << endl;
}
}
void searchSort::ArraySort()
{
if(choice = 3)
{
int i, *a;
int hold;
int arraySize;
cout << "Enter the array size:" << endl;
cin >> arraySize;
cout << "Enter elements for sorting" << endl;
a = new int[arraySize];
for(i=0; i<arraySize; i++)
{
cout << "Enter element:" << i+1 << ":\t";
cin >> a[i];
}
cout << "Data items in origional order" << endl;
for(i = 0; i<arraySize; i++)
cout << setw(4) << a[i];
for(int pass =1; pass <arraySize; pass++)
for(i=0; i<arraySize-1; i++)
if(a[i] > a[i+1])
{
hold = a[i];
a[i] = a[i+1];
a[i+1] = hold;
}
cout << endl << "Data items in ascending order" << endl;
for(i=0; i<arraySize; i++)
cout << setw(4) << a[i];
cout << endl;
// return 0;
}
}
void searchSort::ArrayExit()
{
if(choice = 4)
{
cout << " You typed 4" << endl;
cout << " Exit from the program" << endl;
}
}
int searchSort::get_size()
{
int size;
cout << "Enter the size of the array " << endl;
cin >> size;
return size;
}
int searchSort::linearSearch(int array[], int key, int sizeOfArray)
{
for(int n=0; n < sizeOfArray; n++)
if(array[n] == key)
return n;
return -1;
}
int main()
{
int *z, searchKey, size;
searchSort ss;
ss.CreateMainMenu();
ss.IntArray();
ss.FloatArray();
ss.DoubleArray();
ss.CharArray();
ss.ArraySearch();
ss.ArraySort();
ss.ArrayExit();
ss.get_size();
ss.linearSearch(z, searchKey, size);
return 0;
}
This is Ravi. I was trying one searching and sorting problem using classes, functions and objects. I am not getting any errors but i am getting some warnings. Please check my problem when you feel free. Please help me in this regard. Please let me know where did i mistake? I'll be appreciated your help in this regard. Take care.Bye for now.
with best regards
Ravi.
Below the problem is :
// Searching and Sorting Array Elements Using Classes and Objects:
#include <iostream.h>
#include <iomanip.h>
class searchSort {
private:
int choice;
int choiceCreate;
int i, size;
public:
void CreateMainMenu();
void IntArray();
void FloatArray();
void DoubleArray();
void CharArray();
void ArraySearch();
void ArraySort();
void ArrayExit();
int get_size();
int linearSearch(int[], int, int);
};
void searchSort::CreateMainMenu()
{
cout << "1 Creates an array depends on its data type" << endl;
cout << "2 Search for the elements of an array" << endl;
cout << "3 Sorting an exsting array" << endl;
cout << "4 Exit from the program" << endl;
int choice;
cin >> choice;
if(choice = 1)
{
int choiceCreate ;
cout << "\t\t Array Menu" << endl;
cout << "\t\t1 create int array" << endl;
cout << "\t\t2 create float array" << endl;
cout << "\t\t3 create double array" << endl;
cout << "\t\t4 create char array" << endl;
cout << "\t\t Enter Selection :\t";
cin >> choiceCreate;
//checking choice bounding
if(choiceCreate <1 || choiceCreate>4)
{
cerr << "Invalid entry \n";
//return 1;
}
}
}
void searchSort::IntArray()
{
if(choiceCreate == 1)
cout << endl << "You entered integer values" << endl;
int *x;
size=get_size();
x = new int[size];
for(i=0; i<size; i++)
{
cout << " Enter Element " << i+1 <<":\t";
cin >> x[i];
}
for(i=0; i<size; i++)
cout << x[i] << "\t ";
cout << "The size of the array is =" << size << endl ;
delete [] x;
}
void searchSort::FloatArray()
{
if(choiceCreate == 2)
cout << endl << "You entered float values" << endl;
float *y;
size=get_size();
y = new float[size];
for(i=0; i<size; i++)
{
cout << " Enter Element " << i+1 <<":\t";
cin >> y[i];
}
for(i=0; i<size; i++)
cout << y[i] << "\t ";
cout << "The size of the array is =" << size << endl ;
delete [] y;
}
void searchSort::DoubleArray()
{
if(choiceCreate == 3)
cout << "You entered double values" << endl;
double *z;
size=get_size();
z = new double[size];
for(i=0; i<size; i++)
{
cout << " Enter Element " << i+1<<":\t";
cin >> z[i];
}
for(i=0; i<size; i++)
cout << z[i] << "\t ";
cout << "The size of the array is =" << size << endl ;
delete [] z;
}
void searchSort::CharArray()
{
if(choiceCreate == 4)
cout << "You entered character values" << endl;
char *c;
size=get_size();
c = new char[size];
for(i=0; i<size; i++)
{
cout << " Enter Element " << i+1<<":\t";
cin >> c[i];
}
for(i=0; i<size; i++)
cout << c[i] << "\t ";
cout << "The size of the array is =" << size << endl ;
delete [] c;
}
void searchSort::ArraySearch()
{
if(choice = 2)
{
int *z;
int size,searchKey;
cout << "Search for an element in the array:" << endl;
size=get_size();
z= new int[size];
for(int i=0; i<size; i++)
{
cout << " Enter Element " << i+1<<":\t";
cin >> z[i];
}
cout << "Enter integer search key:" << endl;
cin >> searchKey;
int element = linearSearch(z, searchKey, size);
if(element >0)
cout << "Found value at index \t" << element << endl;
else
cout << "Value not found" << endl;
}
}
void searchSort::ArraySort()
{
if(choice = 3)
{
int i, *a;
int hold;
int arraySize;
cout << "Enter the array size:" << endl;
cin >> arraySize;
cout << "Enter elements for sorting" << endl;
a = new int[arraySize];
for(i=0; i<arraySize; i++)
{
cout << "Enter element:" << i+1 << ":\t";
cin >> a[i];
}
cout << "Data items in origional order" << endl;
for(i = 0; i<arraySize; i++)
cout << setw(4) << a[i];
for(int pass =1; pass <arraySize; pass++)
for(i=0; i<arraySize-1; i++)
if(a[i] > a[i+1])
{
hold = a[i];
a[i] = a[i+1];
a[i+1] = hold;
}
cout << endl << "Data items in ascending order" << endl;
for(i=0; i<arraySize; i++)
cout << setw(4) << a[i];
cout << endl;
// return 0;
}
}
void searchSort::ArrayExit()
{
if(choice = 4)
{
cout << " You typed 4" << endl;
cout << " Exit from the program" << endl;
}
}
int searchSort::get_size()
{
int size;
cout << "Enter the size of the array " << endl;
cin >> size;
return size;
}
int searchSort::linearSearch(int array[], int key, int sizeOfArray)
{
for(int n=0; n < sizeOfArray; n++)
if(array[n] == key)
return n;
return -1;
}
int main()
{
int *z, searchKey, size;
searchSort ss;
ss.CreateMainMenu();
ss.IntArray();
ss.FloatArray();
ss.DoubleArray();
ss.CharArray();
ss.ArraySearch();
ss.ArraySort();
ss.ArrayExit();
ss.get_size();
ss.linearSearch(z, searchKey, size);
return 0;
}