Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Mohammad Ali Jinnah University Islamabad

Department of Computer Science,


Faculty of Computing

Lab Manual for Computer Programming

Lab 11: Single Dimensional Arrays and Sorting


Lab 11: Single Dimensional Arrays and Sorting

Table of Contents
1. Introduction 95

2. Activity Time-boxing 95

3. Objective of the Experiment 95

4. Concept Map 96
4.1 Single-Dimensional Arrays 96
4.2 Introduction to Multi-Dimensional Arrays 97
4.3 Sorting 97
4.4 Summary 97

5. Home work before Lab 98


5.1 Problem Solution Modeling 98
5.2 Practices from home 98

6. Procedure & Tools 98


6.1 Tools 98
6.2 Setting-up Visual Studio 2008 98
6.3 Walkthrough Task 99

7. Practice Tasks 100


7.1 Practice Task 1 100
7.2 Practice Task 2 100
7.3 Practice Task 3 101
7.4 Out comes 101
7.5 Testing 101

8. Evaluation Task (Unseen) 102

9. Evaluation Criteria 102

10. Further Readings 102


10.1 Slides 102

Department of computer Science, P a g e | 94


MAJU, 2013
Lab 11: Single Dimensional Arrays and Sorting

Lab 11: Single Dimensional Arrays, Sorting, and


Introduction to Multi-Dimensional Arrays.
1. Introduction

In this lab, you will learn about arrays, arranging data in an array (sorting), and multi-dimensional
arrays.

The section 2 presents a table that outlines some major activities and tasks you will do as the part of this
lab. Table 1 also provides the estimated-time for each activity, which will help you to organize your tasks
well. Section 3 presents some of the learning objectives for this lab. Section 4 (“Concept Map”) discusses
and provides a comprehensive introduction of the topic. Section 5 lists the set of home-tasks you are
required to complete before this lab. Section 6 presents a “walkthrough task” that you will do as the first
practical activity during your lab. The walkthrough task has many small steps which you should follow as
directed in-order to complete the task and to get the desired output. After that, you will be ready to
work on some tasks on your own. The section 7 lists practice tasks for this purpose. As the part of
section 8, your lab instructor will give you some tasks at runtime and will evaluate those according to
the criteria mentioned in section 9. Section 10 lists some further reading links.

Note: Before coming to the lab, you are required to read Lab contents until section 5. You will
start your practical work from section 6 onward in the lab.

Relevant Lecture Readings:

a) Lecture No. 15
b) Text Book: Computer Programming by D.S. Malik, second edition, pages: 475—495, and 504—
521

2. Activity Time-boxing
Table 1: Activity Time Boxing
Task No. Activity Name Activity time Total Time
5.1 Evaluation of Design 20 mins 20 mins
6.2 Setting-up Visual Studio 5 mins 5 mins
6.3 Specialized Tasks 30 mins 30 mins
7 Practice tasks 20 mins for each task 60 mins
8 Evaluation Task 55 min for all assigned task 55 mins
Total Time 170 mins

3. Objective of the Experiment


 To get familiarize with the concept of arrays.
 Problem solving using one dimensional array and sorting array values.
 Introducing multi-dimensional arrays in particular 2D arrays.

Department of computer Science, P a g e | 95


MAJU, 2013
Lab 11: Single Dimensional Arrays and Sorting

4. Concept Map

4.1 Single-Dimensional Arrays


An array is basically a collection of data-items or values all based on same data-type. For example: A
student’s marks in five courses in a semester: 78, 91, 83, 67, and 89; represent a collection of five int
type numbers where each value represents his/her marks in a certain course. An array is a structured
data-type (collection of values) of some fixed size (number of elements/values) where all values are of
same type. To define an array you have to mention its type, name and size (number of elements/values
the array will contain). For example:
int marks[5];

Above statements defines an int type array named marks, having size: 5 (capable of storing five values).
In the similar way, you can create array of any basic type, Examples:

float GPA[4]; //To store GPA of last 4 semesters


double area_of_circles[10]; //To store area of 10 circles
char student_name[30]; //To store name of size 30 (maximum characters)

Individual array elements can be accessed using same name (array name) along its index. An index
(which must be an integer value) indicates relative position of an element/value within an array. In
C++, array are 0-index based, it means that index of the starting/first element of an array is 0. For
example: int marks[5]; defines an int-type array having five elements, where first value or element will
be at index 0, second on index 1, thirds element at index 2, fourth at index 3, and the fifth element at
index 4. Examples:

cin>>marks[2]; //Gets marks from user and stores at 3rd position

As you have learned that array elements can be accessed using index values. Therefore, it is common
and a very convenient way to access array elements using loops (mainly for-loop). For example, below
shown code gets input from the user for all five elements of array marks.

