Fall 2019 - CS201 - 2

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Assignment No.

2 Total Marks: 20
Semester: Fall 2019 Due Date:
CS201 – Introduction to Programming 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.

Copyright © Virtual University of Pakistan


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( );

Copyright © Virtual University of Pakistan


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:

See this message!

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

The User Presses 1, then


the array populated!

Copyright © Virtual University of Pakistan


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

Displaying array elements


upon pressing 2!

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:

See this output when the


user presses option 3

Copyright © Virtual University of Pakistan


Transpose of the array
after pressing 4!

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.

Copyright © Virtual University of Pakistan

You might also like