Skip to content

CS201 - Introduction to Programming

4 Topics 56 Posts
  • 0 Votes
    8 Posts
    7k 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
    7 Posts
    3k 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; }
  • cs201 assignment no 3 soultion fall 2019 needed

    Locked Moved fall 2019
    2
    0 Votes
    2 Posts
    816 Views
    mehwishM
    Duplicate topic please check discussion! https://cyberian.pk/topic/931/cs201-assignment-3-solution-and-discussion/4
  • 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
| |