Top Banner
Programming Fundamentals Lab Spring 2011 Lab Number: 10 Registration: 1378-FBAS/BSSE/F10 Name: Muhammad Anas Section: A INTERNATIONAL ISLAMIC UNIVERSITY, ISLAMABAD FACULTY OF BASIC AND APPLIED SCIENCES DEPARTMENT OF SOFTWARE ENGINEERING
23

Lab Ten 1378

Apr 07, 2018

Download

Documents

Muhammad Anas
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 1/22

Programming Fundamentals LabSpring 2011

Lab Number: 10

Registration: 1378-FBAS/BSSE/F10

Name: Muhammad Anas

Section: A

INTERNATIONAL ISLAMIC UNIVERSITY, ISLAMABAD

FACULTY OF BASIC AND APPLIED SCIENCES

DEPARTMENT OF SOFTWARE ENGINEERING

Page 2: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 2/22

Report of Lab Ten

Experiments and Exercises

Question No. 1

Part a)

Source Code

#include <iostream>using namespace std;

//takes an integer as argument and//returns its square

int square(int a){

a = a*a;return a;

}/////////////...............///////int main(){

int n;cout << "Enter an integer to calculate its square: ";cin >> n;cout << "Square is " << square(n)//call to the function

<< endl;

system("pause");return 0;

}

Output Screens

Page 3: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 3/22

Part b)

Source Code

#include <iostream>using namespace std;

//takes an integer as argument and//returns its squareint square2(int &a){

a = a*a;return a;

}/////////////...............///////int main(){

int x;cout << "Enter x: ";cin >> x;cout << "Square of x is " << square2(x)//call to the function

<< endl;cout << "\nAnd the value of x is now " << x//the value of x has now changed

<< endl << endl;

system("pause");return 0;

}

Output Screens

Page 4: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 4/22

Part c)

Source Code

#include <iostream>using namespace std;

//takes an integer as argument and//returns its squareint square3(int* a){

(*a) = (*a)*(*a);return (*a);

}/////////////...............///////int main(){

int x;cout << "Enter x: ";cin >> x;

cout << "Square is " << square3(&x)//call to the function<< endl;

cout << "\nAnd the value of x is now " << x//the value of x has now beenchanged

<< endl << endl;

system("pause");return 0;

}

Output Screens

Page 5: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 5/22

Question No. 2

Source Code

#include <iostream>using namespace std;

//here come the declarations of all the functions//in this program. check function definitions for input//output details.....................................void get_operands(float&, float&);float add(float, float);float sub(float, float);float mul(float, float);float div(float, float);long double factorial(int);//////////////////....................\\\\\\\\\\\\\\\\\\\

void main(){

float op1, op2;//operands for binary operationsint n;//operand for factorialchar operation;//operation to perform on above operators

//prompt the user to enter an appropriate operatorcout << "What do you want to do?\n";cout << "Press + for addition" << endl;cout << "Press - for subtraction" << endl;cout << "Press * for multiplication" << endl;cout << "Press / for division" << endl;cout << "Press ! for factorial" << endl;cin >> operation;

//take and validate inputs(operands), carry out the//calculations and output resultsswitch(operation){case '+':

get_operands(op1, op2);cout << op1 << " + " << op2 << " = " << add(op1, op2) << endl;break;

case '-':get_operands(op1, op2);

Page 6: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 6/22

cout << op1 << " - " << op2 << " = " << sub(op1, op2) << endl;break;

case '*':get_operands(op1, op2);cout << op1 << " * " << op2 << " = " << mul(op1, op2) << endl;break;

case '/':

get_operands(op1, op2);if (op2 != 0)//don't accept zero in the denominator

cout << op1 << " / " << op2 << " = " << div(op1, op2) << endl;else

cout << "Division by zero is not allowed ...\n";break;

case '!':cout << "Enter the number whose factorial you want to calculate:\n";cin >> n;while (n < 0)//don't accept negative numbers{

cout << "Please enter a non-negative number to calculate itsfactorial!\n";

cin >> n;}cout << n << "! = " << factorial(n) << endl;break;

