[go: up one dir, main page]

0% found this document useful (0 votes)
21 views8 pages

Lab 03

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Lab 03

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

GRADED LAB

COMPUTER FUNDAMENTALS AND


PROGRAMMING
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.

2. Condi onal Statement Where can I get lab handouts?


A: All material related to laboratory sessions will be available on Zambeel and
3. Array and While Loop
Canvas under CFP-2024 course folder / page.

Is it mandatory to submit the work done in a laboratory session?


A: Not necessarily, submission of work related to laboratory session varies from
among laboratory sessions. Submission if required will be communicated via the
laboratory handout along with deadline and when and how to submit details.

Will there be a laboratory exam?


A: Laboratory performance is graded based (a) comple on and quality of assigned
work (b) number of laboratory sessions a ended (c) laboratory quizzes (d) course
project.

The lab work of a session should be completed with


all ques ons resolved before the next lab session.

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

CF-Lab-03 prepared by Dr. Nouman Shamim


GRADED LAB

Sec on -01 Reading and Syntax

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

int A[10]; //This will create an array of 10 integers

int A[10],B[10]; //This will create two arrays of 10 integers each

float A[10]; //This will create an array of 10 floats

char A[10]; //This will create an array of 10 floats

Declaring Array

type arrayName [size] ;

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];

Ini alizing Array

Arrays can be ini alized in many ways

Method-1 Just like variables

int A[5];

A[0]=1; A[1]=2; A[2]=3; A[3]=4; A[4]=5;

CF-Lab-03 prepared by Dr. Nouman Shamim


GRADED LAB
Method-2 Using loops

int A[5],n=0;

while( n<5){

A[n]=n+1;

n=n+1;

Method-3 Using Ini alizer list

int A[5]={1,2,3,4,5];

int A[5]={1,2,3}; //rest of the elements will be ini alized to 0

Method-4 Without size

int A[]={1,2,3,4,5} //Size of the array will be equal to the size of ini alizers list

CF-Lab-03 prepared by Dr. Nouman Shamim


GRADED LAB

Sec on-02 Working With Arrays


This sec on provides examples programs for handling arrays along with simple tasks to exercise the
concepts

Accessing and displaying the value of array elements

Array elements can be accessed and used by means of elements indexes

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;
}

CF-Lab-03 prepared by Dr. Nouman Shamim


GRADED LAB
Example Program-02

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;
}
}

CF-Lab-03 prepared by Dr. Nouman Shamim


GRADED LAB

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.

Ac vity-01 Find Square of array Elements


Use example program-03, update it into a new program that finds the square of each element of the array A and store
these squares in array B, finally display the contents of these two arrays. The output of the program should be as under

Sample Output

Array A: ={1,2,3,4,5}

Array B: ={1,4,9,16,25}

Example Program-04

Ini alizing array elements from user input.

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++){

printf(“ \n Enter value for element %d of array :”,index);

scanf(“%d”,&A[index]);

CF-Lab-03 prepared by Dr. Nouman Shamim


GRADED LAB
sum=A[0]+A[1]+A[2];

average=sum/3.0;

printf(“Sum of array members = %d“,sum);

printf(“Average of array members = %f”,average);

Ac vity-02 Store Even And Odd Numbers In Different Arrays

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.

Ac vity-03 Re-arrange an Array

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.

Ac vity-04 Find the tuple of maximum sum


Write a program that creates an array of 10 elements, let the user enter the values, the program should find the two
array elements whose sum is largest of sum of any two array elements. For example for an array A consis ng of
20,5,13,7,11 the sum of 20 and 13 is largest of sum of two elements in this array. Your program should print such two
elements of the array provided by the user. Following is a sample output of your target program.

Sample output (for 5 element array)

Enter value : 20

Enter value : 5

CF-Lab-03 prepared by Dr. Nouman Shamim


GRADED LAB
Enter value : 13

Enter value : 7

Enter value : 11

Answer : 20 and 13

Ac vity-05 Find Maximum of an Array


a) Recall that we developed a program that can find the largest number from three given numbers, extend the
above program and rewrite it to find the smallest or largest of 10 numbers, you should store the numbers in an
array and then process the array to get desired results.
b) Extend the program developed for above problem, find largest and smallest of N numbers where N <10 and
N>20. After the smallest and largest numbers are found, the program should store the largest number at the
beginning of the array and smallest number at the end of the array . For example if the name of your array is A,
A[0] should contain the largest number and A[N] should contain the smallest number.

Ac vity-06 Swapping Arrays

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.

(Hint , you might need a third array ).

Ac vity-07: Merge and Reverse Two Arrays

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).

CF-Lab-03 prepared by Dr. Nouman Shamim

You might also like