C++- Searching and Sorting in Arrays!(Error-Local Function Definations are illegal)
Hi,
this is the sample output.
I have to display main menu as
1.Create Array
2.Search for elements in the array
3.Sort Array
4.Exit
in the submenu, 1st one again i have to display
a.create int array- it has to ask "Enter the size of the array"?
b.create float array " "
c.create double array " "
d.create char array " "
Below I am sending the code i have written so far.I am getting error. Please let me know how to remove that one. Please check the below code and particularly please check the serching and sorting cases.Thanks for your advanced help. The code is
------------------------------******-----------------------------------------
#include <iostream.h>
#include <iomanip.h>
int linearSearch(int [], int, int);
//main function declaration
int main()
{
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;
switch (choice)
{
case 1:
{
int choiceCreate ;
// try cout object in a cascading style
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;
}
// use else if -- Kishore
//------------------*for integer array elements*------------------//
if(choiceCreate == 1)
cout << endl << "You entered integer values" << endl;
int i, *x, size;
cout << "Enter the size of the array " << endl;
cin >> 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;
//------------------*for floating point array elements*------------------//
if(choiceCreate == 2)
cout << endl << "You entered float values" << endl;
float *y;
cout << "Enter the size of the array " << endl;
cin >> 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;
//------------------*for double array elements*------------------//
if(choiceCreate == 3)
cout << "you entered double values" << endl;
double *z;
cout << "enter the size of the array " << endl;
cin >> 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;
//------------------*for charecter array elements*------------------//
if(choiceCreate == 4)
cout << "you entered character values" << endl;
char *c;
cout << "enter the size of the array " << endl;
cin >> 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;
break ;
}
case 2:
{
cout << " You typed 2" << endl;
cout <<"Search for an array element" << endl;
int elementPosition;
cin >> elementPosition;
cout << "Search for an element in the array:" << endl;
const int arraySize = 100;
//int a[100];
int a[arraySize], searchKey, element;
for(int x=0; x<arraySize; x++)
a[x] = 2 * x;
cout << "Enter integer search key:" << endl;
cin >> searchKey;
element = linearSearch(a, searchKey, arraySize);
if(element != -1)
cout << "Found value in element" << element << endl;
else
cout << "Value not found" << endl;
return 0;
int linearSearch(int array[], int key, int sizeOfArray)
{
for(int n=0; n < sizeOfArray; n++)
if(array[n] == key)
return n;
return -1;
}
cout << "The position of element in the array is=" << elementPosition << endl;
break ;
}
case 3:
{
cout << " You typed 3" << endl;
int arraySize;
cout << "Enter elements for sorting" << endl;
cin >> arraySize;
int a[100];
//const int arraySize = 10;
//int a[arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
int hold;
cout << "Data items in origional order" << endl;
for(int 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 bubblesort( int array[], int n )
{
for ( int i = 0; i < n-1; ++i )
for ( int j = 1; j < n-i; ++j )
if ( array[j-1] > array[j] )
// Note the use here of swap()
swap( array[j-1], array[j] );
}*/
//cout << "The sorted exsting array is=" << sort << endl;
break ;
}
default :
{
cout << " You typed 4" << endl;
cout << " Exit from the program" << endl;
break ;
}
}
return 0;
}
--------------------------------**************-------------------------
Thanks & Best Regards
Ravi.
C++ -Array Search & Sort problem
Hi KevinHall,
Nice to receive a mail from you.Thanks for sending a mail.
Yaa i am getting following error.
'linrearSearch': local function definations are illegal
Please compile once and check it.Once i'll clear this program i'll make this program using function concept.
Thanks for your help.
Thanks & Best Regards
Ravi.
The code is............
#include <iostream.h>
#include <iomanip.h>
int linearSearch(int [], int, int);
//main function declaration
int main()
{
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;
switch (choice)
{
case 1:
{
int choiceCreate ;
// try cout object in a cascading style
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;
}
// use else if -- Kishore
//------------------*for integer array elements*------------------//
if(choiceCreate == 1)
cout << endl << "You entered integer values" << endl;
int i, *x, size;
cout << "Enter the size of the array " << endl;
cin >> 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;
//------------------*for floating point array elements*------------------//
if(choiceCreate == 2)
cout << endl << "You entered float values" << endl;
float *y;
cout << "Enter the size of the array " << endl;
cin >> 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;
//------------------*for double array elements*------------------//
if(choiceCreate == 3)
cout << "you entered double values" << endl;
double *z;
cout << "enter the size of the array " << endl;
cin >> 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;
//------------------*for charecter array elements*------------------//
if(choiceCreate == 4)
cout << "you entered character values" << endl;
char *c;
cout << "enter the size of the array " << endl;
cin >> 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;
break ;
}
case 2:
{
cout << " You typed 2" << endl;
cout <<"Search for an array element" << endl;
int elementPosition;
cin >> elementPosition;
cout << "Search for an element in the array:" << endl;
const int arraySize = 100;
//int a[100];
int a[arraySize], searchKey, element;
for(int x=0; x<arraySize; x++)
a[x] = 2 * x;
cout << "Enter integer search key:" << endl;
cin >> searchKey;
element = linearSearch(a, searchKey, arraySize);
if(element != -1)
cout << "Found value in element" << element << endl;
else
cout << "Value not found" << endl;
return 0;
int linearSearch(int array[], int key, int sizeOfArray)
{
for(int n=0; n < sizeOfArray; n++)
if(array[n] == key)
return n;
return -1;
}
cout << "The position of element in the array is=" << elementPosition << endl;
break ;
}
case 3:
{
cout << " You typed 3" << endl;
int arraySize;
cout << "Enter elements for sorting" << endl;
cin >> arraySize;
int a[100];
//const int arraySize = 10;
//int a[arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
int hold;
cout << "Data items in origional order" << endl;
for(int 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 bubblesort( int array[], int n )
{
for ( int i = 0; i < n-1; ++i )
for ( int j = 1; j < n-i; ++j )
if ( array[j-1] > array[j] )
// Note the use here of swap()
swap( array[j-1], array[j] );
}*/
//cout << "The sorted exsting array is=" << sort << endl;
break ;
}
default :
{
cout << " You typed 4" << endl;
cout << " Exit from the program" << endl;
break ;
}
}
return 0;
}
C++-Sort and Search problem
Thanks Kevin. I'll try my level best. Please meanwhile can you compile and check the output for Searching and Sorting code.And please let me know where should i change the code? Once this program works, i'll transform this program into Classes and Functions.
Please tell me where should i change.The user has to type the input in the prompt.I don't want constant arraySize for searching and Sorting. Please explain me the code for this.
Thanks & Best Regards
Ravi.