CS201 Assignment 2 Solution and Discussion


  • Cyberian's Gold

    Re: CS201 Assignment 2 Solution and Discussion

    Assignment No.  2
    

    Semester: Spring 2020
    CS201 – Introduction to Programming Total Marks: 20

    Due Date:
    16-6-2020

    Instructions
    Please read the following instructions carefully before submitting assignment:
    It should be clear that your assignment will not get any credit if:

    o Assignment is submitted after due date.
    o Submitted assignment does not open or file is corrupt.
    o Assignment is copied (From internet/students).

    Software allowed to develop Assignment

    • Dev C++

    Objectives:
    In this assignment, the students will learn:
    • How to fill / populate a two-dimensional array using given source data.
    • How to write user defined functions and pass an array to them as parameter.
    • How to use if statement and do while loop.
    • How to calculate the percentage in elements of array.
    • How to display array elements.

    Assignment Submission Instructions
    You are required to submit only .cpp file on the assignments interface of CS201 at VU-LMS. Assignment submitted in any other format will not be accepted and will be graded zero marks.

    Problem Statement
    Write a menu in C++ which should show the country code and name at the start of application:

    1. Press 0 for Pakistan.
    2. Press 1 for China.
    3. Press 2 for Italy.
    4. Press 3 for UK.
    5. Press 4 for Iran.
    6. Press 5 for France.
    7. Press 6 for Turkey.
    8. Press 7 to exit.
      Instructions to write C++ program:

     Write functions to calculate % of recovered patients, % of deaths in specific country . Following function names should be used for consistency.

    To display all elements in matrix form showElements( );
    Percentage of death PercentageDeath ( ):
    Percentage of recovered PercentageRecovered();

    Source data:

    Country Code Total Cases Total Deaths Total Recovered
    0 560,433 22,115 32,634
    1 156,363 19,899 34,211
    2 84,279 10,612 0
    3 82,160 3,341 77,663
    4 71,686 4,474 43,894
    5 56,956 1,198 3,446
    6 5,374 93 1095

    Sample Output:
    First, user will call the showElements() method to display all the data in matrix form:
    Then, it will display a menu to calculate the percentage of dead and recovered persons for specific country:

    f0a03ad3-d28d-412c-a2d8-e73dcc7d35da-image.png

    If user presses 0 then it will show the percentage of dead and recovered persons for Pakistan:
    be43d8d9-feaf-4fd8-be53-2d0e0b95a0d3-image.png

    If user presses 1 then it will show the percentage of dead and recovered persons for China:
    9daeed49-a07e-4ee4-86a7-cb0af8f44ade-image.png

    If the user enters option other then 0 to 7. Following messages display:
    Choice should be between 0 and 7
    Invalid choice, please select again:
    ba8aa527-baf7-4930-94f4-969cdfa62456-image.png

    Good Luck

    Lectures Covered: This assignment covers Lecture # 11-15.
    Deadline: The deadline to submit your assignment solution is 16-6-2020. Your assignment must be submitted within the due date through VU-LMS. No assignment will be accepted through email after the due date.


  • Cyberian's Gold

    @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";
    }
    
    
    
    


  • code please?



Quiz 100% Result Quiz 100% Result
| |