Thanks to visit codestin.com
Credit goes to www.scribd.com

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

DSA Assignment 1

The document contains three C programming assignments. The first assignment involves inserting an element into a non-empty array, the second assignment covers merging two arrays (both sorted and unsorted), and the third assignment focuses on printing even elements while reshuffling an array to place odd elements before even ones in sorted order. Each section includes code snippets demonstrating the required functionality.

Uploaded by

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

DSA Assignment 1

The document contains three C programming assignments. The first assignment involves inserting an element into a non-empty array, the second assignment covers merging two arrays (both sorted and unsorted), and the third assignment focuses on printing even elements while reshuffling an array to place odd elements before even ones in sorted order. Each section includes code snippets demonstrating the required functionality.

Uploaded by

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

Assignment-1

Roll No: 24H80059 Section : H-X

1. Write a program in C to insert an element at a given location in a non-empty array.

#include <stdio.h>
int main() {
int arr[100], n, pos, elem;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
scanf("%d %d", &pos, &elem);
for (int i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = elem;
for (int i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

2. Write a program in C to merge two arrays for the following two cases-

a) Arrays are sorted

#include <stdio.h>
int main() {
int a[50], b[50], c[100], n1, n2, i = 0, j = 0, k = 0;
printf("Enter the size of the first sorted array: ");
scanf("%d", &n1);
printf("Enter %d elements of the first sorted array: ", n1);
for (int x = 0; x < n1; x++) {
scanf("%d", &a[x]);
}
printf("Enter the size of the second sorted array: ");
scanf("%d", &n2);
printf("Enter %d elements of the second sorted array: ", n2);
for (int x = 0; x < n2; x++) {
scanf("%d", &b[x]);
}
while (i < n1 && j < n2) {
if (a[i] < b[j]) {
c[k++] = a[i++];
} else {
c[k++] = b[j++];
}
}
while (i < n1) {
c[k++] = a[i++];
}
while (j < n2) {
c[k++] = b[j++];
}
printf("Merged sorted array: ");
for (int x = 0; x < k; x++) {
printf("%d ", c[x]);
}
return 0;
}

b) Arrays are unsorted

#include <stdio.h>
int main() {
int a[50], b[50], c[100], n1, n2, k = 0, temp;
printf("Enter the size of the first unsorted array: ");
scanf("%d", &n1);
printf("Enter %d elements of the first unsorted array: ", n1);
for (int i = 0; i < n1; i++) {
scanf("%d", &a[i]);
}
printf("Enter the size of the second unsorted array: ");
scanf("%d", &n2);
printf("Enter %d elements of the second unsorted array: ", n2);
for (int i = 0; i < n2; i++) {
scanf("%d", &b[i]);
}
for (int i = 0; i < n1; i++) {
c[k++] = a[i];
}
for (int i = 0; i < n2; i++) {
c[k++] = b[i];
}
for (int i = 0; i < k - 1; i++) {
for (int j = 0; j < k - i - 1; j++) {
if (c[j] > c[j + 1]) {
temp = c[j];
c[j] = c[j + 1];
c[j + 1] = temp;
}
}
}
printf("Merged and sorted array: ");
for (int i = 0; i < k; i++) {
printf("%d ", c[i]);
}
return 0;
}

3. Given an integer array, print the even elements of the array and reshufle the array where

all odd elements will come before the even elements in the sorted order.

#include <stdio.h>
int main() {
int arr[100], odd[100], even[100], n, o = 0, e = 0, temp;

printf("Enter the size of the array: ");


scanf("%d", &n);

printf("Enter %d elements of the array: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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


if (arr[i] % 2 == 0) {
even[e++] = arr[i];
} else {
odd[o++] = arr[i];
}
}

for (int i = 0; i < o - 1; i++) {


for (int j = 0; j < o - i - 1; j++) {
if (odd[j] > odd[j + 1]) {
temp = odd[j];
odd[j] = odd[j + 1];
odd[j + 1] = temp;
}
}
}

for (int i = 0; i < e - 1; i++) {


for (int j = 0; j < e - i - 1; j++) {
if (even[j] > even[j + 1]) {
temp = even[j];
even[j] = even[j + 1];
even[j + 1] = temp;
}
}
}

printf("Even elements of the array: ");


for (int i = 0; i < e; i++) {
printf("%d ", even[i]);
}
printf("\n");

for (int i = 0; i < o; i++) {


arr[i] = odd[i];
}
for (int i = 0; i < e; i++) {
arr[o + i] = even[i];
}

printf("Array after reshuffling: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}

You might also like