Skip to content

CS201 - Introduction to Programming

3 Topics 56 Posts
  • 0 Votes
    4 Posts
    3k 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"); }
  • CS201 Assignment 2 Solution and Discussion

    Solved assignment 2 cs201 discussion solution spring 2020
    3
    0 Votes
    3 Posts
    1k 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"; }
  • CS201 Assignment 2 Solution and Discssion

    Solved cs201 solution discussion fall 2019 assignment 2
    4
    0 Votes
    4 Posts
    4k 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
Reputation Earning
How to Build a $1,000/Month World CUP LIVE Matches Live Cricket Streaming
Ads
File Sharing
Stats

0

Online

3.0k

Users

2.8k

Topics

8.5k

Posts
Popular Tags
Online User
| |