You are here:

# C++/Array Processing!

Question
Hi. I'm Donn and I'm from the Philippines. I'm a 2nd year college
student right now taking up Chemical Engineering. I'm having a hard time
with my machine problem and I was hoping you could help me with it. I need a complete program to see what was wrong with the one I made.

Here it is:

Write a program that will offer the following services:
1>Create the Array
2>Edit the array- with 3 options: Add, Delete and Change element
3>Sort the array
4>Display the Array
5>Quit this program

The size N of the array and the N integers will be entered on the
keyboard. Sorting will be done in ascending order.The array will be
displayed in 2 columns, the first column showing the index and the second
column showing the content. Make a loop, so that the program always displays
terminates if the user selects 5 on the menu.

I would really appreciate it if you could help me with it. Yhank you so
much and GOD bless you always.

Dear Donn!

Array processing is the simplest and most fundamental operation in data structures !

Here i present you complete code written in C ++   ,I present you some information regarding this program!
1. Program uses bubble sort algorithm to sort the array  , ( Which is the simplest sorting algorithm )
2. Deletion is not possible in array (Its possible only is array is implemented as a list  , where as list are not stored in contiguous memory location ) So to represent the deletion operation  , i replaced  the element to 0
3. All other aspects are self explainable!

Here is the program !

#include<iostream.h>
#define max 100
int array[max];
int maxsize , counter = -1;
void sort( int *a  , int n)
{
for( int i  = 0 ; i < n ; i  )
for( int j = 0 ; j < n-1; j  )
if(a[j] > a[j+1] )
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}

{
if(counter == maxsize - 1)
{
cerr<<"Error : Overflow .."<<endl;
return;
}
array[++counter] = n;
}
void disp()
{
if( counter == -1)
{
cerr<<"Error : No element in the array .."<<endl;
return;
}
cout<<"The array elements are :";
for(int i = 0 ; i <= counter ; i  )
cout<<array[i]<<" ";
cout<<endl;
}
void edit( int loc , int data)
{
if( loc == 0)
{
cerr<<"Invalid location"<<endl;
return;
}
else if( loc >= counter )
{
cerr<<"Error : Location out of limit :"<<endl;
return;
}
array[loc-1] = data;
}
void Delete ( int loc)
{
if( loc <= 0)
{
cerr<<"Error : Invalid location .."<<endl;
return;
}
else if( loc >= counter )
{
cerr<<"Error : Location out of limit :"<<endl;
return;
}
array[loc] = 0;
}
void main()
{
int opt , data , loc;

cout<<"Array processing in C++  "<<endl;
cout<<"Enter the size of array :";
cin>>maxsize;
do{
<<"2.Edit Array"<<endl
<<"3.Display "<<endl
<<"4.Delete Element"<<endl
<<"5.Sort"<<endl
<<"6.Exit"<<endl;

cin>>opt;
switch(opt)
{
case 1:
cout<<"Enter element to insert in array:";
cin>>data;
break;
case 2:
cout<<"Enter loc  , data to change ";
cin>>loc>> data;
edit(loc , data);
break;
case 3:
disp();
break;
case 4:
cout<<"Enter a location to delete :";
cin>>loc;
Delete(loc);
break;
case 5:
sort(array , counter);
break;
}
}while(opt != 6);
}

Thanks and regards!
Prince M. Premnath.

C++

Volunteer

#### Prince M. Premnath

##### Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

##### Experience

More than 5 years

Education/Credentials
MCA