0% found this document useful (0 votes)
29 views8 pages

Exam

Uploaded by

sz4sk5xccv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

Exam

Uploaded by

sz4sk5xccv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Task 1

Write program to Calculate Difference Between Two Weights using Structures


(Check the program with the following values-> 10kg 100 gr and 5 kg 1800 gr.
Should be 3kg 300gr)

struct weight {
int grams;
int kilograms;
};

#include <stdio.h>

struct weight {
int grams;
int kilograms;
};

struct weight calculateDifference(struct weight w1, struct weight w2) {


struct weight result;

// Convert both weights to total grams


int totalGrams1 = w1.kilograms * 1000 + w1.grams;
int totalGrams2 = w2.kilograms * 1000 + w2.grams;

// Calculate the difference in total grams


int differenceInGrams = totalGrams1 - totalGrams2;

// Convert the difference back to kilograms and grams


result.kilograms = differenceInGrams / 1000;
result.grams = differenceInGrams % 1000;

return result;
}

int main() {
// Define two weights
struct weight weight1 = {100, 10}; // 10 kg 100 g
struct weight weight2 = {1800, 5}; // 5 kg 1800 g

// Calculate the difference


struct weight difference = calculateDifference(weight1, weight2);

// Display the result


printf("The difference is: %d kg %d g\n", difference.kilograms,
difference.grams);

return 0;
}

Task 2
Using #define preprocessor write Macros and use it
circle_length

#include <stdio.h>

#include <math.h>

// Macro definition for calculating circle circumference

#define CIRCLE_LENGTH(radius) (2 * M_PI * (radius))

int main() {

// Define the radius of the circle

double radius = 5.0;

// Use the macro to calculate the circle circumference

double circumference = CIRCLE_LENGTH(radius);

// Display the result

printf("The circumference of the circle with radius %.2f is: %.2f\n",


radius, circumference);

return 0;

Task 3

Using dynamic memory allocation create array of Students structure (size of


array is 5)

Students {
char [10] first_name;
char [15] last_name;
int age;
int midtermExamGrade;
int finalExamGrade
}
And sort it according age in descending order. Then output data.

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

// Define the Students structure


struct Student {
char first_name[10];
char last_name[15];
int age;
int midtermExamGrade;
int finalExamGrade;
};

// Compare function for qsort to sort by age in descending order


int compareByAgeDesc(const void *a, const void *b) {
return ((struct Student*)b)->age - ((struct Student*)a)->age;
}

int main() {
// Dynamically allocate memory for an array of 5 Students
struct Student *students = (struct Student*)malloc(5 * sizeof(struct
Student));

// Fill the array with sample data


strcpy(students[0].first_name, "John");
strcpy(students[0].last_name, "Doe");
students[0].age = 22;
students[0].midtermExamGrade = 85;
students[0].finalExamGrade = 90;

// Repeat similar steps for the remaining students (1-4)

// Sort the array based on age in descending order


qsort(students, 5, sizeof(struct Student), compareByAgeDesc);

// Output the sorted data


printf("Sorted Students by Age (Descending Order):\n");
for (int i = 0; i < 5; ++i) {
printf("Name: %s %s, Age: %d, Midterm Grade: %d, Final Grade: %d\n",
students[i].first_name, students[i].last_name,
students[i].age, students[i].midtermExamGrade,
students[i].finalExamGrade);
}

// Free the dynamically allocated memory


free(students);

return 0;
}
Task 4
The algorithm for calculating the value of the function F (n), where n is a
natural number, is:
F(n<=1) = 1
F(n) = F(n–1) * (n + 4), n > 1

Write two functions – recursive and iterative (while loop)

recursive
#include <stdio.h>

int recursiveF(int n) {
if (n <= 1) {
return 1;
} else {
return recursiveF(n - 1) * (n + 4);
}
}

int main() {
int n = 5; // You can change the value of n as needed
int result = recursiveF(n);

printf("Recursive: F(%d) = %d\n", n, result);

return 0;
}

inerative

#include <stdio.h>

int iterativeF(int n) {
int result = 1;

while (n > 1) {
result *= (n + 4);
n--;
}

return result;
}

int main() {
int n = 5; // You can change the value of n as needed
int result = iterativeF(n);

printf("Iterative: F(%d) = %d\n", n, result);

return 0;
}
Task 5
You have 2D array [5][4]. Please write program for finding column’s number
with minimal sum of elements

#include <stdio.h>

int main() {
// Define a 2D array [5][4]
int array[5][4] = {
{3, 7, 1, 5},
{2, 8, 4, 6},
{9, 2, 8, 3},
{5, 1, 7, 4},
{6, 3, 2, 9}
};

// Initialize variables to store the column with the minimal sum


int minColumn = 0;
int minSum = 0;

// Calculate the sum of each column and find the column with the minimal
sum
for (int j = 0; j < 4; ++j) {
int columnSum = 0;
for (int i = 0; i < 5; ++i) {
columnSum += array[i][j];
}

// Update minColumn if the current column has a smaller sum


if (j == 0 || columnSum < minSum) {
minSum = columnSum;
minColumn = j;
}
}

// Output the result


printf("Column with the minimal sum is column %d.\n", minColumn + 1);

return 0;
}

Task 6
User enters the x and n= 1..3, which is number of action
Using switch construction, calculate and output the answer
1) -2x2-4; 2) 5x+2; 3) 15-3x.

