0% found this document useful (0 votes)
23 views12 pages

DSA LAB ASSIGNMENT 02 - Removed

The document contains a series of programming assignments in C, each focusing on different array operations such as reversing an array, searching for an element, sorting in descending order, counting elements between two values, finding the next greater element, calculating minimum distance between two numbers, segregating even and odd numbers, and performing various operations on a square matrix. Each assignment includes the code implementation and expected output. The assignments aim to enhance understanding of array manipulation and matrix operations in C programming.

Uploaded by

Rohan Nag
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)
23 views12 pages

DSA LAB ASSIGNMENT 02 - Removed

The document contains a series of programming assignments in C, each focusing on different array operations such as reversing an array, searching for an element, sorting in descending order, counting elements between two values, finding the next greater element, calculating minimum distance between two numbers, segregating even and odd numbers, and performing various operations on a square matrix. Each assignment includes the code implementation and expected output. The assignments aim to enhance understanding of array manipulation and matrix operations in C programming.

Uploaded by

Rohan Nag
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/ 12

ASSIGNMENT-2

Q1. WAP to reverse the contents of a array of n elements.


Code:
#include <stdio.h>
void main (){

int i, j, a, n;
printf("enter number of elements in an array:");
scanf("%d", &n);
int num[n];
printf("Enter the elements:");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
int temp;
for(int i = 0; i<n/2; i++){
temp = num[i];
num[i] = num[n-i-1];
num[n-i-1] = temp;
}
printf("\nReversed array: ");
for(int i = 0; i < n; i++){
printf("%d ", num[i]);
}
}

Output:
Q2. WAP to search an element in array of n numbers.

