CS201 Assignment 2 Solution and Discssion


  • Cyberian's Gold

    Assignment No. 2
    Semester: Fall 2019
    CS201 – Introduction to Programming Total Marks: 20

    Due Date:
    02-12-2019

    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 random numbers.
    • How to write user defined functions and pass an array to them as parameter.
    • How to implement switch statement to process specific options.
    • How to compare elements of array.
    • How to find transpose of an array.

    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.

    An array of arrays is known as two-Dimensional array. In C++ it is also known as matrix. A matrix can be represented as a table of rows and columns. Like single dimensional arrays, we can store same type of data in it. Elements stored in array can be accessed using array index. Index starts from [0][0]. Means the index of row and column starts from 0. Following is an example of a two-dimensional array declaration and initialization having 3 rows and 3 columns.

    int mularray[3][3] = {         //Declaration and initialization of a 2D Array
    {1, 2, 3},
    {5, 7, 9},
    {0, 2, 4}
    };
    // The value of mularray [0][0] is 1
    // The value of mularray [0][1] is 2
    // The value of mularray [1][2] is 9
    // The value of mularray [2][1] is 2
    

    Problem Statement
    Write an option-based program in C++ which should show following four options to the user at the start of application:

    1. Press 1 to populate a two-dimensional array with integers from 1 to 100.
    2. Press 2 to display the array elements.
    3. Press 3 to display the largest element present in the array along with its row and column index.
    4. Press 4 to find and show the transpose of the array.
      Instructions to write C++ program:

     Use random number function to populate the array i.e. rand( ). Range should be 1 to 100.
    Hint: use srand( ) function before rand( ) to seed the random number. In loop, rand( ) may generate same numbers so you should use srand(time(0)) before rand( ) to generate different random numbers. To use time(0) you may need to include time.h header file.
     Switch statement will be implemented to perform multiple conditions and to perform different actions based on the conditions. i.e. Option 1, 2, 3 and 4.
     Write user defined functions to perform tasks given in options. Following function names should be used for consistency.

    To populate the array poulateArray( );
    To display all elements of Array showElements( );
    To show largest element in the array showLargestElement( );
    To show transpose of the array transposeArray( );

    Screenshots for Guidance:

    1 If the user choses any option other than “1” at the start when the array is empty, the user should get a message like “Sorry the array is empty, first populate it by pressing 1 to perform this task ". See the following sample output:

    5f5817c6-e506-4fdc-a8d7-82e07ba655b4-image.png

    2 Upon pressing 1, a message like “Array has been populated successfully!" should be displayed. See the following sample output:

    2b1e1975-d11f-4284-b312-1c896e972296-image.png

    3 Array elements should be displayed when the user presses 2. See the following screenshot for it:

    f4578b3b-b4f7-4ecb-a05a-afe8f17e0a27-image.png

    4 Largest number along with its row and column number can be found by pressing 3. Similarly, if the user presses 4 transpose of the array should be displayed. See the sample outputs for both options:

    6166f03c-44bc-4148-8a01-a37afa3ac067-image.png

    Good Luck!

    Lectures Covered: This assignment covers Lecture # 11-15.
    Deadline: The deadline to submit your assignment solution is 02-12-2019. 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

    Ideas Solution Code

    #include<iostream>
    #include<time.h>
    #include<stdlib.h>
    void poulateArray(int [][10]);
    void showElements(int [][10]);
    void showLargestElement(int [][10]);
    void transposeArray(int [][10]);
    using namespace std;
    int main()
    {
    int option,arr[10][10]= {0};
    bool arrEmpty=true;
    while(1)
    {
    cout<<"1.Press 1 to populate a two-dimensional array with integers
    from 1 to 100.\n"
    <<"2.Press 2 to display the array elements.\n"
    <<"3.Press 3 to display the largest element present in the array along
    with its row and column index.\n"
    <<"4.Press 4 to find and show the transpose of the array.\n\n"
    
    <<"Please select a option, use numbers from 1 to 5: ";
    cin>>option;
    cout<<"\n--------------------------------------------------------\n\n";
    switch(option)
    {
    case 1:
    poulateArray(arr);
    arrEmpty=false;
    break;
    case 2:
    if(arrEmpty==true)
    {
    cout<<"Sorry the array is empty, first populate it by pressing 1 to
    perform this task\n\n\n\n";
    }
    else
    {
    showElements(arr);
    }
    break;
    case 3:
    if(arrEmpty==true)
    
    {
    cout<<"Sorry the array is empty, first populate it by pressing 1 to
    perform this task\n\n\n\n";
    }
    else
    {
    showLargestElement(arr);
    }
    break;
    case 4:
    if(arrEmpty==true)
    {
    cout<<"Sorry the array is empty, first populate it by pressing 1 to
    perform this task\n\n\n\n";
    }
    else
    {
    transposeArray(arr);
    }
    break;
    case 5:
    exit(0);
    
    break;
    default:
    cout<<"You entered wrong option.\n\n";
    }
    }
    return 0;
    }
    void poulateArray(int ar[][10])
    {
    int num;
    srand(time(0));
    for(int i=0; i<10; i++)
    {
    for(int j=0; j<10; j++)
    {
    num=1+rand()%100;
    ar[i][j]=num;
    }
    }
    cout<<"Array has been populated successfully!\n\n";
    }
    void showElements(int ar[][10])
    
    {
    for(int i=0; i<10; i++)
    {
    for(int j=0; j<10; j++)
    {
    cout<<ar[i][j]<<"\t";
    }
    cout<<endl;
    }
    }
    void showLargestElement(int ar[][10])
    {
    int largest=0,i,j,row,colum;
    for( i=0; i<10; i++)
    {
    for( j=0; j<10; j++)
    {
    if(ar[i][j]>largest)
    {
    largest=ar[i][j];
    row=i+1;
    colum=j+1;
    
    }
    }
    }
    cout<<"Largest element is "<<largest<<" which is at row "<<row<<" and
    colum "<<colum<<"\n\n";
    }
    void transposeArray(int ar[][10])
    {
    for(int i=0; i<10; i++)
    {
    for(int j=0; j<10; j++)
    {
    cout<<ar[j][i]<<"\t";
    }
    cout<<"\n";
    }
    }
    

  • Cyberian's Gold


  • Cyberian's Gold



    Recent Topics


  • 7
  • 1
  • 10
  • 2
  • 3
  • 3
  • 2
  • 4
|