0% found this document useful (0 votes)
4 views5 pages

C Programming Lab: Arrays and Operators

This document outlines a laboratory exercise for a Computer Fundamentals and Programming course, focusing on the use of unary and conditional operators, as well as if statements. It includes objectives for learning about arrays, their declaration, initialization, and various programming tasks related to arrays, such as computing sums, printing elements, and comparing arrays. Additionally, it provides practice activities and exercises aimed at reinforcing the concepts taught in the lab session.

Uploaded by

Subhan Amjad
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)
4 views5 pages

C Programming Lab: Arrays and Operators

This document outlines a laboratory exercise for a Computer Fundamentals and Programming course, focusing on the use of unary and conditional operators, as well as if statements. It includes objectives for learning about arrays, their declaration, initialization, and various programming tasks related to arrays, such as computing sums, printing elements, and comparing arrays. Additionally, it provides practice activities and exercises aimed at reinforcing the concepts taught in the lab session.

Uploaded by

Subhan Amjad
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

CIS-101 COMPUTER

FUNDAMENTALS AND
PROGRAMMING

Instructor: Dr. Nouman Shamim

BS(CIS) Fall-2025
Semester-01

Laboratory Exericse-06
 Unary Operators
 Conditional Operators
 If Statement

DEPARTMENT OF COMPUTER AND


INFORMATION SCIENCES

PAKISTAN INSTITUTE OF ENGINEERING


AND APPLIED SCIENCES
LAB OBJECTIVE

The objective of this lab session is to

1. Learn the use for loop,


2. Learning to solve problems that involve code repetitions
3. Using if else , scanf printf inside the loop

SECTION-01 LEARNING

ARRAYS

An array is a sequence of objects all having same data types, stored at contiguous memory locations. The objects in an
array are called ‘elements’ of the array, array elements are accessed using an index starting from 0 (in C/C++)

Declaring and Initializing arrays

Declaring Array

type arrayName [size] ;

Example

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 characters

Exceptions

An array can also be declared using a variable (The variable should must be declared and initialized before using
with array)

Example

int size=10;

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

Initializing Array

Arrays can be initialized in many ways

Method-1 Just like variables

int A[5];

CIS-101 CFP, Document prepared by Dr. Nouman Shamim nauman@[Link]


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

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}; //elements 4 and 5 will be initialized to 0

Method-4 Without size

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

//Size of the array will be equal to the size of initializers list

SECTION-02 PRACTICE

Activity-01:

1. Create an array of 5 integers and initialize it using these values [78,39,13,26,1}, using a list of initializers, display
the entire array using a for loop.
2. Modify 1, remove the list of initializers, using a for loop and scanf get 5 integers from user and assign them to
array elements starting from first element
3. Write a program to compute the sum of all elements in the array , you can use array of any size of your choice
and initialize the array using list of initializers or scanf whatever you like to use.
4. Write a program to print elements of an array in reverse order, that is start printing from last element of the
array.

SECTION-03 EXERCISE

TASK-01 Even / Odd printing of array elements ( Difficulty Low)

CIS-101 CFP, Document prepared by Dr. Nouman Shamim nauman@[Link]


Page 3 of 5
Write a program to print the even and odd elements of the array separately, that is the program should first print
only the even elements of the array and then it should print the odd elements of the array.

TASK-02: Even / Odd sum of array elements (Difficulty Low)

Write a program that initializes and array of 10 elements and computes the sum of even and odd elements of the
array separately. That is consider the following array and final answers

int A[]={1,2,3,4,5,6,7,8,9,10};
Sum of even elements of the array = 30
Sum of odd elements of the array = 25

TASK-03 : Copy an array (Difficulty Low)

Write a program to copy elements of one array into another array, that is let A is an array of 10 elements and is
completely initialized, while B is an array of 10 elements. Your program should copy A[0] to B[0], A[1] to B[1] etc., a
copy operation is simply an assignment statement for example to copy A[0] to B[0] you should write B[0] = A[0].

TASK-04 Compare two arrays (Difficulty Low)

Ahmed is conducting an experiment to compare item prices listed online with those available in the local market. To
do this, he has created two indexed lists:

 List A: Prices of items from the internet


 List B: Prices of the same items from the local market

Each item is assigned the same index in both lists, meaning the item at index i in List A corresponds to the same item
at index i in List B. Since arrays in C cannot store both item names and prices together, only the prices are stored,
and the index serves as the identifier for each item.

Your task is to write a C program that compares these two arrays and provides the following insights:

 Count of items with the same price in both lists


 Count of items with different prices
 Indices of items that are cheaper online (List A < List B)
 Indices of items that are cheaper in the local market (List B < List A)

TASK-05 Re-arrangement of an array (Difficulty Moderate)

Re-arrangement of an array is to change the position 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 4th element of the array should be placed at position 0, first element should be

CIS-101 CFP, Document prepared by Dr. Nouman Shamim nauman@[Link]


Page 4 of 5
placed at index 1 and the 2nd element of A should be placed at position 2. Write a program that creates an array of
5 elements name it as arr_original, use a list of initializer to initialize 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.

TASK-06 Find the tuple of maximum sum(Difficulty Moderate)


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 consisting 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

Enter value : 13

Enter value : 7

Enter value : 11

Answer : 20 and 13

CIS-101 CFP, Document prepared by Dr. Nouman Shamim nauman@[Link]


Page 5 of 5

You might also like