105360 10 19UCSES301 B 3 12Unit 1 Solved Application Qns
105360 10 19UCSES301 B 3 12Unit 1 Solved Application Qns
DEPARTMENT OF ECE
SUBJECT CODE: 19UCSES301 SEM: III
SUBJECT NAME: FUNDAMENTALS OF DATA STRUCTURES IN C YEAR: II
UNIT- I PROGRAMMING BASICS
14.Write a simple C program to Find Size of int, float, double and char of Your System.
#include<stdio.h>
int main()
{
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %ld bytes\n", sizeof(intType));
printf("Size of float: %ld bytes\n", sizeof(floatType));
printf("Size of double: %ld bytes\n", sizeof(doubleType));
printf("Size of char: %ld byte\n", sizeof(charType));
return 0;
}
Soln: 12 12
Prefix and Postfix operators have the same effect if they are used in an isolated C
statement. For example, the two statements
x++; and ++x; have the same effect
But prefix and postfix operators have different effects when they are assigned to some other
variable. For example the statements
z = ++x; and z = x++; have different effects.
Sample scenario: Inorder to find largest of three numbers say a,b,c , the first expression
evaluates if a>b and if the condition is true, then it evaluates expression 2.i.e (a>c) and the
result is printed. Suppose if the expression 1 is false (a>b), then it evaluates expression 3
(b>c) and prints the result
int x = (a>b) ? (a>c) : (b>c);
22.Write a c program to demonstrate the output for logical expressions (a>5)&&(b>2) and
(a>5)||(b>2) applying a and b value as 7 and 4.
Soln:
#include <stdio.h>
void main()
{
int a=7,b=4;
int c,d;
c=(a>5)&&(b>2);
d=(a>5)||(b>2);
printf ("%d %d",c,d);
}
30.In the following snippet , how many times the message IFET is printed?
#include <stdio.h>
void main()
{
int i = 0, j = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 1;)
{
break;
}
printf("IFET\n");
}}
Soln: IFET will be printed 5 times.
Explanation: When the control comes to inner loop, condition will always be true as j is less than
1 and it will break the inner loop.
31.Develop a small program to find the sum of first ‘n’ natural numbers.
Soln:
#include <stdio.h>
int main()
{
int n, count, sum = 0;
printf("Enter the value of n(positive integer): ");
scanf("%d",&n);
count=1;
while(count <= n)
{
sum = sum + count;
count++;
}
printf("Sum of first %d natural numbers is: %d",n, sum);
return 0;
}
Output
Enter the value of n(positive integer): 5
Sum of first 5 natural numbers is: 15
33.Write a program to calculate the sum of numbers from m to n using while loop.
Soln:
#include <stdio.h>
int main()
{
int n, m, i, sum =0;
printf("\n Enter the value of m : ");
scanf("%d", &m);
i=m;
printf("\n Enter the value of n : ");
scanf("%d", &n);
while(i<=n)
{
sum = sum + i;
i = i + 1;
}
printf("\n The sum of numbers from %d to %d = %d", m, n, sum);
return 0;
}
Output:
Enter the value of m : 2
Enter the value of n : 10
The sum of numbers from 2 to 10 = 54
36.What is the formula to find the length of an array? Calculate the length of the array given
below:
Let Age[5] be an array of integers such that
Age[0] = 2, Age[1] = 5, Age[2] = 3, Age[3] = 1, Age[4] = 7
Soln:
The memory representation of the array Age[5] is given as below.
2 5 3 1 7
Age[ ] Age[1] Age[2] Age[3] Age[4]
Length = upper_bound – lower_bound + 1
Here, lower_bound = 0, upper_bound = 4
Therefore, length = 4 – 0 + 1 = 5
40.Data[] is an array that is declared as int Data[10]; and contains the following values:
Data[] = {12, 23, 34, 45, 56, 67, 78, 89, 90, 100};
(a) If a data element with value 56 has to be deleted, find its position.
(b) Delete the data element 56 and show the memory representation after the deletion.
Soln:
(a)Since the elements of the array are stored in ascending order, we will compare the value that
has to be deleted with the value of every element in the array.As soon as VAL= Data[I],
where I is the index or subscript of the array, we will get the position from which the
element has to be deleted.
For example, if we see this array, here VAL = 56. Data[0] = 12 which is not equal to 56. We
will continue to compare and finally get the value of POS = 4.
(b)
12 23 34 45 67 78 89 90 100
Data[ ] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] Data[7] Data[8]
PART B
6. Develop a C program for the following:
i) To swap two numbers without using temporary variable
#include <stdio.h>
#include <conio.h>
void main()
{
int x,y;
clrscr();
printf(“\n Enter the first number”);
scanf(“%d”,&x);
printf(“\n Enter the second number”);
scanf(“%d”,&y);
printf("Before Swapping: The value of x is %d and value of y is %d", x, y);
x = x + y;
y = x - y;
x = x - y;
printf("After Swapping: The value of x is %d and value of y is %d", x, y);
getch();
}
Sample Output:
Enter the first number 10
Enter the second number 20
Before Swapping: The value of x is 10 and value of y is 20
After Swapping: The value of x is 20 and value of y is 10
ii) To find the given number is Armstrong or not.
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
Sample Output:
enter the number=153
armstrong number
Sample Output:
Enter size of array: 5
Enter element to search:10
20
30
40
50
40 is found at position 3
12.Write a C program to print the unique elements in an array
Soln:
#include <stdio.h>
#include<conio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int count(int arr[], int n);
int i,n,arr[1000];
clrscr();
printf("enter the no of elements in array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("No of distinct elements are %d \n",count(arr, n));
return 0;
getch();
}
int count(int arr[], int n)
{
int res = 1;
for (int i = 1; i < n; i++)
{
int j = 0;
for (j = 0; j < i; j++)
if (arr[i] == arr[j])
break;
if (i == j)
res++;
}
return res;
}
OUTPUT
Enter the no of elements in array 7
10 15 20 20 25 30 35
No of distinct elements are 6
14. Write a program to input two m*n matrices and then calculate the sum of their
corresponding elements and store it in a third m *n matrix.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j;
int rows1, cols1, rows2, cols2, rows_sum, cols_sum;
int mat1[5][5], mat2[5][5], sum[5][5];
clrscr();
printf("\n Enter the number of rows in the first matrix : ");
scanf("%d",&rows1);
printf("\n Enter the number of columns in the first matrix : ");
scanf("%d",&cols1);
printf("\n Enter the number of rows in the second matrix : ");
scanf("%d",&rows2);
printf("\n Enter the number of columns in the second matrix : ");
scanf("%d",&cols2);
if(rows1 != rows2 || cols1 != cols2)
{
printf("\n Number of rows and columns of both matrices must be equal");
getch();
exit();
}
rows_sum = rows1;
cols_sum = cols1;
printf("\n Enter the elements of the first matrix ");
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("\n Enter the elements of the second matrix ");
for(i=0;i<rows2;i++)
{
for(j=0;j<cols2;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<rows_sum;i++)
{
for(j=0;j<cols_sum;j++)
sum[i][j] = mat1[i][j] + mat2[i][j];
}
printf("\n The elements of the resultant matrix are ");
for(i=0;i<rows_sum;i++)
{
printf("\n");
for(j=0;j<cols_sum;j++)
printf("\t %d", sum[i][j]);
}
return 0;
}
Output
Enter the number of rows in the first matrix: 2
Enter the number of columns in the first matrix: 2
Enter the number of rows in the second matrix: 2
Enter the number of columns in the second matrix: 2
Enter the elements of the first matrix
1234
Enter the elements of the second matrix
5678
The elements of the resultant matrix are
6 8
10 12
15.(i). Write a program to insert a number in an array that is already sorted in ascending
order.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, j, num, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\n Enter the number to be inserted : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] > num)
{
for(j = n–1; j>=i; j––)
arr[j+1] = arr[j];
arr[i] = num;
break;
}
}
n = n+1;
printf("\n The array after insertion of %d is : ", num);
for(i=0;i<n;i++)
printf("\n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 4
arr[3] = 5
arr[4] = 6
Enter the number to be inserted : 3
The array after insertion of 3 is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6
ii. Write a program to delete a number from an array that is already sorted in ascending
order.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, j, num, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\n Enter the number to be deleted : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
for(j=i; j<n–1;j++)
arr[j] = arr[j+1];
}
}
n = n–1;
printf("\n The array after deletion is : ");
for(i=0;i<n;i++)
printf("\n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the number to be deleted : 3
The array after deletion is :
arr[0] = 1
arr[1] = 2
arr[2] = 4
arr[3] = 5