0% found this document useful (0 votes)
54 views7 pages

MODULE 3 4 Important 10 Marks Ans

Uploaded by

Dinesh Kumar
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)
54 views7 pages

MODULE 3 4 Important 10 Marks Ans

Uploaded by

Dinesh Kumar
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/ 7

MODULE 3

1. C Program for Mean, Median, and Mode


#include <stdio.h>
#include <stdlib.h>

void sortArray(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

float calculateMean(int arr[], int n) {


int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return (float)sum / n;
}

float calculateMedian(int arr[], int n) {


sortArray(arr, n);
if (n % 2 == 0) {
return (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
} else {
return arr[n / 2];
}
}

int calculateMode(int arr[], int n) {


int maxCount = 0, mode = arr[0];
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mode = arr[i];
}
}
return mode;
}

int main() {
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Mean: %.2f\n", calculateMean(arr, n));


printf("Median: %.2f\n", calculateMedian(arr, n));
printf("Mode: %d\n", calculateMode(arr, n));

return 0;
}

2. String Handling Functions in C

String Handling Functions in C include:

• strcpy(): Copies one string to another.


• strcat(): Concatenates two strings.
• strlen(): Returns the length of a string.
• strcmp(): Compares two strings.
• strrev(): Reverses a string (not standard, but often
available).
• strchr(): Finds the first occurrence of a character in a
string.

Example:
#include <stdio.h>
#include <string.h>

int main() {
char str1[50], str2[50];

printf("Enter first string: ");


scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
printf("Length of str1: %ld\n", strlen(str1));
strcpy(str2, str1);
printf("str2 after copying: %s\n", str2);
strcat(str1, str2);
printf("str1 after concatenation: %s\n", str1);

return 0;
}

3. Linear and Binary Search

Linear Search: Sequentially checks each element in the array.

Binary Search: Divides the array into halves and searches.

Example Program for Both:


#include <stdio.h>

void linearSearch(int arr[], int n, int key) {


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Element found at index %d\n", i);
return;
}
}
printf("Element not found.\n");
}

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}

int main() {
int n, key;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements (sorted for binary search): ",
n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);

printf("Linear Search:\n");
linearSearch(arr, n, key);

printf("Binary Search:\n");
int index = binarySearch(arr, n, key);
if (index != -1) {
printf("Element found at index %d\n", index);
} else {
printf("Element not found.\n");
}

return 0;
}

4. C Program for Sorting in Descending Order


#include <stdio.h>

void sortDescending(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
sortDescending(arr, n);
printf("Array in descending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

MODULE 4

1. User-Defined Function and Factorial Calculation

User-Defined Functions: Functions defined by the user.


Components include:

• Function declaration.
• Function definition.
• Function call.

Factorial Program:
#include <stdio.h>

int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, factorial(n));
return 0;
}

2. Function Prototypes Based on Parameters and Return Types

1. No Parameters, No Return:
void sayHello() {
printf("Hello, World!\n");
}

2. With Parameters, No Return:


void printSquare(int n) {
printf("Square: %d\n", n * n);
}

3. With Parameters, With Return:


int add(int a, int b) {
return a + b;
}

4. No Parameters, With Return:


int getTen() {
return 10;
}

3. Binary Search Using Recursion


#include <stdio.h>

int binarySearch(int arr[], int low, int high, int key) {


if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] > key) {
return binarySearch(arr, low, mid - 1, key);
} else {
return binarySearch(arr, mid + 1, high, key);
}
}

int main() {
int n, key;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements (sorted): ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);

int result = binarySearch(arr, 0, n - 1, key);


if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found.\n");
}
return 0;
}

You might also like