Sie sind auf Seite 1von 6

Problem Number 1.

Write a program that implements a simple stack of integers. The program provides Three options: 1) Push a value onto the stack (function push) 2) Pop a value off the stack (function pop) 3) Terminate the program

/*Program Created by: Sevilla, Lorenzo Jose, I. BSIT-1*/ #include<iostream.h> #include<process.h> const int maxstk=100; int top; int stack [maxstk]; push (int item); pop(); display(); void main() { int ch, item; cout<<"Enter Choice:\n1 to push a value on the stack\n"; cout<<"2 to pop a value off the stack\n3 to end the program"; do { cout<<"\n\nWhat is your choice? "; cin>>ch; switch (ch) { case 1: cout <<"\n\nEnter an integer: "; cin>>item; push (item); break; case 2: cout<<"The popped value is "<<stack[top]<<"\n"; pop(); break; case 3: cout<<"End of run."; exit (0); break; default: cout<<"Invalid choice. \n"; cout<<"\nEnter Choice:\n1 to push a value on the stack\n"; cout<<"2 to pop a value off the stack\n3 to end the program"; break;

} } while(1); } push(int item) { if(top==maxstk) { cout<<"overflow\n"; return 0; } else top=top+1; stack[top]=item; display(); } pop() { if(top==0) { cout<<"empty\n"; return 0; } else top=top-1; display(); } display() { cout<<"The stack is:\n"; for(int i=top;i>=1;i--) { cout<<stack[i]<<" -> "; } cout<<" NULL"; }

Enter Choice: 1 to push a value on the stack 2 to pop a value off the stack 3 to end the program What is your choice? 1 Enter an integer: 5 The stack is: 5 -> NULL What is your choice? 1 Enter an integer: 6 The stack is: 6 -> 5 -> NULL What is your choice? 1 Enter an integer: 4 The stack is: 4 -> 6 -> 5 -> NULL What is your choice? 2 The popped value is 4 The stack is: 6 -> 5 -> NULL What is your choice? 2 The popped value is 6 The stack is: 5 -> NULL What is your choice? 2 The popped value is 5 The stack is: NULL What is your choice? 2 The popped value is 0 empty What is your choice? 4 Invalid choice. Enter Choice: 1 to push a value on the stack 2 to pop a value off the stack 3 to end the program What is your choice? 3 End of run.

Problem Number 2.
Write a program that computes the Mean, Median, Median, Mode using arrays . The mean is the arithmetic average of the 99 values. The median is the middle value The mode is the value that occurs most frequently among 99 responses.

/*Program Created by: Sevilla, Lorenzo Jose, I. BSIT-1*/ #include<iostream.h> #define SIZE 99 void mean(const int[]); void median(int[]); void mode(int[],int[]); void bubbleSort(int[]); void printArray(const int[]); main() { int frequency[10]={0}; int responce[SIZE]= {6, 7, 9, 8, 7, 8, 9, 8, 9,7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 6, 7, 9, 3, 9, 8, 7, 8, 7, 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 6, 7, 7, 8 ,7, 9, 8, 9, 2 ,7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 5, 6, 2, 5, 3, 9, 4, 6, 4, 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 7, 4, 2, 5, 3, 8, 7, 5, 6, 4, 5, 6, 1, 6, 5, 7, 8, 7 };

mean(responce); median(responce); mode(frequency,responce); return 0; } void mean( const int answer[] ) { int j,total = 0; cout << "Mean\n"; for ( j = 0; j <=SIZE -1; j++ ) total += answer[ j ]; cout << "The mean is the average value of the data " << "items. The mean is equal to\nthe total of" << " all the data items divided by the number" << " of data items (" << SIZE << ").\nThe mean value for this run is: "

<< total << " / " << SIZE << " = " << static_cast< double >( total ) / SIZE << "\n\n"; }

void median( int answer[] ) {

cout << "\tMedian\n\n" << "The unsorted array of responses is"; printArray(answer);

bubbleSort( answer ); cout << "\n\nThe sorted array is"; printArray( answer ); cout << "\n\nThe median is element 49" << " of\nthe sorted 99" << " element array.\nFor this run the median is " << answer[ SIZE / 2 ] << "\n\n"; }

void {

mode( int freq[], int answer[] ) cout << "Mode"; int rating,j, largest = 0, modeValue = 0; for ( rating = 1; rating <= 9; rating++ ) freq[ rating ] = 0; for ( j = 0; j <= SIZE; j++ ) ++freq[ answer[ j ] ];

for ( rating = 1; rating <= 9; rating++ )

if ( freq[ rating ] > largest ) { largest = freq[ rating ]; modeValue = rating;

cout<<"\nThe mode is the most frequent value.\n" <<"For this run the mode is "<<modeValue <<" which occurred " << largest <<" times.\n";

} void bubbleSort( int a[] ) { int pass,j,hold; for ( pass = 1; pass <=SIZE -1;pass++ ) for ( j = 0; j <= SIZE - 2; j++ ) if ( a[ j ] > a[ j + 1 ] ) { hold = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } }
Mean The mean is the average value of the data items. The mean is equal to the total of all the data items divided by the number of data items (99). The mean value for this run is: 646 / 99 = 6.52525 Median The unsorted array of responses is 6798789897895987878 6793987877898989789 6778798927898989753 5625394647896878978 74253875645616578

void printArray( const int a[] ) { int j; for ( j = 0; j <= SIZE -1; j++ ) { if ( j % 20 == 0 ) cout << endl; cout << a[ j ] <<" "; } }

The sorted array is 12223333444455555555 66666666677777777777 77777777777888888888 88888888888888888888 9999999999999999999

The median is element 49 of the sorted 99 element array. For this run the median is 7

Mode The mode is the most frequent value. For this run the mode is 8 which occurred 24 times.

Final Venture

Sevilla, Lorenzo Jose I. B.S. in info Tech (BSIT),First Year 091-0226 08:00-10:30 / WS

Das könnte Ihnen auch gefallen