Starting from:

$25

CS2092D - Programming Laboratory  Assignment - 1 - Solved

Programs should be written in C language and compiled using C compiler in Linux platform. Invalid input should be detected and suitable error messages should be generated. Sample inputs are just indicative. You are instructed to submit the questions 1 and 2 only, on Eduserver. The remaining questions are given for practice.

Questions

1.       Write a C program that reads an array of n integers and check whether a given integer is present in the array. If it is present in the array, print the position of the element in the array( array indexing starts from 0). If the element presents more than once in the array then print all the positions where the element is present in the array. Otherwise, print -1. (Hint:Use linear search) Input : Number of elements in the array : 6

Elements: 4, 3, 12 , 5 , 4, 3

Enter the element to be searched : 4

Output:Position of element is : 0, 4

Input : Number of elements in the array : 6

Elements: 7, 8, 12 , 5 , 4, 3

Enter the element to be searched : 6

Output:Position of element is : -1

2.       Write a program that reads an array of n integers and sorts the array elements in ascending order using bubble sort algorithm.

Input : Number of elements in the array : 6 
Elements: 3, 6, 12 , 5 , 4, 9

Output: Sorted array is : 3, 4, 5, 6, 9, 12

3.       Write a C program to convert a given binary number (base 2) to its equivalent decimal number (base 10). For example, the decimal number corresponding to the binary number (1011) 2 is 1*2° + 1*21 + 0*22 + 1*23 = 11

Input: Enter a binary number: 110110111 Output: The equivalent decimal number: 439

4.       Write a C program to circular shift an array of integers of size n from left to right by k elements. If k>n, then take k=k%n, where n is the size of the array and k is the number of times the array has to be shifted.

Input: Number of elements, n= 5, k=2 
Elements are : 1,2,3,4,5

Output: 4,5,1,2,3

5.       Write a program that reads two integer arrays A and B of size 'm' and 'n' respectively. Merge arrays A and B into a single array by avoiding repeated elements. Print the resultant array.

Input: Size of the first array 5

Array Elements 5, 15, 8, 50, 40

Size of the second array 7

Array Elements 8, 50, 10, 2, 15, 5, 7 Output: Merged array 5, 15, 8, 50, 40, 10, 2, 7

6.       Write a C program to find whether a majority element exists in an integer array. If it exists in an array, print it. Otherwise, print -1. A majority element in an array arr[] of size n is an element that appears more than n/2 times (and hence there is at most one such element).

Input: 1,2,2,2,5 Output: 2

Input: 1,2,2,4,5 Output: -1

****************************************

More products