Lab 03
Lab 03
SCHEDULE
LAB MANUAL
CFP Laboratory sessions will be conducted in Computer Center on each Thursday
1. Introduc on from 8:25 to 11:30 a.m.
LAB INSTRUCTORS
Afaq Ahmed
NOTE:
Aimen Wadood
Nouman Shamim THIS IS A GRADED LAB - YOU
WILL NEED TO COMPLETE A
EMAIL
MINIMUM OF 04 ACTIVITIES
nauman@pieas.edu.pk
FROM SEC-03 (EACH ACTIVITY
HAS 25% WEIGHT)
Let’s Talk to Computer
Read this sec on if you need to revise your concepts related to Arrays, for while loop you may consult the
lecture slides.
ARRAYS
An array is a sequence of objects all having same data types, stored at con guous memory loca ons. The objects in an
array are called ‘elements’ of the array, array elements are accessed using an index star ng from 0 (in C/C++)
Examples
Declaring Array
Excep ons
An array can also be declared using a variable (The variable should must be declared and ini alized before using with
array)
int size=10;
int A[size];
int A[5];
int A[5],n=0;
while( n<5){
A[n]=n+1;
n=n+1;
int A[5]={1,2,3,4,5];
int A[]={1,2,3,4,5} //Size of the array will be equal to the size of ini alizers list
Example
int A[5]={2,4,6,8,10};
printf(“%d,%d,%d,%d,%d”,A[0],A[1],A[2], A[3],A[4]);
OR
int n=0;
while( n<5){
printf(“%d “,A[n]);
n=n+1;
}
Example Program-01
The following program declares an array of 5 integers, ini alizes the array using an ini alizer list (complete) and displays
the values stored in array elements
Examples
#include<stdio.h>
int main(){
int A[5]={1,2,3,4,5};
int index=0;
while(index<5){
printf(“%d \n”,A[index]);
index=index+1;
}
return 0;
}
The following program declares an array of 5 integers, ini alizes the array using an ini alizer list(in-complete) and
displays the values stored in array elements
#include<stdio.h>
int main(){
int A[5]={1,2,3};
int index=0;
while(index<5){
printf(“%d”,A[index]);
index=index+1;
}
return 0;
}
Example Program-03
The following program declares two arrays, ini alizes them using ini alizer lists, and displays the sum of their values
#include<stdio.h>
void main(){
int A[5]={1,2,3,4,5};
int B[5]={6,7,8,9,10};
int index=0;
while(index<5){ //displaying elements of Array A
printf(“A [%d] = %d \n“,index,A[index]);
index=index+1;
}
index=0;
while(index<5){ //displaying elements of Array B
printf(“B [%d] = %d \n“,index, B[index]);
index=index+1;
}
inex=0;
prin (“Displaying sum of elements of two arrays \n”);
while(index<5){
printf(“A[%d]+B[%d] = %d”,index,index,A[index]+B[index]);
index=index+1;
}
}
Sec on-03 Ac vi es
In each of the following ac vi es you are provided with a sample problems statement along with
programming solu ons to them, you have to complete the ac vi es given following the sample problem/
solu on. In most of the cases you have to extend the solu on of given programming solu on according to
the direc ons.
Sample Output
Array A: ={1,2,3,4,5}
Array B: ={1,4,9,16,25}
Example Program-04
The following program gets 3 numbers from user, stores numbers in an array and prints the, sum and average of these
numbers
#include<stdio.h>
int main(){
int A[3];
int B[3];
int index=0,sum;
float average;
for(index=0;index<3;index++){
scanf(“%d”,&A[index]);
average=sum/3.0;
Write a program that gets five (5) numbers from user (using console), in this program create two integer arrays each of
size 5, name these arrays as array_even, array_odd. As the user enters a number the program should check whether the
number is even or odd. If the number entered by the user is even the number should be stored in array_even, if the
number is odd it should be stored array_odd, the number should be ignored otherwise. When the user finish inpu ng
the number, the program should display even and odd numbers by displaying the contents of the array_even and
array_odd.
Re-arrangement of an array is to change the posi on of elements of the array. For example consider an array
A[5]={8,5,9,11,20} its present arrangement scheme is A[0]=8, A[1]=5 and A[4]=20 . . ., now consider a re-arrangement
scheme [4, 0 ,1,2,3] in which the 4 th element of the array should be placed at posi on 0, first element should be placed
at index 1 and the 2nd element of A should be placed at posi on 2.
Write a program that creates an array of 5 elements name it as arr_original, use a list of ini alizer to ini alize this array
( choose any values between 1 to 100 for simplicity), create another array of 5 elements and name it as arr_order,
ini alize this array with an arrangement scheme (you may use the one given in above example). Create a third array
arr_arranged, copy the elements of arr_original into arr_arranged according to the arrangement scheme store in
arr_order.
Advanced Version : Ini alize the arr_original and arr_order from user input and re-arrange the arr_original.
Enter value : 20
Enter value : 5
Enter value : 7
Enter value : 11
Answer : 20 and 13
Write a program that gets 6 numbers from user, store these numbers in two arrays A and B, each array should have 3
members only. The program should display the values of two arrays and later should exchange the values of A with B i.e.
values stored in array A should be replaced by values stored in array B and values stored in array B should be replaced by
values stored in array A. Finally the program should display the new values of A and B.
Write a program that merges two arrays of integers into a third array and then reverses the merged array. The program
should also include error checking to ensure the user inputs valid integer values for the arrays. Display the contents of all
three arrays (the two original arrays and the reversed merged array).