for(int i=0;i<5; i++)


cin>>marks[i];

C++ language does not provide any boundary-check; it means C++ does not prohibit access of array
elements beyond the array size. Therefore, it is the responsibility of the programmer to access only valid
array elements (within the limit of array size) otherwise runtime errors or system crash may occur.

Similar to simple variables, arrays can also be initialized. To initialize an array, you have to provide a
comma ( , ) separated list of values (enclosed in braces “{“ and “}” ). All values to be used in initialization
should belong to the same data-type the array processes. In the following example, an array named
marks (int type) is being initialized:
int marks[4] = {87, 90, 73, 91};
char country[9] = {‘P’,’A’,’K’,’I’,’S’,’T’,’A’,’N’,’\0’};

Department of computer Science, P a g e | 96


MAJU, 2013
Lab 11: Single Dimensional Arrays and Sorting

4.2 Introduction to Multi-Dimensional Arrays


Until now, you have learnt that an array is a linear list (one dimensional list) of values of same data-type.
In C++, arrays can also have more than one dimension. For example an array can be declared having two
dimensions which represents set of values arranged in tabular form. For example, following code
declares a 2 Dimensional (2D) array or a table having 3 rows and 5 columns:
Column index

0 1 2 3 4

int marks_of_three_students[3][5]; 0
1
Row index
2

Above C++ code creates a two dimensional array having 3 rows (indexed from 0—2) and 5 columns
(indexed from 0—4). In a 2D array, accessing an array element requires two index values (one for row
and other for column). For examples:

//Prints value of the element at 2nd row and 4th column


cout<<marks_of_three_students[1][3];

4.3 Sorting
Sorting data means arranging values in some pre-defined order based on their numerical value etc.
Sorting an array means to arrange array elements in a certain order (ascending or descending). In
ascending-order, values are arranged in an array such that smaller values are followed by the larger
ones, e.g., 11, 36, 56, 98, 201. In descending-order, values of an array are arranged such that larger
values are placed first then the smaller values e.g., 201, 98, 56, 36, 11. There are many ways (algorithms)
to sort an array, in this course you will learn to sort an array using bubble-sort algorithm.

Bubble-sort algorithm performs N-1 steps, to sort an array. Here N refers to the size of an array (number
of elements in the array). In each step, the complete array is traversed and comparisons are made for
pair of adjacent values. Based on the element’s value and the order to sort (either ascending or
descending) those values may be swapped. For more details, kindly review lecture 15.

4.4 Summary
• Arrays are very useful data-structure that helps to process large amount of data of same-type.
Arrays simplify coding of the programs (processing large amount of similar data) using fewer
code lines as compared to the non-array based C++ program having same functionality.

• All array elements are accessed using same name, which helps to group related set of values
using same variable-name and results in improved code readability and maintainability.

• Sorting is the process of arranging data-items or values in a certain order. One of the main
advantages of using sorting is that it helps arranging data in a more meaningful way or desired
order (based on programmers need).

Department of computer Science, P a g e | 97


MAJU, 2013
Lab 11: Single Dimensional Arrays and Sorting

5. Home work before Lab

5.1 Problem Solution Modeling


Write the pseudo-code of the following task. You are required to bring this code with you and submit
to your lab instructor.

5.1.1 Problem description


Write pseudo-code of program that creates char array of 5 elements of type char. The array contains 5
vowels a, e, i, o, u. Ask the user to enter characters of their choice until the user enters a vowel. After
user enters a vowel the program should stop taking inputs. And display all the entered characters by
user. To check either a character entered is vowel or not your program should check the entered
characters from the array on each input. For example user enter q, d, f, a. The output should be q, d, f.

5.2 Practices from home


5.3.1 Task-1
Write a C++ program that asks the user to enter 10 integer values in an array. After entering 10
elements arrange the values of array in descending order and display it on output screen.

Sample Input:
Please enter elements for array:
10 8 5 9 14 7 12 10 1 2

Sample Output:
Sorted Array: 14 12 10 10 9 8 7 5 2 1

5.3.2 Task-2
Write a C++ program that takes five alphabets from the user and sort them in alphabetical order. You
should store the alphabets in a char array.

Sample Input:
Please enter 5 alphabets:
PIUDA

Sample Output:
After Sorting Array in Alphabetical order:
ADIPU

6. Procedure & Tools

6.1 Tools
Visual Studio 2008.

6.2 Setting-up Visual Studio 2008 [Expected time = 5 mins]


Setup Visual Studio and make a project named “StudentAvg”. For setup details please refer to the Lab-5
section 6.2.

Department of computer Science, P a g e | 98


