Skip to content

CS201 - Introduction to Programming

8 Topics 55 Posts
  • 0 Votes
    8 Posts
    4k Views
    cyberianC

    modules:composer.user_said_in, @cyberian, CS201 Assignment 1 Solution and Discussion

    Write a C++ program that performs the following tasks:
    1-Print your name and VU id.
    2-Add last 3 digit of your VU id.
    3-Display the result of sum on screen.
    4-Use if-else statement ::
    a) If sum is odd then print your name using while loop. Number of iterations of while loop should be equal to the sum.
    b) If sum is even then print your VU id using while loop. Number of iterations of while loop should be equal to the sum.
    [use remainder operator on sum value to determine the odd and even value for if condition]
    For example, suppose the student id is BC123456781. Then by adding last 3 digits of vu id, we get 16 which is an even number. In this case, program should print your VU ID for 16 times using while loop.

    // C201 Assignment No: 1 Fall2021 #include <iostream> #include <string> using namespace std; void printnameid(string studentid, string studentname); int calculatelastthreedigits(string studentid); int main() { string studentid="bc123456789"; string studentname="ZAHID"; printnameid(studentid,studentname); int TotalLastThreeDigits=calculatelastthreedigits(studentid); int counter=1; int a,b,c; a=6; b=4; c=0; cout<<"Sum of Last Three Numbers ="<<a+b+c; cout<<"\n\n"; if ( TotalLastThreeDigits % 2 == 0) // Divide by 2 and see if the reminder is zero? then it is even otherwise it is odd number { cout << "the sum is an even value: \n\n"; cout<<"++++++++++++++++++++++++++++++++++++++++++++ \n\n"; while(counter <= TotalLastThreeDigits) { cout << " Iteration: " << counter << "\n"; cout << "My student id is:" << studentid << "\n"; counter++; } } else { cout << "the sum is an odd value: \n\n"; while(counter <= TotalLastThreeDigits) { cout << " Iteration: " << counter << "\n"; cout << "My name is " << studentname << "\n"; counter++; } } return 0; } void printnameid(string studentid, string studentname){ cout<<"My name is " << studentname << "\n\n\n"; cout<<"My student id is:" << studentid << "\n\n\n"; } int calculatelastthreedigits(string studentid) { int end=studentid.length(); // Ending point that is total length of string int start=end-3; // Starting point string lastthreedigits=studentid.substr(start,end); // Trim the last three digits; int total=0; //Calculate the sum of last three digits for ( int index=0; index < lastthreedigits.length(); index++) { total += lastthreedigits[index] - '0'; } return total; // return the total to calling statement. }
  • 0 Votes
    5 Posts
    625 Views
    zaasmiZ

    CS201 ASSIGNMENT 3 SOLUTION SPRING 2021

    #include <iostream> using namespace std; #define PI 3.14159265 class Circle { private: double radius; public: void setRadius(); void computeAreaCirc(); Circle(); ~Circle(); }; Circle::Circle() { radius = 0.0; } void Circle::setRadius() { radius = 5.6; } void Circle::computeAreaCirc() { cout << "Area of circle is: " << PI * (radius * radius) << endl; cout << "Circumference of circle is: " << 2 * PI * radius << endl; } Circle::~Circle() { } class Rectangle { private: double length; double width; public: void setLength(); void setWidth(); void computeArea(); Rectangle(); ~Rectangle(); }; Rectangle::Rectangle() { length = 0.0; width = 0.0; } void Rectangle::setLength() { length = 5.0; } void Rectangle::setWidth() { width = 4.0; } void Rectangle::computeArea() { cout << "Area of Rectangle: " << length * width << endl; } Rectangle::~Rectangle() { } main() { cout<<"********************SCIENTIFIC CALCULATOR********************"<<endl; cout<<""<<endl; int run = 1; string option, choice; while(run) { cout << "\nOPTION 1 for computing Area and Circumference of the circle" << endl; cout << "OPTION 2 for computing Area of the Rectangle" << endl; cout << "Select your desired option(1-2): "; cin >> option; if(option == "1") { Circle nCircle; nCircle.setRadius(); nCircle.computeAreaCirc(); cout << "Do you want to perform anyother calculation(Y/N):"; cin >> choice; if(choice == "Y" || choice == "y") { continue; } else { break; } } else if(option == "2") { Rectangle nRectangle; nRectangle.setLength(); nRectangle.setWidth(); nRectangle.computeArea(); cout << "Do you want to perform anyother calculation(Y/N):"; cin >> choice; if(choice == "Y" || choice == "y") { continue; } else { break; } } else { cout << "Invalid Option!, Option should be from (1-2)" << endl; } } }
  • 0 Votes
    4 Posts
    1k Views
    zaasmiZ
    // CS201 Assignment 2 Solution Spring 2021 #include <iostream> #include <conio.h> using namespace std; int ShowMatrix() { //main matrix int row=2, column=2; int matrix[2][2] = { {8, -4} , {-6, 2} }; cout<<"The matrix is:"<<endl; for(int i=0; i<row; ++i) { for(int j=0; j<column; ++j) cout<<matrix[i][j]<<" "; cout<<endl; } } //Transpose int showTranspose ( ) { int transpose[2][2], row=2, column=2, i, j; int matrix[2][2] = { {8, -4} , {-6, 2} }; cout<<endl; for(i=0; i<row; ++i) for(j=0; j<column; ++j) { transpose[j][i] = matrix[i][j]; } cout<<"The transpose of the matrix is:"<<endl; for(i=0; i<column; ++i) { for(j=0; j<row; ++j) cout<<transpose[i][j]<<" "; cout<<endl; } } //determenent calculating int calculateDeterminant() { int determMatrix[2][2], determinant; int matrix[2][2] = { {8, -4} , {-6, 2} }; determinant = ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0])); cout << "\nThe Determinant of 2 * 2 Matrix = " << determinant; } //Adjoint of matrix int showAdjoint() { int ch,A2[2][2] = {{8,-4},{-6,2}},AD2[2][2],C2[2][2]; //Calculating co-factors of matrix of order 2x2 C2[0][0]=A2[1][1]; C2[0][1]=-A2[1][0]; C2[1][0]=-A2[0][1]; C2[1][1]=A2[0][0]; //calculating ad-joint of matrix of order 2x2 AD2[0][0]=C2[0][0]; AD2[0][1]=C2[1][0]; AD2[1][0]=C2[0][1]; AD2[1][1]=C2[1][1]; cout<<"\n\nAdjoint of A is :- \n\n"; cout<<"|\t"<<AD2[0][0]<<"\t"<<AD2[0][1]<<"\t|\n|\t"<<AD2[1][0]<<"\t"<<AD2[1][1]<<"\t|\n"; } int main() { int cho = 0; cout<<" ||---Enter your choice---||"<<endl; cout<<""<<endl; cout<<"---Press 1 to display the matrix and its transpose---"<<endl; cout<<"---Press 2 to find adjoint and determinant of the matrix---"<<endl; cout<<""<<endl; cout<<"Press any other key to exit."; cout<<""<<endl; cin>>cho; if (cho ==1) { ShowMatrix(); showTranspose ( ); } else if (cho == 2) { showAdjoint(); calculateDeterminant(); } else system("pause"); }
  • 0 Votes
    3 Posts
    268 Views
    zaasmiZ

    @zaasmi said in CS201 Assignment 2 Solution and Discussion:

    code please?

    #include<iostream> using namespace std; // Declaration of function showElements void showElements(long s[][4]); // Declaration of function PercentageDeath void PercentageDeath(long s[][4], int i); // Declaration of function PercentageRecovered void PercentageRecovered(long s[][4], int i); main() { cout<<"\n\nCS201 Assignment No. 2 Solution \n\n"; long source_data[7][4]= {0,560433, 22115, 32634, 1,156363, 19899, 34211, 2,84279, 10612, 0, 3,82160, 3341, 77663, 4,71686, 4474, 43894, 5,56956, 1198, 3446, 6,5374, 93, 109}; showElements(source_data); int user_choice; do { cout<<"\nPress the country code to calculate percentage of dead and recovered persons\n"; cout<<"\n*** Press 0 for Pakistan ***"; cout<<"\n*** Press 1 for China ***"; cout<<"\n*** Press 2 for Italy ***"; cout<<"\n*** Press 3 for UK ***"; cout<<"\n*** Press 4 for Iran ***"; cout<<"\n*** Press5 for France ***"; cout<<"\n*** Press 6 for Turkey ***"; cout<<"\n*** Press 7 to Exit ***"; cout<<"\n\nPlease select an option use number from 0 to 7 : "; input: cin>>user_choice; if(user_choice>=0 && user_choice<=6) { PercentageDeath(source_data, user_choice); PercentageRecovered(source_data, user_choice); } else if(user_choice<0 || user_choice>7) { cout<<"\n\nChoice should be between 0 to 7 "; cout<<"\ninvalid choice ! please select again : "; goto input; } }while(user_choice!=7); } // definition of function showElements void showElements(long s[][4]) { cout<<"Source Data : \n\n"; cout<<"Country\tCases\tDeaths\tRecovered\n\n"; for(int i=0; i<7; i++) { for(int j=0; j<4; j++) { cout<<s[i][j]<<"\t"; } cout<<"\n"; } } // definition of function PercentageDeath void PercentageDeath(long s[][4], int i) { float d_rate=(float)100*s[i][2]/s[i][1]; cout<<"\nPercentage of death is "<<d_rate; } // definition of function PercentageRecovered void PercentageRecovered(long s[][4], int i) { float r_rate=(float)100*s[i][3]/s[i][1]; cout<<"\n\nPercentage of recocered is "<<r_rate<<"\n"; }
  • 0 Votes
    4 Posts
    708 Views
    cyberianC
    #include <iostream> #include <stdlib.h> using namespace std; main() { int choice = 0, salary = 0; float increment = 0.0, tax = 0.0, newsal = 0.0; cout<<"\n ********* SALARY CALCULATOR *********\n"<<endl; cout<<" *************************************\n"<<endl; cout<<" ********* Enter 1 for SPS6 *********\n"<<endl; cout<<" ********* Enter 2 for SPS7 *********\n"<<endl; cout<<" ********* Enter 3 for SPS8 *********\n"<<endl; cout<<" ********* Enter 4 for SPS9 *********\n"<<endl; cout<<" Select a pay scale from the menu: "; cin>>choice; switch(choice){ case 1: salary = 40000; increment = salary * 20/100; newsal = salary + increment; tax = newsal * 3/100; break; case 2: salary = 60000; increment = salary * 15/100; newsal = salary + increment; tax = newsal * 3/100; break; case 3: salary = 80000; increment = salary * 10/100; newsal = salary + increment; tax = newsal * 3/100; break; case 4: salary = 100000; increment = salary * 5/100; newsal = salary + increment; tax = newsal * 3/100; break; default: cout<<" Selected choice is invalid."<<endl<<endl; } if(choice >= 1 && choice <=4) { cout<<" Initial Salary: "<<salary<<endl; cout<<" Incremented Amount: "<<increment<<endl; cout<<" Increased Salary: "<<newsal<<endl; cout<<" Tax Deduction: "<<tax<<endl; cout<<" Net Salary: "<<newsal-tax<<endl<<endl; } system("pause"); }
  • 0 Votes
    7 Posts
    935 Views
    zareenZ

    Ideas Solution 2:

    #include<iostream> #include<fstream> #include<stdio.h> using namespace std; class Employee{ private: int code; char name[20]; float salary; public: void read(); void display(); int getEmpCode() { return code;} int getSalary() { return salary;} void updateSalary(float s) { salary=s;} }; void Employee::read(){ cout<<"Enter employee code: "; cin>>code; cout<<"Enter name: "; cin.ignore(1); cin.getline(name,20); cout<<"Enter salary: "; cin>>salary; } void Employee::display() { cout<<code<<" "<<name<<"\t"<<salary<<endl; } fstream file; void deleteExistingFile(){ remove("EMPLOYEE.DAT"); } void appendToFille(){ Employee x; x.read(); file.open("EMPLOYEE.DAT",ios::binary|ios::app); if(!file){ cout<<"ERROR IN CREATING FILE\n"; return; } file.write((char*)&x,sizeof(x)); file.close(); cout<<"Record added sucessfully.\n"; } void displayAll(){ Employee x; file.open("EMPLOYEE.DAT",ios::binary|ios::in); if(!file){ cout<<"ERROR IN OPENING FILE \n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))) if(x.getSalary()>=10000 && x.getSalary()<=20000) x.display(); } file.close(); } void searchForRecord(){ Employee x; int c; int isFound=0; cout<<"Enter employee code: "; cin>>c; file.open("EMPLOYEE.DAT",ios::binary|ios::in); if(!file){ cout<<"ERROR IN OPENING FILE \n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))){ if(x.getEmpCode()==c){ cout<<"RECORD FOUND\n"; x.display(); isFound=1; break; } } } if(isFound==0){ cout<<"Record not found!!!\n"; } file.close(); } void increaseSalary(){ Employee x; int c; int isFound=0; float sal; cout<<"enter employee code \n"; cin>>c; file.open("EMPLOYEE.DAT",ios::binary|ios::in); if(!file){ cout<<"ERROR IN OPENING FILE \n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))){ if(x.getEmpCode()==c){ cout<<"Salary hike? "; cin>>sal; x.updateSalary(x.getSalary()+sal); isFound=1; break; } } } if(isFound==0){ cout<<"Record not found!!!\n"; } file.close(); cout<<"Salary updated successfully."<<endl; } void insertRecord(){ Employee x; Employee newEmp; newEmp.read(); fstream fin; file.open("EMPLOYEE.DAT",ios::binary|ios::in); fin.open("TEMP.DAT",ios::binary|ios::out); if(!file){ cout<<"Error in opening EMPLOYEE.DAT file!!!\n"; return; } if(!fin){ cout<<"Error in opening TEMP.DAT file!!!\n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))){ if(x.getEmpCode()>newEmp.getEmpCode()){ fin.write((char*)&newEmp, sizeof(newEmp)); } fin.write((char*)&x, sizeof(x)); } } fin.close(); file.close(); rename("TEMP.DAT","EMPLOYEE.DAT"); remove("TEMP.DAT"); cout<<"Record inserted successfully."<<endl; } int main() { char ch; deleteExistingFile(); do{ int n; cout<<"ENTER CHOICE\n"<<"1.ADD AN EMPLOYEE\n"<<"2.DISPLAY\n"<<"3.SEARCH\n"<<"4.INCREASE SALARY\n"<<"5.INSERT RECORD\n"; cout<<"Make a choice: "; cin>>n; switch(n){ case 1: appendToFille(); break; case 2 : displayAll(); break; case 3: searchForRecord(); break; case 4: increaseSalary(); break; case 5: insertRecord(); break; default : cout<<"Invalid Choice\n"; } cout<<"Do you want to continue ? : "; cin>>ch; }while(ch=='Y'||ch=='y'); return 0; }
  • 0 Votes
    2 Posts
    1k Views
    mehwishM

    Qno 1: Function Overloading Mean…

    1 ) The name of the function will Remain the same but its behavior may change.

    The Name of the Function will not remain same but its bahavior will not be change.

    Making a function inline.

    Function Name can be change.

    Qno 2: sorry i forget.

    Qno 3: For accessing data member we use … operator.

    Plus+. Multiplication* Dot. Division/.

    Qno4: Constructor has…

    No name. The same name as of class. The same Name as Data Member. Return type.

    Qno5: Free Function is Avaliable in… header file.

    Conio.h Iostream.h String.h Stdlib.h

    Qno6: New operator can Be used for…

    Only integer Data type. Only char and integer data type. Integer, flot, char and double data type. Dot operator.

    Qno7: The Malloc function takes…Arguments.

    Two Three Four One.

    Qno8: Macros are categorized into…types.

    One Four Two None of the given options.

    Qno9: class can be define as…

    A class include both data member as well as Function to manipulate that data. A class include only object, A class include both object and structure. A class does not include Data member and Function.

    Qno10: A class is…

    A built in Function. A user Define data type. An Array. A member function.
  • 0 Votes
    4 Posts
    2k Views
    zareenZ

    Ideas Solution Code

    #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; const int rows = 10; const int cols = 10; int selectOption(){ int choice = 0; cout<<"Press 1 to populate a two-dimensional array with integers from 1 to 100\n"; cout<<"Press 2 to display the array elements\n"; cout<<"Press 3 to display the largest element present in the array along with its row and column index\n"; cout<<"Press 4 to find and show the transpose of the array\n"; cout<<"Press 5 To Exit\n"; cout<<"\nPlease select an option, use numbers from 1 to 5: "; do { cin>>choice; if(choice >= 1 && choice <= 5){ break; } else { cout<<"\nChoice should be between 1 and 5\n"; cout<<"Invalid choice, please select again: "; } } while(true); cout<<"___________________________________________________\n"; return choice; } // end selectOption function void populateArray(int data[rows][cols]){ srand(time(0)); for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ data[i][j] = rand() % 100 + 1; } } cout<<"Array populated sucessfully\n"; } // end of poulateArray function void showElements(int data[rows][cols]){ for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ cout<<data[i][j]<<"\t"; } cout<<endl; } } // end of showElements function void showLargestElement(int data[rows][cols]){ int largest = 1, row =0, col = 0; for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(data[i][j] > largest){ largest = data[i][j]; row = i; col = j; } } } cout<<"Largest element is "<<largest<<" which is at row = "<<row+1<<" or index = "<<row<<" and column "<<col+1<<" or index "<<col<<endl; } // end of showLargestElement function void transposeArray(int data[rows][cols]){ for(int i = 0; i < cols; i++){ for(int j = 0; j < rows; j++){ cout<<data[j][i]<<'\t'; } cout<<endl; } } // end of transposeArray function main(){ int choice = 0, data[rows][cols] = {0}; do{ choice = selectOption(); switch(choice){ case 1: cout<<endl; populateArray(data); cout<<endl; break; case 2: if(data[0][0] == 0){ cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl; continue; } cout<<endl; showElements(data); cout<<endl; break; case 3: if(data[0][0] == 0){ cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl; continue; } cout<<endl; showLargestElement(data); cout<<endl; break; case 4: if(data[0][0] == 0){ cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl; continue; } cout<<endl; transposeArray(data); cout<<endl; break; } }while(choice != 5); // end of do-while loop } // end of main function