0% found this document useful (0 votes)
16 views

Lab Session 4

The document outlines a lab session for a C programming course, detailing various programming tasks including finding the second largest element in an array, summing array elements, reversing an array, and counting element frequencies. It also includes in-lab exercises such as deleting duplicates, inserting elements, calculating 2's complement, and sorting arrays. Additionally, post-lab tasks involve finding indices of two numbers that sum to a target and arranging array elements in a specific order based on their positions.

Uploaded by

sugunasri2736
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab Session 4

The document outlines a lab session for a C programming course, detailing various programming tasks including finding the second largest element in an array, summing array elements, reversing an array, and counting element frequencies. It also includes in-lab exercises such as deleting duplicates, inserting elements, calculating 2's complement, and sorting arrays. Additionally, post-lab tasks involve finding indices of two numbers that sum to a target and arranging array elements in a specific order based on their positions.

Uploaded by

sugunasri2736
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming Using C Lab (24IT101) Register Number:

Lab Session – 4

Date:

Pre-Lab|4:
1. Write a C program to find second largest element in an array.

Program code:
Programming Using C Lab (24IT101) Register Number:

2. Write a C program to find sum of all array elements

Program code:
Programming Using C Lab (24IT101) Register Number:

3. Write a program to reverse an array

Program Code:
Programming Using C Lab (24IT101) Register Number:

4. Write a C program to count frequency of each element in an array.

Program Code:
Programming Using C Lab (24IT101) Register Number:

In–Lab|4:
1. Write a C program to delete all duplicate elements from an array.

Program code:
Computer Programming Lab Register Number:

2. Write a C program to insert an element in an array.

Program code:
Programming Using C Lab (24IT101) Register Number:

3. Find 2’s complement of the given binary number.


To get 2’s complement of a binary number, simply invert the given number and add 1 to the
least significant bit (LSB) of given result.
Test Data and Output:
Find 2’s complement of binary number 10101110.
Simply invert each bit of given binary number, which will be 01010001. Then add 1 to the LSB
of this result, i.e., 01010001+1=01010010 which is answer.
Find 2’s complement of binary number 10001.001.
Simply invert each bit of given binary number, which will be 01110.110 Then add 1 to the
LSB of this result, i.e., 01110.110+1=01110.111 which is answer.

Program code:
Computer Programming Lab Register Number:

4. Write a C program to sort array elements in ascending or descending order.

Program code:
Programming Using C Lab (24IT101) Register Number:

Post–Lab|4:

1. Given an array of integers nums and an integer target, return indices of the two
numbers such that they add up to target. You may assume that each input would have exactly
one solution and you may not use the same element twice. You can return the answer in any
order.
Sample Input:
Enter the size of an array: 4
Enter array elements: 2 7 11 15
Enter target: 9
Output:
[0,1]
Program code:
Programming Using C Lab (24IT101) Register Number:

2. Given a number N denoted the elements in an array. He wants to arrange the elements of an
array such that the odd positions have sorted elements in the ascending order and even
positions have sorted elements in descending order. For example, if we have 1 2 3 4 5 then the
result will be 1 5 2 4 3.
Input Format: The first line of the input contains an integer T denoting the numberof test
cases. The description of each test case is given below.
First line of each test case contains N number of elements in an array.Second line contains N
space separated integers A1, A2, ….., AN.
Output Format : Print array in desired order.
Constraints : 1 <= T <= 10; 1 <= N <= 1000000; 1 <= Ai <= 100000000

Sample Input
:2
8

3 6 9 10 12 5 15 18

1 6 8 9 11 12

Sample Output :

3 18 5 15 6 12 9 10

1 12 6 11 8 9

Program code:
Programming Using C Lab (24IT101) Register Number:

Evaluator Signature______________ Marks Secured:_______Out of _______

You might also like