21
CPT111 Principles of Programming Semester 1 , Academics Session 2012/2013 Assignment 21 Course:CPT111 Principles of Programming School:School of Computer Science Name: Nur Izzati Zafirah Group:C

Principle of programming c++

Embed Size (px)

Citation preview

Page 1: Principle of programming c++

CPT111 Principles of ProgrammingSemester 1 , Academics Session

2012/2013Assignment 21

Course:CPT111 Principles of Programming

School:School of Computer Science

Name: Nur Izzati Zafirah

Group:C

Question 1

Page 2: Principle of programming c++

Specification requirement

1. First find 2 random number and display arithmetic calculation type.

2. Ask user to choose type of calculation.

3. The calculation will be process.

Problem analysis

Input Process Output Test data

User need to input

calculation type

The process involved in this

stage is the selection of

switch condition case.

And then the calculation will

be perform.

div= max / num

mul=num1*num2

sub=num1-num2

add = num1+num2

The output that will

appear on the screen is

the addition,

calculation ,

subtraction or

division.

Input = 1

Output= 60

.

Pseudo code

Page 3: Principle of programming c++

start

2 random number will be generate.

Display choice of arithmetic calculation

Case 1 : AdditionCase 2 : SubtractionCase 3 : MultiplicationCase 4 : Division

Switch (case)

Case 1 : Addition = a + b

Case 2 : Subtraction = a - b

Case 3 : Multiplication = a * b

Case 4 : Division = a/b Remainder = a%b or b%a

Print the total calculationEnd

start

Flowchart

Page 4: Principle of programming c++

Source code

Add= num1 + num2

end

Case 1

Read type calculation

Print addition

You enter wrong number

True

false

Case 2

Case 3

Case 4

Subtraction= num 1 – num2

Multiplication= num1 x num2

Division = num1 ÷ num 2

Print subtraction

Print Multiplication

Print Division

false

false

false

True

True

True

Random number generate

Page 5: Principle of programming c++

#include <cstdlib>#include <ctime>#include <iostream>using namespace std;int biggest(int x,int y);int lowest(int x,int y) ;

int main(){ srand((unsigned)time(0)); int num1 , num2 ; { num1 = (rand()%99)+1;

num2 = (rand()%99)+1; cout <<"2 random number = "<< num1 <<" & "<< num2<< endl; }

int type , add , sub , mul , div , max , x , y , num , remainder ; cout<<"please choose your calculation type"<<endl ;

cout<<"1 : Addition"<<endl ;cout<<"2 : Subtraction"<<endl ;

cout<<"3 : Multiplication"<<endl ; cout<<"4 : Division"<<endl ; cin>>type ;

switch(type){case 1 :cout<<"addition"<<endl ;

add = num1+num2 ;cout<<num1<<"+"<<num2<<"="<<add<<endl ;break ;

case 2 :cout<<"Subraction"<<endl ; sub=num1-num2 ;

cout<<num1<<"-"<<num2<<"="<<sub<<endl ;break ;

case 3 :cout<<"multiplication"<<endl ;mul=num1*num2 ;cout<<num1<<"*"<<num2<<"="<<mul<<endl ;break ;

case 4 :cout<<"division"<<endl ; x=num1 ; y=num2 ;

max=biggest( x ,y);

Page 6: Principle of programming c++

num=lowest (x , y);div= max / num ;remainder = max%num ;cout<<max<<"/"<<num<<"="<<"\nquotient ="<<div<<"\nremainder

="<<remainder<<endl ;break ;

default : cout<<"you enter wrong number"<<endl ;} return 0;}int biggest(int x,int y){ if(x>=y)

return x;else

return y;;}

int lowest(int x,int y){ if(x<=y)

return x;else

return y;;}

Print screen of output

The input = 1 ,Output= 41 , 19 , 60

Page 7: Principle of programming c++

The input = 2 output = 3,12 ,9

The input = 3 , output = 21 , 89 and 1869

Page 8: Principle of programming c++

The input = 4 output = 66 , 89 , 1 , 23

The input = 0 output = 96 , 32, wrong number

Page 9: Principle of programming c++

