Programming Fundamentals Lab: Spring 2011
Programming Fundamentals Lab: Spring 2011
Output Screens
Output Screens
Output Screens
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 operations int n;//operand for factorial char operation;//operation to perform on above operators //prompt the user to cout << "What do you cout << "Press + for cout << "Press - for cout << "Press * for cout << "Press / for cout << "Press ! for cin >> operation; enter an appropriate operator want to do?\n"; addition" << endl; subtraction" << endl; multiplication" << endl; division" << endl; factorial" << endl;
//take and validate inputs(operands), carry out the //calculations and output results switch(operation) { case '+': get_operands(op1, op2); cout << op1 << " + " << op2 << " = " << add(op1, op2) << endl; break; case '-': get_operands(op1, op2);
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 its factorial!\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 it float 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 number float sub(float o1, float o2) { return (o1-o2); }
//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 arguments float div(float o1, float o2) { return (o1/o2); } //factorial() //takes an integer as argument as returns //its returns its factorial as a long double long double factorial(int num) { long double fact=1; for (int i=2; i <= num; i++) { fact *= i; } return fact; }
Output Screens
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 than 2\n" << "Press 3 for displaying name of student whose GPA is highest\n" << "Press 4 for displaying GPA of a particular student\n"
<< "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 numbers float 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
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 int regnumber) { 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 in the available data.\n"; } } //editgpa()
//enables one to edit the gpa of a perticular student whose registraion is passed to //it. Also prints out an error message if the supplied registration number does not exist //in the available data. Also takes the arays containing gpas and registration numbers 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 in the 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
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 details void 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"; else cout << "Sets are not same!\n"; system("pause"); return 0;
//sortArray() //sorts the integer type array passed as argument along with its size //in ascending order void 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];
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
Question No. 5
Source Code
#include <iostream> using namespace std; //function declaration float 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
Question No. 6
Source Code
#include <iostream> using namespace std; //function declaration double 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 average double 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
Question No. 7
Source Code
#include <iostream> using namespace std; //function declaration bool 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 otherwise bool 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;
} 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; }
//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; else return 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
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 characters string 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)); else return (dec_to_binary(num/2)+static_cast<char>((num%2)+48)); }
Output Screens