Professional Documents
Culture Documents
Lab 11
Lab 11
Table of Contents
1. Introduction 95
2. Activity Time-boxing 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
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.
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
4. Concept Map
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:
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:
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.
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’};
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:
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).
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.1 Tools
Visual Studio 2008.
6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned in Lab-5 (section 6.3.2)
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.
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
Sample Inputs:
Enter 5 elements in array A:
4 5 9 8 2
Sample Outputs:
Sorted Array
2 4 6 8 10
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
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).
10.1 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\