#include <stdio.h>

int main() {
// Input values from the user
double x;
int n;

printf("Enter the value of x: ");


scanf("%lf", &x);

printf("Enter the number of action (1, 2, or 3): ");


scanf("%d", &n);

// Perform calculations based on the value of n using a switch statement


switch (n) {
case 1:
printf("Result for action 1: %.2lf\n", -2 * x * x - 4);
break;
case 2:
printf("Result for action 2: %.2lf\n", 5 * x + 2);
break;
case 3:
printf("Result for action 3: %.2lf\n", 15 - 3 * x);
break;
default:
printf("Invalid action number. Please enter 1, 2, or 3.\n");
break;
}

return 0;
}

Task 7
Find the maximum element with an even index in the array.

#include <stdio.h>

int main() {
// Define an array
int array[] = {10, 5, 20, 8, 15, 7, 25, 12};

// Calculate the length of the array


int length = sizeof(array) / sizeof(array[0]);

// Initialize variables to store the maximum element and its index


int maxElement = array[0];
int maxIndex = 0;

// Find the maximum element with an even index


for (int i = 2; i < length; i += 2) {
if (array[i] > maxElement) {
maxElement = array[i];
maxIndex = i;
}
}

// Output the result


printf("Maximum element with an even index is %d at index %d.\n",
maxElement, maxIndex);
return 0;
}

Task 8
Create Function using reference and use it inside of main
void circle_length (int *a)

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

// Function to calculate the circumference of a circle


void circle_length(int *radius) {
// Assuming radius is passed by reference, update the value
// Calculate the circumference (2 * pi * radius)
double circumference = 2 * M_PI * (*radius);

// Update the radius with the calculated circumference


*radius = (int)circumference; // Convert to int for simplicity
}

int main() {
// Initialize the radius
int radius = 5;

// Call the function with the address of the radius variable


circle_length(&radius);

// Output the updated radius (now holds the calculated circumference)


printf("The updated radius is: %d\n", radius);

return 0;
}

Task 9
Write a program to write an 1D array to a file

#include <stdio.h>

int main() {
// Define an array
int array[] = {10, 20, 30, 40, 50};

// Calculate the length of the array


int length = sizeof(array) / sizeof(array[0]);

// Open a file for writing


FILE *file = fopen("output.txt", "w");

// Check if the file was opened successfully


if (file == NULL) {
fprintf(stderr, "Error opening the file.\n");
return 1;
}

// Write the array to the file


for (int i = 0; i < length; ++i) {
fprintf(file, "%d ", array[i]);
}

// Close the file


fclose(file);

printf("Array written to the file successfully.\n");

return 0;
}

You might also like