Question 2(a)#include<iostream>#include<cmath>using namespace std;int main(){int i;int x_sqr[100];double x_sqr_rt[100];for(i=0;i<=100;i++){x_sqr[i]=i*i;cout<<"x_sqr[i]"<<x_sqr[i]<<endl;}for (i=0;i<=100;i++){x_sqr_rt[i]=sqrt(i);cout<<"x_sqr_rt[i]"<<x_sqr_rt[i]<<endl;cout<<"thats all"<<endl;}return 0;}

Page 10: Principle of programming c++

Question 2(b)#include <iostream>#include <cmath>using namespace std;

const int inputSIZE = 17;void print(int *input){ for ( int i = 0; i < inputSIZE; i++ )

cout << input[i] << " "; cout << endl;}void merge(int* input, int p, int r){ int mid = floor((p + r) / 2); int i1 = 0; int i2 = p; int i3 = mid + 1; int temp[r-p+1]; while ( i2 <= mid && i3 <= r ) if ( input[i2] < input[i3] ) temp[i1++] = input[i2++]; else temp[i1++] = input[i3++]; while ( i2 <= mid ) temp[i1++] = input[i2++]; while ( i3 <= r ) temp[i1++] = input[i3++];

for ( int i = p; i <= r; i++ ) input[i] = temp[i-p];

}

void merge_sort(int* input, int p, int r){if ( p < r ) { int mid = floor((p + r) / 2); merge_sort(input, p, mid); merge_sort(input, mid + 1, r); merge(input, p, r); }}

