0% found this document useful (0 votes)
21 views18 pages

Anand Project

The document contains solutions to 5 programming problems in C language. It includes the code to calculate statistics, find correlation, linear regression, matrix operations, and use switch case. Detailed code solutions and explanations are provided for each problem.

Uploaded by

Anand Tripathi
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)
21 views18 pages

Anand Project

The document contains solutions to 5 programming problems in C language. It includes the code to calculate statistics, find correlation, linear regression, matrix operations, and use switch case. Detailed code solutions and explanations are provided for each problem.

Uploaded by

Anand Tripathi
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/ 18

C Practical File

STB-506

Semester V 2023-24

Name: Anand Kumar Tripathi


Exam Roll. No.: 21220STA075
Class Roll. No.: St-67
1. In a mid-semester examination, 10 students of a class scored the following marks
(out of 50):
25,18,27,8,30,42,20,50,45,18
WAP to calculate the following for the given data:
(a) Mean, variance and standard deviation
(b) Mode
(c) Median
(d) Moments, skewness and kurtosis

Solution 1:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

//functions for various questions

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

float sum = 0;

for (int i = 0; i < n; i++) {

sum += marks[i];

return sum / n;

float calculateVariance(int marks[], int n, float mean) {

float sum = 0;

for (int i = 0; i < n; i++) {

sum += pow(marks[i] - mean, 2);

return sum / n;

}
float calculateStandardDeviation(float variance) {

return sqrt(variance);

float calculateMoments(int marks[], int n, float mean, int moment) {

float sum = 0;

for (int i = 0; i < n; i++) {

sum += pow(marks[i] - mean, moment);

return sum / n;

int compare(const void *a, const void *b) {

return (*(int *)a - *(int *)b);

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

// Sort the array

qsort(marks, n, sizeof(int), compare);

// If the number of elements is odd, return the middle element

// If the number of elements is even, return the average of the two middle elements

if (n % 2 == 0) {

return (marks[n / 2 - 1] + marks[n / 2]) / 2.0;

} else {

return marks[n / 2];

int main() {

// Given data
int marks[] = {25, 18, 27, 8, 30, 42, 20, 50, 45, 18};

int n = sizeof(marks) / sizeof(marks[0]);

float mean = calculateMean(marks, n);

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

float variance = calculateVariance(marks, n, mean);

printf("Variance: %.2f\n", variance);

float std_deviation = calculateStandardDeviation(variance);

printf("Standard Deviation: %.2f\n", std_deviation);

int mode = marks[0];

int maxCount = 1;

int currentCount = 1;

for (int i = 1; i < n; i++) {

if (marks[i] == marks[i - 1]) {

currentCount++;

} else {

if (currentCount > maxCount) {

maxCount = currentCount;

mode = marks[i - 1];

currentCount = 1;

// Check for mode at the end

if (currentCount > maxCount) {

mode = marks[n - 1];

}
printf("Mode: %d\n", mode);

//median

float median = calculateMedian(marks, n);

printf("Median: %.2f\n", median);

// Calculate moments, skewness, and kurtosis

float moments = calculateMoments(marks, n, mean, 2);

printf("Second Moment (Variance): %.2f\n", moments);

float skewness = calculateMoments(marks, n, mean, 3) / pow(variance, 1.5);

printf("Skewness: %.2f\n", skewness);

float kurtosis = calculateMoments(marks, n, mean, 4) / pow(variance, 2) - 3;

printf("Kurtosis: %.2f\n", kurtosis);

return 0;

2. WAP to find the Karl Pearson’s coefficient of correlation from the following data
between height of father (x) and son (y).

X 64 65 66 67 68 69 70

Y 66 67 65 68 70 68 72

Comment on the result.


Solution:

#include <stdio.h>
#include <math.h>

// Function to calculate mean


float calculateMean(int data[], int n) {
float sum = 0;

for (int i = 0; i < n; i++) {


sum += data[i];
}

return sum / n;
}

// Function to calculate Pearson correlation coefficient


float calculatePearsonCoefficient(int x[], int y[], int n) {
float sum_xy = 0, sum_x = 0, sum_y = 0, sum_x_squared = 0, sum_y_squared =
0;

for (int i = 0; i < n; i++) {


sum_xy += x[i] * y[i];
sum_x += x[i];
sum_y += y[i];
sum_x_squared += pow(x[i], 2);
sum_y_squared += pow(y[i], 2);
}

float numerator = n * sum_xy - sum_x * sum_y;


float denominator = sqrt((n * sum_x_squared - pow(sum_x, 2)) * (n *
sum_y_squared - pow(sum_y, 2)));

return numerator / denominator;


}

int main() {
// Given data
int x[] = {64, 65, 66, 67, 68, 69, 70};
int y[] = {66, 67, 65, 68, 70, 68, 72};
int n = sizeof(x) / sizeof(x[0]);

// Calculate Pearson correlation coefficient


float pearsonCoefficient = calculatePearsonCoefficient(x, y, n);

printf("Karl Pearson's Coefficient of Correlation: %.4f\n", pearsonCoefficient);

return 0;
}

3. WAP to find the two regression equations from the following data:

X 6 2 10 4 8

Y 9 11 5 8 7

Solution:
#include <stdio.h>
// Function to calculate mean
float calculateMean(int data[], int n) {
float sum = 0;

for (int i = 0; i < n; i++) {


sum += data[i];
}

return sum / n;
}

// Function to calculate covariance


float calculateCovariance(int x[], int y[], int n, float mean_x, float mean_y) {
float sum = 0;

for (int i = 0; i < n; i++) {


sum += (x[i] - mean_x) * (y[i] - mean_y);
}

return sum / n;
}

// Function to calculate slope of the regression line (b)


float calculateSlope(int x[], int y[], int n, float mean_x, float mean_y) {
float covariance = calculateCovariance(x, y, n, mean_x, mean_y);
float variance_x = calculateCovariance(x, x, n, mean_x, mean_x);

return covariance / variance_x;


}

// Function to calculate intercept of the regression line (a)


float calculateIntercept(float mean_x, float mean_y, float slope) {
return mean_y - slope * mean_x;
}

int main() {
// Given data
int x[] = {6, 2, 10, 4, 8};
int y[] = {9, 11, 5, 8, 7};
int n = sizeof(x) / sizeof(x[0]);

// Calculate mean of x and y


float mean_x = calculateMean(x, n);
float mean_y = calculateMean(y, n);

// Calculate slope (b) of the regression line for y on x


float slope_y_on_x = calculateSlope(x, y, n, mean_x, mean_y);

// Calculate intercept (a) of the regression line for y on x


float intercept_y_on_x = calculateIntercept(mean_x, mean_y, slope_y_on_x);

printf("Regression Equation for y on x: y = %.2fx + %.2f\n", slope_y_on_x,


intercept_y_on_x);

// Calculate slope (b) of the regression line for x on y


float slope_x_on_y = calculateSlope(y, x, n, mean_y, mean_x);

// Calculate intercept (a) of the regression line for x on y


float intercept_x_on_y = calculateIntercept(mean_y, mean_x, slope_x_on_y);

printf("Regression Equation for x on y: x = %.2fy + %.2f\n", slope_x_on_y,


intercept_x_on_y);

return 0;
}

4. A and B are the matrices of 3*3. 𝐴 = [18 10 9 12 14 14 10 13 10 ] and


𝐵 = [8 12 16 10 16 11 17 12 15 ]
WAP to do the following:
(a) A+B
(b) A-B
(c) A transpose and B transpose
(d) A*B

Solution:

#include <stdio.h>

// Function to print a matrix


void printMatrix(int mat[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}

// Function to add two matrices


void addMatrices(int A[3][3], int B[3][3], int result[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}

// Function to subtract two matrices


void subtractMatrices(int A[3][3], int B[3][3], int result[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = A[i][j] - B[i][j];
}
}
}

// Function to calculate transpose of a matrix


void transposeMatrix(int mat[3][3], int result[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = mat[j][i];
}
}
}

// Function to multiply two matrices


void multiplyMatrices(int A[3][3], int B[3][3], int result[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = 0;
for (int k = 0; k < 3; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}

int main() {
// Given matrices A and B
int A[3][3] = {{18, 10, 9}, {12, 14, 14}, {10, 13, 10}};
int B[3][3] = {{8, 12, 16}, {10, 16, 11}, {17, 12, 15}};

// Result matrices
int resultAdd[3][3], resultSubtract[3][3], resultTransposeA[3][3], resultTransposeB[3][3],
resultMultiply[3][3];

// Perform operations
addMatrices(A, B, resultAdd);
subtractMatrices(A, B, resultSubtract);
transposeMatrix(A, resultTransposeA);
transposeMatrix(B, resultTransposeB);
multiplyMatrices(A, B, resultMultiply);

// Display results
printf("A + B:\n");
printMatrix(resultAdd);

printf("\nA - B:\n");
printMatrix(resultSubtract);

printf("\nTranspose of A:\n");
printMatrix(resultTransposeA);

printf("\nTranspose of B:\n");
printMatrix(resultTransposeB);

printf("\nA * B:\n");
printMatrix(resultMultiply);

return 0;
}

5. If a=98 and b=56, WAP using switch to do the following:


(a) a+b
(b) a-b
(c) a*b
(d) a/b
(e) a%b

Solution:

#include <stdio.h>
int main() {
int a = 98;
int b = 56;
int result;

printf("a = %d, b = %d\n", a, b);

printf("\nChoose an operation:\n");
printf("1. a + b\n");
printf("2. a - b\n");
printf("3. a * b\n");
printf("4. a / b\n");
printf("5. a %% b\n");

int choice;
scanf("%d", &choice);

switch (choice) {
case 1:
result = a + b;
printf("a + b = %d\n", result);
break;
case 2:
result = a - b;
printf("a - b = %d\n", result);
break;
case 3:
result = a * b;
printf("a * b = %d\n", result);
break;
case 4:
if (b != 0) {
// Avoid division by zero
result = a / b;
printf("a / b = %d\n", result);
} else {
printf("Error: Division by zero\n");
}
break;
case 5:
if (b != 0) {
// Avoid modulo by zero
result = a % b;
printf("a %% b = %d\n", result);
} else {
printf("Error: Modulo by zero\n");
}
break;
default:
printf("Invalid choice\n");
}

return 0;
}
6. Given below are the three strings:
Str1=CHANDIGARH; Str2=MADRAS; Str3=BOMBAY; Str4=AHMEDABAD
WAP (with-out using string functions) to do the following:
(a) Count the number of characters in all four strings.
(b) Count the number of vowels & consonants in strings.
(c) Concatenate the four strings into one string to be called str5.
(d) Check whether str4 is palindrome or not.

Solution:

#include <stdio.h>
#include <ctype.h>

// Function to count characters in a string


int countCharacters(const char *str) {
int count = 0;
while (*str != '\0') {
count++;
str++;
}
return count;
}

// Function to count vowels and consonants in a string


void countVowelsConsonants(const char *str, int *vowels, int *consonants) {
*vowels = 0;
*consonants = 0;

while (*str != '\0') {


if (isalpha(*str)) {
switch (toupper(*str)) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
(*vowels)++;
break;
default:
(*consonants)++;
}
}
str++;
}
}

// Function to concatenate strings


void concatenateStrings(const char *str1, const char *str2, const char *str3, const char
*str4, char *result) {
while (*str1 != '\0') {
*result = *str1;
result++;
str1++;
}

while (*str2 != '\0') {


*result = *str2;
result++;
str2++;
}

while (*str3 != '\0') {


*result = *str3;
result++;
str3++;
}

while (*str4 != '\0') {


*result = *str4;
result++;
str4++;
}

*result = '\0';
}

// Function to check if a string is a palindrome


int isPalindrome(const char *str) {
int length = countCharacters(str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1]) {
return 0; // Not a palindrome
}
}
return 1; // Palindrome
}

int main() {
const char *Str1 = "CHANDIGARH";
const char *Str2 = "MADRAS";
const char *Str3 = "BOMBAY";
const char *Str4 = "AHMEDABAD";
char str5[100];

// Count characters
int countStr1 = countCharacters(Str1);
int countStr2 = countCharacters(Str2);
int countStr3 = countCharacters(Str3);
int countStr4 = countCharacters(Str4);

printf("Number of characters in Str1: %d\n", countStr1);


printf("Number of characters in Str2: %d\n", countStr2);
printf("Number of characters in Str3: %d\n", countStr3);
printf("Number of characters in Str4: %d\n", countStr4);

// Count vowels and consonants


int vowels, consonants;
countVowelsConsonants(Str1, &vowels, &consonants);
printf("Vowels in Str1: %d, Consonants in Str1: %d\n", vowels, consonants);

countVowelsConsonants(Str2, &vowels, &consonants);


printf("Vowels in Str2: %d, Consonants in Str2: %d\n", vowels, consonants);

countVowelsConsonants(Str3, &vowels, &consonants);


printf("Vowels in Str3: %d, Consonants in Str3: %d\n", vowels, consonants);

countVowelsConsonants(Str4, &vowels, &consonants);


printf("Vowels in Str4: %d, Consonants in Str4: %d\n", vowels, consonants);

// Concatenate strings
concatenateStrings(Str1, Str2, Str3, Str4, str5);
printf("Concatenated string (str5): %s\n", str5);

// Check if Str4 is a palindrome


if (isPalindrome(Str4)) {
printf("Str4 is a palindrome.\n");
} else {
printf("Str4 is not a palindrome.\n");
}

return 0;
}

7. Define a structure named census with the following three members:


● a character array city [] to store names
● a long integer to store population of the city
● a float member to store literacy level

WAP to do the following:

● to read details for 5 cities randomly using an array variable


● to sort the list based on literacy level
● to sort the list based on population
● to display sorted lists
Solution:

#include <stdio.h>

#include <string.h>

// Define the census structure

struct Census {

char city[50];

long int population;

float literacyLevel;

};

// Function to read details for 5 cities

void readCityDetails(struct Census cities[], int n) {

for (int i = 0; i < n; i++) {


printf("Enter details for City %d:\n", i + 1);

// Read city name

printf("City Name: ");

scanf("%s", cities[i].city);

// Read population

printf("Population: ");

scanf("%ld", &cities[i].population);

// Read literacy level

printf("Literacy Level: ");

scanf("%f", &cities[i].literacyLevel);

printf("\n");

// Function to sort the list based on literacy level

void sortListByLiteracy(struct Census cities[], int n) {

struct Census temp;

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (cities[j].literacyLevel > cities[j + 1].literacyLevel) {

// Swap the elements

temp = cities[j];

cities[j] = cities[j + 1];

cities[j + 1] = temp;

}
}

// Function to sort the list based on population

void sortListByPopulation(struct Census cities[], int n) {

struct Census temp;

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (cities[j].population > cities[j + 1].population) {

// Swap the elements

temp = cities[j];

cities[j] = cities[j + 1];

cities[j + 1] = temp;

// Function to display the sorted list

void displayList(struct Census cities[], int n) {

printf("\n%-20s%-15s%-20s\n", "City", "Population", "Literacy Level");

for (int i = 0; i < n; i++) {

printf("%-20s%-15ld%-20.2f\n", cities[i].city, cities[i].population, cities[i].literacyLevel);

int main() {

// Declare an array of census structures for 5 cities


struct Census cities[5];

// Read details for 5 cities

readCityDetails(cities, 5);

// Sort the list based on literacy level

sortListByLiteracy(cities, 5);

// Display the sorted list based on literacy level

printf("List sorted by literacy level:\n");

displayList(cities, 5);

// Sort the list based on population

sortListByPopulation(cities, 5);

// Display the sorted list based on population

printf("\nList sorted by population:\n");

displayList(cities, 5);

return 0;

You might also like