default:cout << "Sorry! We can't recognize " << operation << " as a valid

operator.\n";}system("pause");

}

//get_operands()//used to input operands from the user//for binary operators such add, subtract etc...

void get_operands(float &o1, float &o2){cout << "Please enter the first number: ";cin >> o1;cout << "Now enter the second number: ";cin >> o2;

}

//add()//returns the sum of two floating point numbers//pased as arguments to itfloat add(float o1, float o2){

return (o1+o2);

}

//sub()//takes two floating point numbers as arguments//subtracts second from the first and and returns//the resulting floating point numberfloat sub(float o1, float o2){

return (o1-o2);}

Page 7: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 7/22

//mul()//returns the product of two floating point numbers//passed as arguments as a floating point number.float mul(float o1, float o2){

return (o1*o2);

}

//div()//returns the floating point quotient//of the the two floating point numbers//passed as argumentsfloat div(float o1, float o2){

return (o1/o2);}

//factorial()//takes an integer as argument as returns//its returns its factorial as a long doublelong double factorial(int num){

long double fact=1;for (int i=2; i <= num; i++){

fact *= i;}return fact;

}

Output Screens

Page 8: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 8/22

Page 9: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 9/22

Question No. 3

Source Code

#include <iostream>#include <string>using namespace std;

//function declarations....................................\\\\\\//for input, output details, please//checkout the definitions of each function!!!!!!!!!float averagegpa(float[], int);void lt2gpaholders(float[], string[], int);string highestgpaholder(float[], string[], int);void displaygpa(float[], unsigned int[], int, unsigned int);void editgpa(float[], unsigned int[], int, unsigned int);///////////////////.............................\\\\\\\\\\\\\\\\\\\

int main(){const int size = 3;float Gpa[size];unsigned int Reg[size];string Name[size];int choice;for (int i=0; i<size; i++){

cout << "Enter Registration number of the student no. " << i+1 << ":";

cin >> Reg[i];cout << "Enter GPA of student no. " << i+1 << ": ";cin >> Gpa[i];

cin.ignore(1024,'\n');cout << "Enter Name of the sudent no. " << i+1 << ": ";getline(cin, Name[i]);

}do {cout << "\n\nPress 1 for Average GPA of class\n"

<< "Press 2 for displaying names of students whose GPA is less than2\n"

<< "Press 3 for displaying name of student whose GPA is highest\n"<< "Press 4 for displaying GPA of a particular student\n"

Page 10: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 10/22

<< "Press 5 for modifying GPA of a particular student\n"<< "Press 6 to exit the application\n";

cin >> choice;switch (choice){case 1:

cout << "Average GPA of class is " << averagegpa(Gpa, size) << ".\n";

break;case 2:

cout << "Here is the list of names of the students whose GPA\n"<< "is less 2.\n";

lt2gpaholders(Gpa, Name, size);break;

case 3:cout << "Name of the student with highest GPA is: "

<< highestgpaholder(Gpa, Name, size) << ".\n";break;

case 4:int regnumber;cout << "Enter the registration no. of the student whose GPA you want

to view:\n";cin >> regnumber;displaygpa(Gpa, Reg, size, regnumber);break;

case 5:cout << "Enter the registration no. of the student whose GPA you want

to modify:\n";cin >> regnumber;editgpa(Gpa, Reg, size,regnumber);break;

case 6:exit(0);

default:cout << "Invalid input\n";

}} while (choice != 6);return 0;

}

//averagegpa()//takes an array of floating point point numbers and its size//as arguments and returns the average of all numbersfloat averagegpa(float gpa[], int size){

float totalgpa = 0;for (int i=0; i<size; i++){

totalgpa += gpa[i];

}return (totalgpa/size);}

//lt2gpaholders(): stands for "less than 2 gpa holders"//takes an array of floating point numbers that stores the//gpas of some students while an other array of strings that contains//names of the same students and the common size of both these arrays as//arguments and prints out the names of all those students who//have gpa's less than 2.0

Page 11: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 11/22

