[go: up one dir, main page]

0% found this document useful (0 votes)
15 views18 pages

Data Structure Lab Book Answer

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)
15 views18 pages

Data Structure Lab Book Answer

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/ 18

Data Structure

Assignment No 1: Array (One Dimensional Array)

Practice Program:

1) Write a menu driven C program to perform the following


operation on an integer array:

a) Display the sum of elements at even subscript position of array

b) Display the sum of elements at odd subscript position of array

#include <stdio.h>

int main() {

int arr[100], n, choice;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

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

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

}
do {

printf("\nMenu:\n");

printf("1. Sum of elements at even subscript positions\n");

printf("2. Sum of elements at odd subscript positions\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

if(choice == 1) {

int sum_even = 0;

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

sum_even += arr[i];

printf("Sum of elements at even subscript positions: %d\n",


sum_even);

} else if(choice == 2) {

int sum_odd = 0;

for(int i = 1; i < n; i += 2) {

sum_odd += arr[i];
}

printf("Sum of elements at odd subscript positions: %d\n",


sum_odd);

} else if(choice == 3) {

printf("Exiting program.\n");

} else {

printf("Invalid choice!\n");

} while(choice != 3);

return 0;

2) Write a C Program to find the largest pair sum in an unsorted


array.(hint: find 2 maximum elements from array and then find the
sum of both numbers.)
#include <stdio.h>

int main() {

int arr[100], n;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

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

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

int max1 = arr[0];

int max2 = arr[1];

if(max1 < max2) {

int temp = max1;

max1 = max2;

max2 = temp;

}
for(int i = 2; i < n; i++) {

if(arr[i] > max1) {

max2 = max1;

max1 = arr[i];

} else if(arr[i] > max2) {

max2 = arr[i];

printf("Largest pair sum is: %d\n", max1 + max2);

return 0;

3) Write a C Program to calculate Median of two sorted arrays of


different sizes

#include <stdio.h>
int main() {

int arr1[50], arr2[50], merged[100];

int n1, n2, n, i = 0, j = 0, k = 0;

printf("Enter size of first sorted array: ");

scanf("%d", &n1);

printf("Enter elements of first array:\n");

for(i = 0; i < n1; i++) {

scanf("%d", &arr1[i]);

printf("Enter size of second sorted array: ");

scanf("%d", &n2);

printf("Enter elements of second array:\n");

for(i = 0; i < n2; i++) {

scanf("%d", &arr2[i]);

i = 0; j = 0; k = 0;
while(i < n1 && j < n2) {

if(arr1[i] < arr2[j]) {

merged[k++] = arr1[i++];

} else {

merged[k++] = arr2[j++];

while(i < n1) {

merged[k++] = arr1[i++];

while(j < n2) {

merged[k++] = arr2[j++];

n = n1 + n2;

float median;

if(n % 2 == 0) {

median = (merged[n/2 - 1] + merged[n/2]) / 2.0;

} else {

median = merged[n/2];
}

printf("Median is: %.2f\n", median);

return 0;

SET A:

1) Write a C Program to Count number of occurrences (or


frequency) in a given sorted array
Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2

Output: 4 // x (or 2) occurs 4 times in arr[]

#include <stdio.h>

int main() {

int arr[] = {1, 1, 2, 2, 2, 2, 3};

int n = sizeof(arr) / sizeof(arr[0]);

int x = 2;

int count = 0;

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

if(arr[i] == x) {

count++;

printf("%d // x (or %d) occurs %d times in arr[]\n", count, x, count);

return 0;

}
2) Write a C program to accept n elements, store those elements in
array and store the square of these numbers in another array and
display both the array.

#include <stdio.h>

int main() {

int n;

printf("Enter number of elements: ");

scanf("%d", &n);

int arr[n], square[n];

printf("Enter elements:\n");

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

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

square[i] = arr[i] * arr[i];

printf("Original array:\n");

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


printf("%d ", arr[i]);

printf("\nSquare array:\n");

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

printf("%d ", square[i]);

printf("\n");

return 0;

3) Write a C program to Copy one array into another array.

#include <stdio.h>
int main() {

int n;

printf("Enter number of elements: ");

scanf("%d", &n);

int arr[n], copy[n];

printf("Enter elements:\n");

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

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

copy[i] = arr[i];

printf("Copied array:\n");

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

printf("%d ", copy[i]);

printf("\n");

return 0;
}

SET B:

1) Write a C program accept the polynomial and display it in


format e.g. 6x^4+2x^2+5x^1+3
#include <stdio.h>
int main() {
int n;
printf("Enter degree of polynomial: ");
scanf("%d", &n);
int coeff[n+1];
printf("Enter coefficients from highest degree to constant
term:\n");
for(int i = n; i >= 0; i--) {
scanf("%d", &coeff[i]);
}
printf("Polynomial: ");
for(int i = n; i >= 0; i--) {
if(coeff[i] != 0) {
if(i != n && coeff[i] > 0)
printf("+");
printf("%dx^%d", coeff[i], i);
}
}
printf("\n");

return 0;
}

2) Write a ‘C’ program to accept n elements store those


elements in an array and find and replace a given number.

#include <stdio.h>
int main() {

int n, old_num, new_num;

printf("Enter number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter elements:\n");

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

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

printf("Enter number to replace: ");

scanf("%d", &old_num);

printf("Enter new number: ");

scanf("%d", &new_num);

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


if(arr[i] == old_num) {

arr[i] = new_num;

printf("Updated array:\n");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

3) Write a ‘C’ program to accept two polynomials and find the


addition of accepted polynomials.
#include <stdio.h>
int main() {
int n1, n2;
printf("Enter degree of first polynomial: ");
scanf("%d", &n1);
printf("Enter degree of second polynomial: ");
scanf("%d", &n2);

int max_deg = (n1 > n2) ? n1 : n2;


int poly1[max_deg+1], poly2[max_deg+1],
sum[max_deg+1];

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


poly1[i] = 0;
poly2[i] = 0;
sum[i] = 0;
}

printf("Enter coefficients of first polynomial (degree %d to


0):\n", n1);
for(int i = n1; i >= 0; i--) {
scanf("%d", &poly1[i]);
}

printf("Enter coefficients of second polynomial (degree %d


to 0):\n", n2);
for(int i = n2; i >= 0; i--) {
scanf("%d", &poly2[i]);
}
for(int i = 0; i <= max_deg; i++) {
sum[i] = poly1[i] + poly2[i];
}

printf("Sum of polynomials: ");


for(int i = max_deg; i >= 0; i--) {
if(sum[i] != 0) {
if(i != max_deg && sum[i] > 0)
printf("+");
printf("%dx^%d", sum[i], i);
}
}
printf("\n");

return 0;
}

You might also like