int main(){ cout<<"a_array= {10, 2, 4, 6, 8, 10, 12, 37, 45, 68, 89}"<<endl; cout<<"b_array= {5, 1, 3, 14, 50, 150}"<<endl; int input[inputSIZE] = {10, 2, 4, 6, 8, 10, 12, 37, 45, 68, 89,5, 1, 3, 14, 50, 150}; cout << "Input: ";

Page 11: Principle of programming c++

print(input); merge_sort(input, 0, 16); cout << "Output: "; print(input); return 0;

Page 12: Principle of programming c++

Question 2(c) [i]#include <iostream>#include <conio.h>#include <cmath>#include <fstream>

using namespace std;int compress (string fileName, string newFileName){

ifstream readfile;readfile.open (fileName.c_str());

if (readfile.fail()){

cout<<"Error Opening.";return 1;

}

ofstream outputfile;outputfile.open(newFileName.c_str());

if (outputfile.fail()){

cout<<"Error Opening.";return 1;

}

char x;int i = 0;readfile.get(x);while (!readfile.eof()){

if (x == ' '){

i++;readfile.get(x);continue;

}else if ( i == 0 )

;else if ( i >= 4 ){

outputfile<<"$"<<i<<"$";i = 0;continue;

}else

Page 13: Principle of programming c++

{for (i; i > 0; i--){

outputfile<<' ';}i=0;

}outputfile<<x;readfile.get(x);

}

readfile.close();outputfile.close();

return 0;}int expand (string openFile, string writeFile){

ifstream readfile2;readfile2.open(openFile.c_str());

if (readfile2.fail()){

cout<<"Error Opening.";return 1;

}

ofstream output2;output2.open (writeFile.c_str());

if (output2.fail()){

cout<<"Error Opening.";return 1;

}

char x;int i = 0;readfile2.get(x);while (!readfile2.eof()){

if ( x == '$')readfile2.get(x);

else{

output2<<x;readfile2.get(x);continue;

Page 14: Principle of programming c++

}if ( x == '$' ){

output2<<'$';continue;

}else if ( (x == '1') | (x == '2') | (x == '3') | (x == '4') | (x == '5') | (x == '6') | (x == '7') | (x ==

'8') | (x == '9') | (x == '0')){

char numx[5];int i = -1, j = 0;while ( (x == '1') | (x == '2') | (x == '3') | (x == '4') | (x == '5') | (x == '6') | (x == '7')

| (x == '8') | (x == '9') | (x == '0')){

i++;numx[i] = x;readfile2.get(x);

}if ( x != '$'){

output2<<'$';for (j = 0; j <= i; j++){

output2<<numx[j];}output2<<x;

}else{

j = i;int y = 0;for ( i; i >= 0; i--){

numx[i] -= '0';static_cast <int> (numx[i]);y += numx[i] * pow ( 10, (j-i));

}for ( y; y >0; y--){

output2<<' ';}readfile2.get(x);

}}else{

output2<<'$'<<x;readfile2.get(x);

Page 15: Principle of programming c++

}}return 0;

}

int main (){

string fileName, newFileName, newFileName2;int compressFlag, expandFlag, reproduce;cout<<"Enter the name of the file you would like to edit: ";cin>>fileName;cout<<"Enter the name of the new file for the edited file: ";cin>>newFileName;compressFlag = compress (fileName, newFileName);if ( compressFlag == 0)

cout<<"The file have been successfully edited."<<endl;else

cout<<"The file have not edited."<<endl;

cout<<"Do you want to convert the file to reproduce the original file?";cin>>reproduce;if ( reproduce == 1){

cout<<"Enter the name of the new file: ";cin>>newFileName2;expandFlag = expand ( newFileName, newFileName2);if ( expandFlag == 0)

cout<<"The file have been successfully edited.";else

cout<<"The file have not edited.";}return 0;

}Question 2(c) [ii]#include <iostream>#include <conio.h>#include <cmath>#include <fstream>

using namespace std;int compress (string fileName, string newFileName){

ifstream readfile;readfile.open (fileName.c_str());

if (readfile.fail()){

Page 16: Principle of programming c++

cout<<"Error Opening.";return 1;

}

ofstream outputfile;outputfile.open(newFileName.c_str());

if (outputfile.fail()){

cout<<"Error Opening.";return 1;

}

char x;int i = 0;readfile.get(x);while (!readfile.eof()){

if (x == ' '){

i++;readfile.get(x);continue;

}else if ( i == 0 )

;else if ( i >= 4 ){

outputfile<<"$"<<i<<"$";i = 0;continue;

}else{

for (i; i > 0; i--){

outputfile<<' ';}i=0;

}outputfile<<x;readfile.get(x);

}

readfile.close();outputfile.close();

return 0;

Page 17: Principle of programming c++

}int expand (string openFile, string writeFile){

ifstream readfile2;readfile2.open(openFile.c_str());

if (readfile2.fail()){

cout<<"Error Opening.";return 1;

}

ofstream output2;output2.open (writeFile.c_str());

if (output2.fail()){

cout<<"Error Opening.";return 1;

}

char x;int i = 0;readfile2.get(x);while (!readfile2.eof()){

if ( x == '$')readfile2.get(x);

else{

output2<<x;readfile2.get(x);continue;

}if ( x == '$' ){

output2<<'$';continue;

}else if ( (x == '1') | (x == '2') | (x == '3') | (x == '4') | (x == '5') | (x == '6') | (x == '7') | (x ==

'8') | (x == '9') | (x == '0')){

char numx[5];int i = -1, j = 0;while ( (x == '1') | (x == '2') | (x == '3') | (x == '4') | (x == '5') | (x == '6') | (x == '7')

| (x == '8') | (x == '9') | (x == '0')){

i++;

Page 18: Principle of programming c++

numx[i] = x;readfile2.get(x);

}if ( x != '$'){

output2<<'$';for (j = 0; j <= i; j++){

output2<<numx[j];}output2<<x;

}else{

j = i;int y = 0;for ( i; i >= 0; i--){

numx[i] -= '0';static_cast <int> (numx[i]);y += numx[i] * pow ( 10, (j-i));

}for ( y; y >0; y--){

output2<<' ';}readfile2.get(x);

}}else{

output2<<'$'<<x;readfile2.get(x);

}}return 0;

}

int main (){

string fileName, newFileName, newFileName2;int compressFlag, expandFlag, reproduce;cout<<"Enter the name of the file you would like to edit: ";cin>>fileName;cout<<"Enter the name of the new file for the edited file: ";cin>>newFileName;compressFlag = compress (fileName, newFileName);if ( compressFlag == 0)

Page 19: Principle of programming c++

cout<<"The file have been successfully edited."<<endl;else

cout<<"The file have not edited."<<endl;

cout<<"Do you want to convert the file to reproduce the original file?";cin>>reproduce;if ( reproduce == 1){

cout<<"Enter the name of the new file: ";cin>>newFileName2;expandFlag = expand ( newFileName, newFileName2);if ( expandFlag == 0)

cout<<"The file have been successfully edited.";else

cout<<"The file have not edited.";}return 0;

}