USP Minor Assignment - 2 (Saswat Mohanty - 1941012407 - CSE-D)
USP Minor Assignment - 2 (Saswat Mohanty - 1941012407 - CSE-D)
USP Minor Assignment - 2 (Saswat Mohanty - 1941012407 - CSE-D)
1941012407
5th D
Contents
Sl. No. Date Page No. Remarks
Name of the Minor Assignment/ Major
Assignment/ End-Term
Project
1. For the given structure below, declare the variable type, and print their values;
Code: -
#include <stdio.h>
int main()
{
int Ia=345;
float Fb=4.5f;
char Chvar='Z';
printf("\nIa: %d",Ia);
printf("\nFb: %f",Fb);
printf("\nChvar: %c",Chvar);
return 0;
}
Output: -
2. For the given structure below, declare the variable type, print their values and
addresses;
Code: -
#include<stdio.h>
int main()
{
int Ia=345;
float Fb=4.5;
char Chvar='Z';
printf("Ia= %d and address:%u\n", Ia,&Ia);
printf("FB= %f and address:%u\n", Fb,&Fb);
printf("Chvar= %c and address:%u\n", Chvar,&Chvar);
return 0;
}
Output: -
3. Declare two integer variable and assign values to them, and print their addresses.
Additionally, Swap the contents of the variables and print their addresses after swap.
State whether the addresses before and after are equal or not.
Code: -
#include<stdio.h>
int main()
{
int a=5,b=7;
printf("\nBEFORE SWAPPING->\n");
printf("The value of a: %d and address of a: %u\n", a,&a);
printf("The value of b: %d and address of b: %u\n", b,&b);
a=a+b;
b=a-b;
a=a-b;
printf("AFTER SWAPPING->\n");
printf("The value of a: %d and address of a: %u\n", a,&a);
Output: -
4. Write the C statement to declare and initialize the pointer variable for the given
structure and display the values of x, y and z with the help of p.
Code: -
#include<stdio.h>
int main()
{
int x=45,y=12,z=23;
int *p;
p=&x;
printf("The value of x : %d\n", *p);
p=&y;
printf("The value of y : %d\n", *p);
p=&z;
printf("The value of z : %d\n", *p);
return 0;
}
Output: -
5. Write the C statement to declare and initialize the pointer variable for the given
structure.
Code: -
#include<stdio.h>
int main()
{
float x=6.7,y=1.2,z=2.3;
float *p;
p=&x;
printf("x : %.1f\n",*p);
p=&y;
printf("y : %.1f\n",*p);
p=&z;
printf("z : %.1f\n\n",*p);
return 0;
}
Output: -
6. Write the C statement to declare and initialize the pointer variable for the given
structure.
Code: -
#include<stdio.h>
void main()
{
int x=89;
int *p1,*p2,*p3;
p1=&x;
printf("\nx through p1 : %d\n",*p1);
p2=&x;
printf("x through p2 : %d\n",*p2);
p3=&x;
printf("x through p3 : %d\n",*p3);
}
Output: -
7. Write the C statement to declare and initialize the pointer variable for the given
structure.
Code: -
#include<stdio.h>
void main()
{
float x=8.6;
float *p1,*p2,*p3;
p1=&x;
printf("\nx through p1 : %.1f\n",*p1);
p2=&x;
printf("x through p2 : %.1f\n",*p2);
p3=&x;
printf("x through p3 : %.1f\n\n",*p3);
}
Output: -
8. Write the C statement to declare and initialize the pointer variable for the given
structure and update the values of a, b and c to be incremented by 10.
Code: -
#include<stdio.h>
int main()
{
int a=12,b=25,c=18;
int *ptr;
printf("a = %d\n",a);
printf("b = %d\n",b);
printf("c = %d\n",c);
printf("AFTER INCREMENTING->\n");
ptr=&a; printf("a = %d\n",*ptr+10);
ptr=&b; printf("b = %d\n",*ptr+10);
ptr=&c; printf("c = %d\n",*ptr+10);
}
Output: -
9. Write the C statement to declare and initialize the pointer variable for the given
structure and update the values of a, b and c to be incremented by 10.
Code: -
#include<stdio.h>
int main()
{
int a=12,b=52,c=8;
int *ptr1,*ptr2,*ptr3;
Output: -
Code: -
#include <stdio.h>
int main()
{
int Ivar=454;
int *ptr=&Ivar;
int **Tptr=&ptr;
Output: -
11. Two pointers are pointing to different variable. Write the C++ statement to find the
greater between a, and b through pointer manipulation.
Code: -
#include <stdio.h>
int main()
{
int a=52;
int b=18;
int *ptr1=&a;
int *ptr2=&b;
if(*ptr1>*ptr2)
{
printf("a=%d is grater than b=%d",*ptr1,*ptr2);
}
else
{
printf("b=%d is greater than a=%d",*ptr2,*ptr1);
}
return 0;
}
Output: -
12. Write the C++ statement to manipulate the value of the variable Ia through
the pointers ptr1, ptr2, and ptr3.
We can use different pointers to
point the same data variable.
For example;
1. int Ia;
2. int ∗ptr1 = &Ia;
3. · · · // manipulate the variable Ia
4. int ∗ptr2 = &Ia;
5. · · · // manipulate the variable Ia
6. · · ·
Code: -
#include <stdio.h>
int main(){
int a=52;
int b=18;
int *ptr1=&a;
int *ptr2=&b;
if(*ptr1>*ptr2){
printf("a=%d is grater than b=%d",*ptr1,*ptr2);
}
else{
printf("b=%d is greater than a=%d",*ptr2,*ptr1);
}
return 0;
}
Output: -
int m = 10, n = 5;
int *mp, *np;
mp = &m;
np = &n;
*mp = *mp + *np;
*np = *mp - *np;
printf("%d %d\n%d %d\n", m, *mp, n, *np); /*line-1 */
Code: -
#include<stdio.h>
int main(){
int m = 10, n = 5;
int *mp, *np;
mp = &m;
np = &n;
*mp = *mp + *np;
*np = *mp - *np;
Output: -
m = &n;
itemp = m;
*itemp = c;
*itemp = &c;
Ans: -
15. Write a prototype for a function sum n avg that has three type double input
parameters and two output parameters. The function computes the sum and the
average of its three input arguments and relays its results through two output
parameters.
Ans: -
16. Given the memory setup shown, fill in the chart by indicating the data type
and value of each reference as well as the name of the function in which the
reference would be legal. Describe pointer values by referring to cell attributes.
Ans: -
17. The following code fragment is from a function preparing to call sum n avg.
Complete the function call statement.
Define the function sum n avg whose prototype you wrote in question-15. The
function should compute both the sum and the average of its three input
parameters and relay these results through its output parameters. Design the
complete C code to get the desire result.
Code: -
#include <stdio.h>
void sum_n_avg(double,double,double,double*,double*);
int main(){
double one,two,three,sum, avg;
double *ptr1=&sum,*ptr2=&avg;
printf("Enter the three numbers: ");
scanf("%lf%lf%lf",&one, &two, &three);
sum_n_avg(one,two,three,ptr1,ptr2);
printf("Sum = %lf, avg= %lf",*ptr1,*ptr2);
return 0;
}
void sum_n_avg(double a, double b, double c,double *ptr1,double *ptr2){
*ptr1=a+b+c;
*ptr2=*ptr1/3;
}
Output: -
19. Show the table of values for x , y , and z that is the output displayed by the
following program.
Code: -
#include <stdio.h>
void sum(int a, int b, int *cp);
int main(void){
int x, y, z;
x = 7; y = 2;
printf("x y z\n\n");
sum(x, y, &z);
printf("%4d%4d%4d\n", x, y, z);
sum(y, x, &z);
printf("%4d%4d%4d\n", x, y, z);
sum(z, y, &x);
printf("%4d%4d%4d\n", x, y, z);
sum(z, z, &x);
printf("%4d%4d%4d\n", x, y, z);
sum(y, y, &y);
printf("%4d%4d%4d\n", x, y, z);
return (0);
}
void sum(int a, int b, int *cp)
{
*cp = a + b;
}
Output: -
xyz
729
729
11 2 9
18 2 9
18 4 9
20.
a) Classify each formal parameter of double trouble and trouble as input,
output, or input/output.
b) What values of x and y are displayed by this program?
void double_trouble(int *p, int y);
void trouble(int *x, int *y);
int main(void){
int x, y;
trouble(&x, &y);
printf("x = %d, y = %d\n", x, y);
return (0);
}
void double_trouble(int *p, int y){
int x;
x = 10;
*p = 2 * x - y;
}
void trouble(int *x, int *y){
double_trouble(x, 7);
double_trouble(y, *x);
}
Ans: -
int x, y;
trouble(&x, &y);
printf("x = %d, y = %d\n", x, y);
return (0);
}
int *x→&x
int *y→&y
22. Write a program in C to add numbers using function via call by reference.
Code: -
#include <stdio.h>
void add(int *n1, int *n2);
int main(void) {
int n1, n2;
printf("Input the first number: ");
scanf("%d", &n1);
printf("Input the second number: ");
scanf("%d", &n2);
printf("The sum of %d and %d is ", n1, n2);
add(&n1, &n2);
printf("%d\n", n1);
return 0;
}
void add(int *n1, int *n2) {
*n1 = *n1 + *n2;
return;
}
Output: -
23. Write a program in C to find the maximum and minimum number in an array having
5 elements.
Code: -
#include <stdio.h>
void maxMin(int arr[5], int n);
int main(void) {
int arr[5];
printf("\n\tInput 5 elements in the array: ");
for(int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);}
for(int i = 0; i < 5; i++) {
printf("\tarr[%d]: %d\n", i, arr[i]);}
maxMin(arr, 5);
return 0;
}
void maxMin(int arr[5], int n) {
int max = 0, min = arr[0];
for(int i = 0; i < n; i++) {
if(arr[i] > max) {
max = arr[i];}
if(arr[i] < min) {
min = arr[i];
}}
printf("\tMaximum element is: %d\n", max);
printf("\tMinimum element is: %d\n\n", min);
}
Output: -
Code: -
#include <stdio.h>
void addition(int *n1, int *n2);
void subtraction(int *n1, int *n2);
void multiplication(int *n1, int *n2);
void division(int *n1, int *n2);
int main(void) {
int n1, n2, n;
printf("1 - ADDITION\n");
printf("2 - SUBTRACTION\n");
printf("3 - MULTIPLICATION\n");
printf("4 - DIVISION\n");
printf("Please Enter The Number As Mentioned To Do Calculation: ");
scanf("%d", &n);
if(n >= 1 && n <= 4) {
printf("Enter The First Number: ");
scanf("%d", &n1);
printf("Enter The Second Number: ");
scanf("%d", &n2);
if(n == 1) {
printf("%d + %d = ", n1, n2);
addition(&n1, &n2);
printf("%d\n", n1);
}
else if(n == 2) {
printf("%d - %d = ", n1, n2);
subtraction(&n1, &n2);
printf("%d\n", n1);
}
else if(n == 3) {
printf("%d * %d = ", n1, n2);
multiplication(&n1, &n2);
printf("%d\n", n1);
}
else {
Output: -
Code: -
#include<stdio.h>
#include<ctype.h>
void main()
{
char str[100];
int count,ch,i;
printf("Enter the String: \n");
for(i=0;(str[i]=getchar())!='\n';i++)
{
;
}
str[i]='\0';
count=i;
printf("The given string is : %s",str);
printf("\nNew bstring after changing the case is : ");
for(i=0;i<count;i++)
{
ch=islower(str[i])? toupper(str[i]):tolower(str[i]);
putchar(ch);
}
}
Output: -
26. Create a program to compute the mean and standard deviation of an array of
data given below and displays the difference between each value and the mean.
15.3 4 90 34 2.5 104 7 25 82
For MAX ITEM data items, if we assume that x is an array whose lowest subscript is 0,
the standard deviation is given by the formula.
Code: -
#include <stdio.h>
#include <math.h>
#define MAX_ITEM 10
void main()
{
float x[MAX_ITEM];
int i, n;
float mean, std_deviation, sum = 0, sum1 = 0,diffrence;
printf("\nEnter the value of N \n");
scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++){
scanf("%f", &x[i]);
}
for (i = 0; i < n; i++){
sum = sum + x[i];
}
mean = sum / (float)n;
for(i=0;i<n;i++){
diffrence=mean-x[i];
printf("\nThe diffrence is : %.2f ",diffrence);
}
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - mean), 2);
}
Output: -
27. You have two independent sorted arrays of size m, and n respectively, where
m, n > 0. You are required to merge the two arrays such that the merged array
will be in sorted form and will contain exactly m + n number of elements. You
are not allowed to use any kind of sorting algorithm. Design your program to
meet the above given requirement. Assume the elements of the array are non-
negative integers. The elements can be read from the keyboard or can be
generated randomly.
Code: -
#include <stdio.h>
#include <limits.h>
int main(void){
int m, n, i;
printf("Enter Size Of First Array: ");
scanf("%d", &m);
int a1[m];
return 0; }
Output: -
28. Write a program to declare one array for storing the square roots of the
integers from 0 through 10 and a second array for storing the cubes of the same
integers.
Code: -
#include <stdio.h>
#include<math.h>
double square_root(int n){
double sq;
sq=sqrt(n);
return sq;}
int cube_func(int n){
int cb;
cb=n*n*n;
return cb;}
int main(){
double root[11];
int cubes[11];
printf("\n");
for(int i=0;i<11;i++){
root[i]=square_root(i);}
for(int i=0;i<11;i++){
cubes[i]=cube_func(i);}
Output: -
29. Write a program to use the user-defined function multiply that takes two
type int array input arguments and their effective size and produces a result
array containing the sums of corresponding elements. For example, for the
three-element input arrays 5 1 7 and 2 4 2 , the result would be an array
containing 7 3 5 .
Code: -
#include <stdio.h>
void multiply(int a1[], int a2[], int n, int *a);
int main(void) {
int n, i;
printf("\n");
printf("Enter The Size Of The Array: ");
scanf("%d", &n);
int a1[n], a2[n], a[n];
printf("Enter The Elements Of First Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a1[i]);
}
printf("Enter The Elements Of Second Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a2[i]);
}
printf("Result Array: \n");
multiply(a1, a2, n, a);
for(i = 0; i < n; i++){
printf("%d ", a[i]);
}
printf("\n\n");
return 0;
}
void multiply(int a1[], int a2[], int n, int *a) {
int i;
for(i = 0; i < n; i++) {
a[i] = a1[i] + a2[i];
}}
Output: -
31. Write a program which uses a user defined function, find largest() to find the
largest element of an integer array.
Code: -
#include <stdio.h>
int find_largest(int a[], int n);
int main(void)
{
int n;
printf("Enter Size Of The Array: \n");
scanf("%d", &n);
int a[n], i;
printf("Enter Value To The Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int max = find_largest(a, n);
printf("Largest Element In The Array Is: %d\n", max);
return 0;
}
int find_largest(int a[], int n) {
int max = 0, i;
for(i = 0; i < n; i++) {
if(max < a[i]) {
max = a[i];
}
}
return max;
}
Output: -
32. The bubble sort is another technique for sorting an array. A bubble sort
compares adjacent array elements and exchanges their values if they are out of
order. In this way, the smaller values “bubble” to the top of the array (toward
element 0), while the larger values sink to the bottom of the array. After the
first pass of a bubble sort, the last array element is in the correct position; after
the second pass the last two elements are correct, and so on. Thus, after each
pass, the unsorted portion of the array contains one less element. Write and
test a function that implements this sorting method.
Code: -
#include <stdio.h>
void bubblesort(int a[], int n);
int main(void) {
int n;
printf("Enter Size Of The Array: ");
scanf("%d", &n);
int a[n], i;
printf("Enter Value To The Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);}
printf("Given Array Is: \n");
for(i = 0; i < n; i++) {
printf("%d ", a[i]);}
bubblesort(a, n);
printf("\nSorted Array Using Bubble Sort Is: \n");
for(i = 0; i < n; i++) {
printf("%d ", a[i]);}
printf("\n");
return 0;
}
void bubblesort(int a[], int n) {
int i, j;
for(i = 0; i < n; i++) {
for(j = 0; j < n - 1; j++) {
if(a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
Output: -
33. The binary search algorithm that follows may be used to search an array
when the elements are in order. The algorithm for binary search given as;
1. Let bottom be the subscript of the initial array element.
2. Let top be the subscript of the last array element.
3. Let found be false.
4. Repeat as long as bottom isn’t greater than top and the target has not been found
5. Let middle be the subscript of the element halfway between bottom and top.
6. if the element at middle is the target
7. Set found to true and index to middle.
else if the element at middle is larger than the target
8. Let top be middle - 1.
else
9. Let bottom be middle + 1.
Write and test a function binary srch that implements this algorithm for an array of integers.
When there is a large number of array elements, which function do you think is faster:
binary_srch or the linear search algorithm.
Code: -
#include <stdio.h>
typedef enum {false, true} boolean;
boolean binary_srch(int a[], int *bottom, int top, int target);
int main(void) {
int n;
printf("Enter Size Of The Array: \n");
scanf("%d", &n);
int a[n], i;
printf("Enter Value To The Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int target;
printf("Enter The Target Value: \n");
scanf("%d", &target);
int bottom = 0, top = n - 1;
boolean b = binary_srch(a, &bottom, top, target);
if(b) {
printf("Target Element Is Present At Index %d In The Array.\n", bottom);
}
else {
printf("Target Element Is Present Not Present In The Array.\n");
}
return 0;
}
boolean binary_srch(int a[], int *bottom, int top, int target) {
boolean found = false;
while(*bottom <= top) {
int middle = *bottom + (top - *bottom) / 2;
if(a[middle] == target) {
found = true;
*bottom = middle;
break;
}
else if(a[middle] > target) {
top = middle - 1;
}
else{
*bottom = middle + 1;
}
}
return found;
}
Output: -
34. Implement the following algorithm for linear search that sets a flag (for loop
control) when the element being tested matches the target.
1. Assume the target has not been found.
2. Start with the initial array element.
3. repeat while the target is not found and there are more array elements
4. if the current element matches the target
5. Set a flag to indicate that the target has been found.
else
6. Advance to the next array element.
7. if the target was found
8. Return the target index as the search result.
else
9. Return -1 as the search result.
Create a user-defined function with prototype int linear search(const int arr[ ],
int target, int n); in your program to search the target element.
Code: -
#include <stdio.h>
Output: -