Click to See Complete Forum and Search --> : C++ Classes and Objects Program!!


Ravi149
December 9th, 2002, 01:50 AM
Hi Friends,
How are you? Good Morning!
I am getting o/p for below C++ program(using genaral C++ arrays , flow statements, and functions).But i want to develope this program on Classes, Objetcs and Functions etc? Please let me know the sample code.
I'll be appreciated your help in this regard.
Please check the o/p of this program.

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

}
//kishore
int get_size()
{
int size;
cout << "Enter the size of the array " << endl;
cin >> size;
return size;
}

//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;
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;
break;

//------------------*for floating point array elements*------------------//
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;
break;

//------------------*for double array elements*------------------//
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;
break;

//------------------*for charecter array elements*------------------//
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;
break ;
}


case 2:
{
int *z;
int size,searchKey;
//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;

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;
//return 0;
break ;
}


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


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

SeventhStar
December 9th, 2002, 02:13 AM
well what do you want exactly?...
to learn how to use templates
or to have this program in c++ style

do you have msdn
look up 'vector' and 'CArray'

Gabriel Fleseriu
December 9th, 2002, 04:37 AM
This looks like homework to me :rolleyes:

SeventhStar
December 9th, 2002, 04:43 AM
Originally posted by Gabriel Fleseriu
This looks like homework to me :rolleyes:
Mmm yes to me too... And many people think that we would provide them with full working functional programs just because of their 'good looks' or whatever :mad: