Lab 06: Arrays (One Dimensional) : Semester BS CS - 01 & BS IT - 02
Lab 06: Arrays (One Dimensional) : Semester BS CS - 01 & BS IT - 02
Semester BS CS – 01 & BS IT - 02
Objective(s) : Upon completion of this lab session, learners will be able to:
Arrays
So far we have been using only the standar data types: interger, character, floating point number, and
Boolean. These types are useful but they only hande limited amout of data. To handle more data in
programming language there are derived datatypes. Such datatype is Array structure.
Lets consider that we have a problem that requires us to read, process and print 10 interger values.
To solve this problem we need to create 10 variables, each with different name as shown in below
figure.
Having ten variables with different name is a problematic. How we can read 10 intergers from
keyboard and store them? To read 10 variables from keyboard we need to write 10 read statements.
Furthermore, once we have stored them in memory, we need to print them on screen, for that we
need to write 10 print statement as well.
Althrough this approach is adaptable for 10 variables, what if we need to store, process and print
1000 interger variables? To process large amout of data, we need a powerfull structure that is Array.
To store, process and print the values, we can use loops. Below
figure demonstrates the flow of used to process 10 elements.
// Output
Elements Square
======== =======
0 0
1 1
2 4
3 9
4 16
Exercise 1
Write a C++ program, that ask user to enter 10 integer values. Store those values in one dimension
array. Create an other one dimension array of same size, and store the values of first array in
reverse order. Print the results on screen.
Your Program should display output as follows:
Exercise 2
Write a C++ Program that checks whether the two arrays are equal or not.
Declare two Interger Arrays with 7 elements, and fill up the array with keyboard input.
Test if every element in Array 1 is equal to corresponding element in Array 2. For example,
the program should check A[0] = B[0], A[1] = B[1], and so for.
Your Program should display output as follows:
Write a C++ Program that will take an array of size 10 input from user and then sort that array in
ascending order.