MAJU, 2013
Lab 11: Single Dimensional Arrays and Sorting

6.3 Walkthrough Task [Expected time = 30 mins]


Write a C++ program that reads marks of a class (having 5 students) from the user and places them in an
array of type float. After that, it should compute average marks obtained by the class. In the end, display
student number and their marks for all those students who got marks above average.

To achieve such output, you need to follow the following instructions.

6.3.1 Writing Code


In the source file created in the project “StudentAvg” write following C++ code:

Figure 1: C++ code for student average program.

6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned in Lab-5 (section 6.3.2)

6.3.3 Executing the Program


A sample output after running the program is shown below:

Department of computer Science, P a g e | 99


MAJU, 2013
Lab 11: Single Dimensional Arrays and Sorting

Figure 2: Final output student average program.

7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to
finish the tasks in the required time. When you finish them, put these tasks in the folder told by your lab
instructor.

7.1 Practice Task 1 [Expected time = 20 mins]


Write a C++ program that creates two one dimensional arrays “A” and “B” of size 10 elements each. The
main program should get the input in the array “A” from the user and initialize array “B” with values “0”.
Your program should calculates the square (number * number) of each element of array “A” and should
store in the square value in corresponding positions (index) in the array “B”. In the end, the main
program (main function) prints the calculated values of array “B”. Also display the values in array B
which are divisible by 3 (if any).

Sample Inputs:
Please enter values in array A:
2 8 3 4 10 5 9 3 2 1

Sample Outputs:
The values in Array B are:
4 64 9 16 100 25 81 9 4 1

Values in Array B which are divisible by 3:


9 81 9

7.2 Practice Task 2 [Expected time = 20 mins]


Write a C++ program that creates an array “A” having 5 elements (in the main function). Then, the
program asks the user to enter values in this array. After that, the program should replace each array
element which is odd with an even value (by adding 1 to it). In the end, the main program (“main”)
shows the updated array elements in ascending order.
Department of computer Science, P a g e | 100
MAJU, 2013
Lab 11: Single Dimensional Arrays and Sorting

Sample Inputs:
Enter 5 elements in array A:
4 5 9 8 2

Sample Outputs:
Sorted Array
2 4 6 8 10

7.3 Practice Task 3 [Expected time = 20 mins]


Write a C++ program that creates a two-dimensional array “mat” of type int, having 4 rows and 5
columns. After that get input in this 2D array from the user. Then, sort row-0 and row-2 of the 2D array
in ascending order. Sort row-1 and row-3 in descending order. In the end, display the sorted 2D array on
the screen in tabular form.

Sample Inputs:
Enter Values in the table:
21, 12, 9, 1, 7
5, 67, 87, 3, 28
15, 81, 9, 23, 6
71, 11, 4, 2, 11

Sample Outputs:
Sorted table is: 1, 7, 9, 12, 21
87, 67, 28, 5, 3
6, 9, 15, 23, 81
71, 11, 11, 4, 2
7.4 Out comes
After completing this lab, student will be use arrays in the program to store and manipulate large
amount of data.

7.5 Testing
Test Cases for Practice Task-1:
Sample Input-1 Output 1(a) Output 1(b)
12 8 2 4 2 5 9 3 2 7 144 64 4 16 4 25 81 4 49 144 81
Sample Input-2 Output 2(a) Output 2(b)
2 8 3 4 10 5 9 3 2 1 4 64 9 16 100 25 81 9 4 1 9 81 9

Test Cases for Practice Task-2:


Sample Input Sample Output (a) Sample Output (b)
3 15 19 18 12 4 16 20 18 12 4 12 16 18 20
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
5 8 13 6 1 6 8 14 6 2 2 6 6 8 14

Department of computer Science, P a g e | 101


MAJU, 2013
Lab 11: Single Dimensional Arrays and Sorting

Test Cases for Practice Task-3:


Sample Input Sample Output
65, 32, 11, 9, 87 9, 11, 32, 65, 87
15, 4, 5, 2, 47 47, 15, 5, 4, 2
7, 31, 5, 81, 54 5, 7, 31, 54, 81
2, 54, 89, 1, 14 89, 54, 14, 2, 1

Practice Tasks Confirmation


T1
T2
T3

8. Evaluation Task (Unseen) [Expected time = 55 mins]


The lab instructor will give you unseen task depending upon the progress of the class.

9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student
has finished the complete/partial task(s).

Table 2: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4.1 Problem Modeling 20
2 6 Procedures and Tools 10
3 7.1, 7.2, and 7.3 Practice tasks and Testing 35
4 8 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

10. Further Readings

10.1 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\

Department of computer Science, P a g e | 102


MAJU, 2013

You might also like