Click to See Complete Forum and Search --> : C++- Searching and Sorting in Arrays!(Error-Local Function Definations are illegal)


Ravi149
December 2nd, 2002, 02:18 AM
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.

KevinHall
December 2nd, 2002, 05:47 AM
This looks like a classic homework problem! I don't think most of us mind helping out with homework as long as you have shown that you have really, REALLY tried.

In this case, it looks like you have tried to write a program. Great! :) I'm not going to compile it for you though -- you also need to learn debugging techniques! Anser these questions: What line is causing your error? What does the compiler report?

If you still can't figure things out, then list the answers to those two questions back here and I am sure people will help direct you in your quest to debug the program!

- Kevin

P.S. I'd recommend seperating your application into more functions where each one can be short and have a more well defined purpose. Not only does this make the code easier to follow, but it also helps isolate bugs. And who knows, you may even be able to use one of them again in future assignements. Ex:


void DemonstrateCharArrayInput()
{
&nbsp;...
}

void DemonstrateIntArrayInput()
{
&nbsp;...
}

...

void DemonstrateArraySearch()
{
&nbsp;...
}

void DemonstrateArraySort()
{
&nbsp;...
}

void ArrayInputMenu()
{
&nbsp;...
}

void MainMenu()
{
&nbsp;...
&nbsp; &nbsp;case 1:
&nbsp; &nbsp; &nbsp; &nbsp;ArrayInputMenu();
&nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;...
}

void main()
{
&nbsp;MainMenu();
}

Ravi149
December 2nd, 2002, 12:09 PM
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;
}

KevinHall
December 2nd, 2002, 12:23 PM
The problem is that you have defined functions within other functions -- in this case main(). This is what the compiler is complaining about. Move the definitions of linearSearch() outside of main(). It's commented out right now, but you will also have to move bubblesort() outside of main() when you uncomment it.

- Kevin

Ravi149
December 2nd, 2002, 01:26 PM
Hi KevinHall,
Once again thanks for spending your valuable time on my C++ problem.Now i am not getting any errors.it's compiling without any errors.
1. one importent thing is the user has to type the input from the keyboard(like arraySize and array Elements etc).
2. Please check the Searching and Sorting code in the program.
3.If possible please check the output of Serching and Sorting.
I tried for so much time,please check.
4.If i tried to enter the values in the promt for sorting ,it's giving some junk values in the original order and ascending order.Please check if you don't mind.
I'll be appreciated your help in this regard. Take care.
Thanks & Best Regards
Ravi.


-------------------Below is the updated code-------------------------------
#include <iostream.h>
#include <iomanip.h>

int linearSearch(int array[], int key, int sizeOfArray)
{
for(int n=0; n < sizeOfArray; n++)
if(array[n] == key)
return n;
return -1;

}

//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;
//int arraySize;
//cout <<"Enter the size of the array:" << endl;
//cin >> arraySize;
//int a[100];
cout << "Search for an element in the array:" << endl;
int linearSearch(int [], int, int);
const int arraySize = 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;
break ;
}


case 3:
{
cout << " You typed 3" << endl;
cout << "Enter elements for sorting" << endl;
int i, arraySize=0;
for(i=0; i<arraySize; i++)
cout << "Enter the array size 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(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;
break ;
}


default :
{
cout << " You typed 4" << endl;
cout << " Exit from the program" << endl;
break ;
}
}
return 0;
}

KevinHall
December 2nd, 2002, 01:44 PM
Do you think the searching works? Try several cases yourself and tell me what you think.

About the sorting, have you noticed the user never gets asked "Enter the array size for sorting"? Why do you think this is? It's right there in your code -- within the case block. Think about it!

KevinHall
December 2nd, 2002, 01:52 PM
Oh, yes. You're welcome. I don't mind helping if:

1) You don't expect me to do your homework -- because I won't
2) It doesn't take too much of my time (this is a small project, so don't worry about it :) )
3) You do your best in trying to figure things out.

I think most of us at CodeGuru have been where you are. Don't get discouraged and don't give up. You are almost there!

- Kevin

P.S. Here are some purely aesthetic changes you might want to consider:

1) Check your spelling.
2) Some of your program's output would look better with an extra space.

Ravi149
December 2nd, 2002, 02:28 PM
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.

KevinHall
December 2nd, 2002, 03:05 PM
I did compile and run everything. The behavior makes sense. Now go up three messages and read what I left there. Answer those questions.

Ravi149
December 2nd, 2002, 04:10 PM
Hi Kevin,
Why i am getting following problem?

C:\Documents and Settings\Ravinder\sortArray.cpp(11) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)

I am getting this error while compiling this program
Please the user should type the elements for sorting, i don't want const array size. How can u make changes to this following program.

#include <iostream.h>
#include <iomanip.h>
int main()
{
int i;
int hold;
const int arraySize=10;
int a[arraySize];

cout << "Enter the array size:" << endl;
cin >> arraySize;

cout << "Enter elements for sorting" << endl;
for(i=0; i<arraySize; i++)

//const int arraySize = 10;
//int a[arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};

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;
}

Please this time what ever u make please send me through this mail(i mean code).
Thanks & Best Regards
Ravi.

KevinHall
December 2nd, 2002, 04:24 PM
You can't write to a constant variable. Try:

int arraySize = 10;

Did you figure out what was going on earlier? Can you answer the questions I asked you?

PaulWendt
December 2nd, 2002, 04:32 PM
You have to make arraySize a non-const int.
Then, instead of using a fixed-size array, you will have to use
the "new" keyword in order to create a dynamically-sized array.
Remember: once you're done with the stuff you "new'ed", be
sure to use the "delete" keyword to get rid of it.

Here's a brief example that should be covered in just about any
introductory C++ textbook:


int arraySize = 10;
int* elementList = new int[arraySize];
for (unsigned int i = 0; i < arraySize; ++i)
{
// do whatever with these items
}
delete [] elementList;