void lt2gpaholders(float gpa[], string name[], int size){

for (int i=0; i<size; i++){

if (gpa[i] < 2.0)cout << name[i] << endl;

}

}

//highestgpaholder()//takes two arrays containing gpa and names of some students and the size//of these arrays as arguments an returns the name of that student who has//the highest gpa.string highestgpaholder(float gpa[], string name[], int size){

float highest_gpa = gpa[0];for (int i=1; i<size; i++){

if (gpa[i] > highest_gpa)highest_gpa = gpa[i];

}int j;for (j=0; j<size; j++){

if (gpa[j] == highest_gpa)break;

}return name[j];

}

//displaygpa()//prints out the gpa of the student whose registration is passed to it.//also prints an error message if the sent registration number does not exist in//available data. takes the arrays containing gpas and registration numbers

//of the students and the common size of these arrays as arguments too.void displaygpa(float gpa[], unsigned int reg[], int size, unsigned intregnumber){

int i;for (i=0; i<size; i++){

if (regnumber == reg[i]){

cout << "GPA of the student with Registration no. " << reg[i]<<

" is " << gpa[i] << ".\n";break;

}

}if (i == size){

cout << "The registration no. " << regnumber << " was not found inthe available data.\n";

}}

//editgpa()

Page 12: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 12/22

//enables one to edit the gpa of a perticular student whose registraion is passedto//it. Also prints out an error message if the supplied registration number doesnot exist//in the available data. Also takes the arays containing gpas and registrationnumbers of the//students and their common size as arguments.

void editgpa(float gpa[], unsigned int reg[], int size, unsigned int regnumber){

int j;for (j=0; j<size; j++){

if (regnumber == reg[j]){

break;}

}if (j == size){

cout << "The registration no. " << regnumber << " was not found inthe available data.\n";

}else{

cout << "Existing GPA of student with registration no. " << regnumber<< " is "

<< gpa[j] << ".\n";cout << "Enter new GPA for this student: ";cin >> gpa[j];cout << "Congratulations! You have successfully modified the GPA of

your required student.\n";}

}

Output Screen

Page 13: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 13/22

Page 14: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 14/22

Question No. 4

Source Code

#include <iostream>

using namespace std;

//function declarations//see function definitions at the end of this program//for input and output detailsvoid sortArray(int[], int);bool compareArrays(int[], int[], int);////////////////////..............\\\\\\\\\\\\\\\\\\\\

int main(){

const int size = 5;int a1[size], a2[size];bool decider = false;

for (int i=0; i<size; i++){

cout << "Enter element number " << i+1 << " of first set: ";cin >> a1[i];

}for (int i=0; i<size; i++){

cout << "Enter element number " << i+1 << " of second set: ";cin >> a2[i];

}

sortArray(a1, size);sortArray(a2, size);

decider = compareArrays(a1, a2, size);

if (decider)cout << "Both sets are exactly same!\n";

elsecout << "Sets are not same!\n";

system("pause");return 0;

}

//sortArray()//sorts the integer type array passed as argument along with its size//in ascending ordervoid sortArray(int a[], int size){

for (int i=0; i<(size-1); i++){

for (int j=0; j<(size-1); j++){

if (a[j] > a[j+1]){

int temp = a[j];a[j] = a[j+1];

Page 15: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 15/22

a[j+1] = temp;}

}}

}

//compareArrays

//compare the elements of two similarly sorted integer type arrays//passed to it along with their common size.//returns true if all elemnts in the arrays are same and returns//false otherwise.bool compareArrays(int a1[], int a2[], int size){

bool same=true;for (int i=0; i<size; i++){

if (a1[i] != a2[i]){

same = false;break;

}}return same;

}

Output Screens

Page 16: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 16/22

Question No. 5

Source Code

#include <iostream>using namespace std;

//function declarationfloat average(float[], int);

int main(){

const int size=10;float testArray[size];cout << "Enter " << size << " floating point numbers to calculate their

average:\n";for (int i=0; i<size; i++)

{cin >> testArray[i];

}cout << "The average of these numbers is " << average(testArray, size) <<

endl;system("pause");return 0;

}

