Upload
enzo-ignacio
View
221
Download
0
Embed Size (px)
Citation preview
8/6/2019 4 Printing Final Venture
http://slidepdf.com/reader/full/4-printing-final-venture 1/6
Problem Number 1.
Write a program that implements a simple stack of integers. The program providesThree 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;
8/6/2019 4 Printing Final Venture
http://slidepdf.com/reader/full/4-printing-final-venture 2/6
}
}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 stack2 to pop a value off the stack3 to end the program
What is your choice? 1
Enter an integer: 5The stack is:5 -> NULL
What is your choice? 1Enter an integer: 6The stack is:6 -> 5 -> NULL
What is your choice? 1Enter an integer: 4The stack is:4 -> 6 -> 5 -> NULL
What is your choice? 2The popped value is 4The stack is:6 -> 5 -> NULL
What is your choice? 2
The popped value is 6The stack is:5 -> NULL
What is your choice? 2The popped value is 5The stack is:NULL
What is your choice? 2The popped value is 0
empty
What is your choice? 4Invalid choice.
Enter Choice:1 to push a value on the stack2 to pop a value off the stack3 to end the program
What is your choice? 3End of run.
8/6/2019 4 Printing Final Venture
http://slidepdf.com/reader/full/4-printing-final-venture 3/6
8/6/2019 4 Printing Final Venture
http://slidepdf.com/reader/full/4-printing-final-venture 4/6
<< 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;
8/6/2019 4 Printing Final Venture
http://slidepdf.com/reader/full/4-printing-final-venture 5/6
}
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;}
}
void printArray( const int a[] ){
int j;for ( j = 0; j <= SIZE -1; j++ ){
if ( j % 20 == 0 )cout << endl;
cout << a[ j ]<<" ";
}
}
MeanThe mean is the average value of the data items. The mean is equal tothe 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 is6 7 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 86 7 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 96 7 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 35 6 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 87 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8
The sorted array is
1 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 5 5 5 56 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 77 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 88 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 89 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
The median is element 49 of the sorted 99 element array.For this run the median is 7
ModeThe mode is the most frequent value.For this run the mode is 8 which occurred 24 times.
8/6/2019 4 Printing Final Venture
http://slidepdf.com/reader/full/4-printing-final-venture 6/6
Final Venture
Sevilla, Lorenzo Jose I.B.S. in info Tech (BSIT),First Year
091-022608:00-10:30 / WS