cs3251 UNIT II QUESTION BANK

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

UNIT II

PART A
1. Write a C program to find mean of n numbers using one dimensional array. AP
Ans: #include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (i = 0; i < n; ++i) {
printf("%d. Enter a number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f\n", avg);
return 0;
}

2. Point out an example code to initialize values for a two dimensional array. AP
Ans : #include <stdio.h>
int main() {
// Directly initializing a 2D array at the time of declaration
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

3. Sort the following elements through selection sort method 23,55,16,78,2.


AP
Ans:
First Pass:
The smallest element is 2. Swap it with the first element (23).
Result: 2, 55, 16, 78, 23
Second Pass:
The smallest element is 16. Swap it with the second element (55).
Result: 2, 16, 55, 78, 23
Third Pass:
The smallest element is 23. Swap it with the third element (55).
Result: 2, 16, 23, 78, 55
Fourth Pass:
The smallest element is 55 (already in place).
Result: 2, 16, 23, 78, 55
Fifth Pass:
The smallest element is 78 (already in place).
Result: 2, 16, 23, 78, 55
Sorted Array: The final sorted array is: 2, 16, 23, 55, 78.

4. Illustrate function declaration, definition and call in C program. AP


Ans: #include <stdio.h>
int sum(int a, int b); // Function declaration (prototype)
int main()
{
int result = sum(10, 30);//function call
printf("Sum is: %d\n", result);
return 0;
}
int sum(int a, int b) // Function definition
{
return a + b;
}

5. show a function is nested when number of functions are called in a program.


AP
#include <stdio.h>
int main()
{
printf("This is main method in c\n");
function_one();
getch();
return 0;
}
void function_one()
{
printf("This is a user define function\n");
function_two();
}
void function_two()
{
printf("This is nested function in c\n");
}

6. Write an example for compile time initialization of character arrays [strings]. AP


7. Ans: char name[] = "christopher";
char name[] = {'c', 'h', 'r', 'i', 's', 't', 'o', 'p', 'h', 'e', 'r'};
8. Write a C program that declares an integer array named “numbers” of size 5 and assign
values.
AP
Ans: #include <stdio.h>
int main()
9.
{
int numbers[5];
numbers[5] = {10, 20, 30, 40, 50};
return 0;

}
10. Write a C function to reverse array of characters in a string using strrev() function.
AP
Ans: #include <stdio.h>
#include <string.h>

11. int main() {


char str[50] = "computer";
printf("The given string is: %s\n", str);
printf("After reversing, the string is: %s", strrev(str));
return 0;
}
12. Given an array int a[10] = {101,102,103,104,105,106,107,108,109,110}. Show the
memory representation and calculate the length.
AP

101 102 103 104 105 106 107 108 109 110
arr[0]
13.
lower bound upper
bound
length of an array : upper bound – lower bound +1
= 9-0+1
=10

14. 15.Write a function call to the calculateArea function, passing arguments 5.0 and 3.0 for
length and width respectively, and print the result.
AP
Ans: #include <stdio.h>
void FindArea(float length, float width) {
float area = length * width;
printf("Area of the rectangle is: %f units\n", area);
}
int main() {
float length = 5.0;
float width = 3.0;
FindArea(length, width);
return 0;
}
16. Write a program that utilizes the sqrt function from the <math.h> library to calculate the
square root of a user-inputted number.
AP
Ans: #include <stdio.h>
#include <math.h>
int main() {
17. double num, root;
printf("Enter any number to find the square root: ");
scanf("%lf", &num);
root = sqrt(num);
printf("Square root of %.2lf = %.2lf\n", num, root);
return 0;
}
18. What is the useof „\0‟ character? U

When declaring character arrays (strings), „\0‟ (NULL) character is automatically added at
19.end.

The „\0‟ character acts as an end of character array.

20. Example for character arrays [strings]. U

#include <stdio.h>main(){

static char name1[] = {‘H’,’e’,’l’,’l’,’o’};


21.
static char name2[] = “Hello”;

printf(“%s\n”, name1);

printf(“%s\n”, name2);}

22. Write a program to copy the value of an array to another. AP

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

23.i,a[5],b[5]={1,2,3,4,5};for(i=0;i<5;i++)a[i]=b[i]; printf(“The content of array


A:\n”);for(i=0;i&lt;5;i++) printf(“%d”,a[i]);}

24. List the different methods for reading and writing a string. U

 The different methods for reading a string are, scanf(),gets(),getchar(),getch() or


getche()
25.
 The different methods for writing a string are, printf(),puts(),putchar()

26. 27.Mention the various string manipulation function in C.


U

S.No Function Purpose

1. strcpy(s1,s2) Copies string s2 into string s1.

2. strcat(s1,s2) Concatenates string s2 onto the end of string s1.


3. strlen(s1) Returns the length of string s1

4. strcmp(s1,s2)Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.

5. strchr(s1,ch) Returns a pointer to the first occurrence of character ch in string s1.

6. strst(s1,s2) Returns a pointer to the first occurrence of sting s2 in string s1

28. Write a C program to Read & write Strings in C using Printf() and Scanf() functions AP

#include <stdio.h>#include <string.h>int main(){char nickname[20]; printf(“Enter your


Nick

name:”);scanf(“%s”, nickname); printf(“%s”,nickname);return 0;}


29.
Output:

Enter your Nick name: Negan Negan

PART B

1 Write C program
To perform string copy, string concatenation,
AP
and string length given s1= ”COMPUTER” s2 = “SCIENCE”
2 to perform matrix addition using two dimensional array.
AP
3 Write a C program
(i) to perform addition operation by passing arguments and return values (8)
AP

4 Write a C program To perform factorial using recursion


AP
5 Using an array representation perform binary search to search a given number in the
following array of numbers.
AP
20,40,60,80,100,120,140
6 Write a c program to perform linear search of a number given array of integers
AP
7 Write a C program using recursion
to find factorial of a given number
AP
8 Write a C program to find sum of numbers
AP
9 Write a C program to count number of words in a sentence.
AP
1 Write a C program to merge and sort the elements of two different arrays.
0 AP

You might also like