//takes a linear float type array and returns the//average of all of its elements also in type float.float average(float numbers[], int size){

long double sum=0;

for (int i=0; i<size; i++)sum+=numbers[i];

return (static_cast<float>(sum/size));}

Output Screen

Page 17: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 17/22

Question No. 6

Source Code

#include <iostream>

using namespace std;

//function declarationdouble average(int[][5], int);

int main(){

const int rows=2, cols=5;int testArray[rows][cols];cout << "Fill out a " << rows << "X" << cols << " matrix of integers:\n";for (int i=0; i<rows; i++)

for (int j=0; j<cols; j++)cin >> testArray[i][j];

cout << "The average of these numbers is " << average(testArray, rows) <<

endl;system("pause");return 0;

}

//average()//calculates the average of all the elements of a two dimensional array//containing 5 columns passed as an argument along with the number of rows//and returns that averagedouble average(int numbers[][5], int rows){

long double sum=0;for (int i=0; i<rows; i++)

for (int j=0; j<5; j++)

sum+=numbers[i][j];return (static_cast<double>(sum/(rows*5)));

}

Output Screen

Page 18: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 18/22

Question No. 7

Source Code

#include <iostream>using namespace std;

//function declarationbool issymmetric(int[5][5]);

int main(){

bool decider;int matrix[5][5];cout << "Enter a 5X5 matrix of integers to check if it is symmetric or

not:\n";for (int i=0; i<5; i++)

for (int j=0; j<5; j++)cin >> matrix[i][j];

decider = issymmetric(matrix);if (decider)

cout << "It is a symmetric matrix.\n";else

cout << "It is not a symmetric matrix.\n";

system("pause");return 0;

}

//issymmetric()//takes a two dimensional integer type array i.e. a matrix as argument//returns true if this matrix is symmetric and returns false otherwisebool issymmetric(int m[5][5]){

bool symmetric=true;for(int i=0; i<5; i++){

for (int j=i+1; j<5; j++){

if (m[i][j] != m[j][i]){

symmetric=false;break;

}}if (!symmetric)

break;

Page 19: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 19/22

}return symmetric;

}

Output Screens

Question No. 8

Source Code

#include <iostream>using namespace std;

//function declarations.int gcd(int, int);int gcditerative(int, int);///////.........\\\\\\\\

int main(){

int num1, num2;cout << "Enter two numbers to calculate their greatest common divisor:\n";cin >> num1 >> num2;cout << "Greatest Common Divisor = " << gcd(num1, num2) << endl;cout << "Same thing by iterative version of the function = "

<< gcditerative(num1, num2) << endl;system("pause");return 0;

}

Page 20: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 20/22

//gcd()//calculates and returns the greatest common devisior of two integers passed to//it and it does so RECURSIVELY.int gcd(int p, int q){

int r;r=p%q;

if(r == 0)return q;

elsereturn gcd(q, r);

}

//gcditerative()//calculates and returns the greatest common devisior of two integers passed to//it and it does so ITERATIVELY.int gcditerative(int p, int q){

int r;r=p%q;while (r != 0){

p=q;q=r;r=p%q;

}return q;

}

Output Screens

Page 21: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 21/22

Question No. 9

Source Code

#include <iostream>#include <string>using namespace std; //function declaration

string dec_to_binary(int);

int main(){

int number;cout << "Enter a decimal integer to convert it into a binary number:\n";cin >> number;cout << dec_to_binary(number) << endl;system("pause");return 0;

} //dec_to_binary()//takes an integer as an argument and

//returns its binary equivalent in the//form of a string of charactersstring dec_to_binary(int num){

if (num==0)return "0";

else if (num==1)return "1";

else if (num<0)return "-"+(dec_to_binary(-num));

elsereturn (dec_to_binary(num/2)+static_cast<char>((num%2)+48));

}

Output Screens

Page 22: Lab Ten 1378

8/6/2019 Lab Ten 1378

http://slidepdf.com/reader/full/lab-ten-1378 22/22