Code:
#include <stdio.h>
int main() {
int a, i, r;
printf("Enter the length of the array: ");
scanf("%d", &a);
int arr[a];
printf("Enter the array elements: ");
for (i = 0; i < a; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the item to be searched: ");


scanf("%d", &r);

i = 0;
while (i < a && r != arr[i]) {
i++;
}
if (i < a) {
printf("The element is found in the index position = %d", i);
} else {
printf("Element not found!");
}
return 0;
}

Output:
Q3. WAP to sort a array of n numbers in Descending Order.

Code:

#include <stdio.h>
void main (){
int num[20];
int i, j, a, n;
printf("enter number of elements in an array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] < num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in descending order is: ");
for (i = 0; i < n; ++i){
printf("%d ", num[i]);
}
}

Output:
Q4. Given an unsorted array of size n, WAP to find and display the number of elements
between two elements a and b (both inclusive). E.g. Input : arr = [1, 2, 2, 7, 5, 4], a=2 and
b=5, Output : 4 and the numbers are: 2, 2, 7, 5.

Code:
#include <stdio.h>

int main()
{
int n, i, a, b, c = 0;

printf("Enter size of array: ");


scanf("%d", &n);

printf("Enter elements of array: ");

int arr[n];

for (i = 0; i < n; i++)


{
scanf("%d", &arr[i]);
}

printf("\nEnter lower limit element & upper limit element respectively: ");
scanf("%d %d", &a, &b);
int arj[10];
for (i = 0; i < n; i++)
{
if (arr[i] == a || arr[i] == b)
{
c++;
}

if (arr[i] > a && arr[i] < b)


{
c++;
}
}
printf("Number of elements in between two elements (Both Inclusive) = %d", c);
printf("");

return 0;
}
Output:
Q5. Given a array, WAP to print the next greater element (NGE) for every element. The next
greater element for an element x is the first greater element on the right side of x in
array. Elements for which no greater element exist, consider next greater element as -1.
E.g. For the input array [2, 5, 3, 9, 7], the next greater elements for each elements are as
follows.

Code:
#include<stdio.h>
void printNGE(int arr[], int n)
{
int next, i, j;
for (i=0; i<n; i++)
{
next = -1;
for (j = i+1; j<n; j++)
{
if (arr[i] < arr[j])
{
next = arr[j];
break;
}
}
printf("%d\n", next);
}
}

int main()
{
int arr[]= {2, 5, 3, 9, 7};
int n = sizeof(arr)/sizeof(arr[0]);
printNGE(arr, n);
return 0;
}
Output:
Q6. Given an unsorted array arr and two numbers x and y, find the minimum distance
between x and y in arr. The array might also contain duplicates. You may assume that
both x and y are different and present in arr.
Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6

Code:
#include <stdio.h>
void minDistance(int *Arr, int n, int x, int y)
{
int distance[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, a = 0, s = 0;
;
for (int i = 0; i <= n; i++)
{
if ((Arr[i] == x || Arr[i] == y) && s == 0)
{
if (Arr[i] == x)
{
s = 1;
// printf("Start by %d",Arr[i]);
}
else if (Arr[i] == y)
{
s = 2;
// printf("Start by %d\n",Arr[i]);
}
}
else if (s == 1 && Arr[i] == y)
{
s = 0;
a++;
// printf("end by %d\n",Arr[i]);
}
else if (s == 2 && Arr[i] == x)
{
s = 0;
a++;
// printf("end by %d\n",Arr[i]);
}
if (s != 0)
distance[a]++;
}
int min = distance[0];
for (int i = 0; i < n; i++)
{
if (distance[i] <= min && distance[i] != 0)
min = distance[i];
}
printf("Min Distance between %d and %d is %d\n", x, y, min);
}
int main()
{
int Arr[50] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, n = 12, a, b;
// limit from a to b.
a = 3;
b = 6;
minDistance(Arr, n, a, b);
return 0;
}

Output:
Q7. WAP to arrange the elements of a array such that all even numbers are followed by all
odd numbers.

Code:
#include <stdio.h>
void swap(int *a, int *b);
void segregateEvenOdd(int arr[], int size)
{
int left = 0, right = size - 1;
while (left < right)
{
while (arr[left] % 2 == 0 && left < right)
left++;
while (arr[right] % 2 == 1 && left < right)
right--;
if (left < right)
{
swap(&arr[left], &arr[right]);
left++;
right--;
}
}
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int arr[] = {27,44,67,82,44,51,79};
int arr_size = sizeof(arr) / sizeof(arr[0]);
int i = 0;
segregateEvenOdd(arr, arr_size);
printf("Array after segregation ");
for (i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
Q8. Let A be nXn square matrix. WAP by using appropriate user defined functions for the
following:
a. Find the number of nonzero elements in A
b. Find the sum of the elements above the leading diagonal.
c. Display the elements below the minor diagonal.
d. Find the product of the diagonal elements.

Code:
#include <stdio.h>
void NonZeroElements(int (*Arr)[5], int n)
{
int a = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (*(*(Arr + i) + j) != 0)
a++;
}
}
printf("Number of non-zero elements in array: %d\n", a);
}
void sumOfElementsAboveLeadingDiagonal(int (*Arr)[5], int n)
{
int a;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j > i)
a += *(*(Arr + i) + j);
}
}
printf("Sum Of Elements Above Leading Diagonal: %d\n", a);
}
void ElementsBelowMinorDiagonal(int (*Arr)[5], int n)
{
printf("Elements below the minor diagonal: \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j >= n - i)
printf(" %d", *(*(Arr + i) + j));
else
printf(" ");
}
printf("\n");
}
}
void ProductOfdiagonalElements(int (*Arr)[5], int n)
{
int a = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j)
a *= *(*(Arr + i) + j);
}
}
printf("Product of leading diagnal Elements: %d\n", a);
}
int main()
{
int mat[5][5] = {
{10, 11, 12, 13, 14},
{20, 21, 22, 23, 24},
{30, 31, 32, 33, 34},
{40, 41, 42, 43, 44},
{50, 51, 52, 53, 54},
};
int n = 5;
NonZeroElements(mat, n);
sumOfElementsAboveLeadingDiagonal(mat, n);
ElementsBelowMinorDiagonal(mat, n);
ProductOfdiagonalElements(mat, n);
}

You might also like