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

105360 10 19UCSES301 B 3 12Unit 1 Solved Application Qns

The document contains programming exercises and solutions related to the fundamentals of data structures in C, including examples of C programs for various tasks such as calculating areas, finding sizes of data types, and implementing algorithms like Fibonacci series and Armstrong number checks. It also discusses concepts like null characters in strings, logical expressions, and operations on arrays and matrices. The document serves as a practical guide for students to understand and apply basic programming concepts in C.

Uploaded by

vv8871455
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)
41 views12 pages

105360 10 19UCSES301 B 3 12Unit 1 Solved Application Qns

The document contains programming exercises and solutions related to the fundamentals of data structures in C, including examples of C programs for various tasks such as calculating areas, finding sizes of data types, and implementing algorithms like Fibonacci series and Armstrong number checks. It also discusses concepts like null characters in strings, logical expressions, and operations on arrays and matrices. The document serves as a practical guide for students to understand and apply basic programming concepts in C.

Uploaded by

vv8871455
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

IFET COLLEGE OF ENGINEERING

DEPARTMENT OF ECE
SUBJECT CODE: 19UCSES301 SEM: III
SUBJECT NAME: FUNDAMENTALS OF DATA STRUCTURES IN C YEAR: II
UNIT- I PROGRAMMING BASICS

SOLVED APPLICATION QUESTIONS

1. Illustrate the structure of a C program.

5.Write a C program to calculate the area of a circle.


#include <stdio.h>
#include <math.h>
int main()
{
float radius, area;
printf("Enter the radius of a circle\n");
scanf("%f", &radius);
area = 3.14159*radius*radius;
printf("Area of the circle = %.2f\n", area);
return 0;
}

10. the need of null character at the end of string.


A null character is used to mark the end of a character string .The reason for a null
terminator on your string is because once it is broken down into assembly language each
character gets a byte of sequential logical memory allocated to it in the stack in the main
memory (RAM) and that is what the computer looks for to know 2 things.
1. The length of the string to find available memory.
To know where the end of the string is otherwise it will continue to read
sequentially through memory until it finds a null terminator.

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;
}

18.What is the output of the following program?


main() {
int x = 10;
printf ("%d %d", ++x, ++x);}

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.

19.Write a suitable scenario where conditional operator shall be used.

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);

21.Give the output for the following Shift operations


i) a<<2
ii) a>>2
Soln:
Assume a=12, then a<<2 = 48 and a>>2=3
Explanation:
#include<stdio.h>
int main()
{
int a = 12;
printf ("a << 2 = %d\n", a << 2);
printf ("a >> 2 = %d\n", a >> 2);
return 0;
}

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);
}

27.Write a program in C to generate Fibonacci series like following: 1, 1, 2, 3, 5, 8, 13 …


#include <stdio.h>
int main() {
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

28.Write a program in C to find whether the given number is odd or even


#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}

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

enter the number=5


not armstrong number
11. Formulate a C program to search an element from an array.
#include <stdio.h>
#include<conio.h>
int main()
{
int arr[20];
int size, i, toSearch, found;
/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);
/* Input elements of array */
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter element to search: ");
scanf("%d", &toSearch);

/* Assume that element does not exists in array */


found = 0;
for(i=0; i<size; i++)
{
/* If element is found in array then raise found flag and terminate from loop.*/
if(arr[i] == toSearch)
{
found = 1;
break;
}
}
/* If element is not found in array */
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}
return 0;
}

Sample Output:
Enter size of array: 5
Enter element to search:10
20
30
40
50

Enter element to search: 40

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

You might also like