0% found this document useful (0 votes)
13 views5 pages

Cat 2

The document outlines various programming tasks related to C language, including defining variables of different data types, explaining standard and user-defined functions, and differentiating between singly and doubly linked lists. It also includes C programs for generating Fibonacci series, calculating averages, defining structures, swapping integers using pointers, and dynamically allocating memory for arrays. Each task is accompanied by code examples and explanations to illustrate the concepts.

Uploaded by

wanjikudavid403
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)
13 views5 pages

Cat 2

The document outlines various programming tasks related to C language, including defining variables of different data types, explaining standard and user-defined functions, and differentiating between singly and doubly linked lists. It also includes C programs for generating Fibonacci series, calculating averages, defining structures, swapping integers using pointers, and dynamically allocating memory for arrays. Each task is accompanied by code examples and explanations to illustrate the concepts.

Uploaded by

wanjikudavid403
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/ 5

ACMP 122: STRUCTURED PROGRAMMING

DAVID GAKUO WANJIKU

S17/03139/2023

CAT 2 ACMP 122: STRUCTURED PROGRAMMING

Each question carries equal weight (5 Marks)


1) Define a variable of each of the following data types in C: integer, floating-point,
character, and string. Initialize each variable with an appropriate value.
#include <stdio.h>
int main() {
int integerVar = 10;
float floatVar = 3.14;
char charVar = 'A';
char stringVar[] = "Hello, world!";
printf("Integer variable: %d\n", integerVar);
printf("Floating-point variable: %.2f\n", floatVar);
printf("Character variable: %c\n", charVar);
printf("String variable: %s\n", stringVar);

return 0;
}
integerVar is an integer variable initialized with the value 10.
floatVar is a floating-point variable initialized with the value 3.14.
charVar is a character variable initialized with the character 'A'.
stringVar is a string variable (an array of characters) initialized with the string "Hello,
world!".
We then use printf statements to print out the values of each variable.
2) Explain the differences between the following two types of functions in C
programming:
a) Standard library functions
b) User-defined functions.
standard library functions are pre-defined functions provided by the C standard library for
common tasks, while user-defined functions are custom functions created by the programmer to
perform specific tasks within their programs, promoting modularity, reusability, and abstraction.
ACMP 122: STRUCTURED PROGRAMMING

3) Differentiate between singly and doubly linked lists?


1. In a singly linked list, each node contains data and a pointer/reference to the next node in
the sequence while In a doubly linked list, each node contains data and pointers/references
to both the next node and the previous node in the sequence.

2.Singly is unidirectional, meaning you can traverse the list only in one direction, typically
from the head (start) to the tail (end) while doubly linked list is bidirectional, meaning you
can traverse the list in both forward and backward directions.

3. Singly linked lists are memory-efficient as they only require one pointer per node while
doubly linked lists is Slightly less memory-efficient
4) Write a C program to print the Fibonacci series up to n terms, where n is provided by the
user. The Fibonacci series is a sequence of numbers where each number is the sum of the
two preceding ones, starting with 0 and 1.
#include <stdio.h>
void printFibonacci(int n) {
int first = 0, second = 1, next;
printf("Fibonacci Series up to %d terms:\n", n);
printf("%d, %d, ", first, second);
for (int i = 2; i < n; i++) {
next = first + second;
printf("%d, ", next);
first = second;
second = next;
}
printf("\n");
}

int main() {
int n;
printf("Enter the number of terms in the Fibonacci series: ");
scanf("%d", &n);
printFibonacci(n);

return 0;
}
ACMP 122: STRUCTURED PROGRAMMING

5) Implement a function in C called calculateAverage that takes an array of integers and its
size as input and returns the average of the numbers in the array. Test the function with a
sample array.
#include <stdio.h>
double calculateAverage(int arr[], int size) {
double sum = 0.0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum / size;
}

int main() {
int sampleArray[] = {10, 20, 30, 40, 50};
int size = sizeof(sampleArray) / sizeof(sampleArray[0]);
double average = calculateAverage(sampleArray, size);
printf("Average of the sample array: %.2lf\n", average);

return 0;
}
6) Define a structure named Student with members name, roll_number, and marks (an array
of five integers). Write a C program that creates an array of five Student structures,
initializes their values, and prints them.
#include <stdio.h>
struct Student {
char name[50];
int roll_number;
int marks[5];
};

int main() {
struct Student students[5];
for (int i = 0; i < 5; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll Number: ");
scanf("%d", &students[i].roll_number);
printf("Enter marks for 5 subjects:\n");
ACMP 122: STRUCTURED PROGRAMMING

for (int j = 0; j < 5; j++) {


printf("Subject %d: ", j + 1);
scanf("%d", &students[i].marks[j]);
}
printf("\n");
}
printf("Details of students:\n");
for (int i = 0; i < 5; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].roll_number);
printf("Marks:\n");
for (int j = 0; j < 5; j++) {
printf("Subject %d: %d\n", j + 1, students[i].marks[j]);
}
printf("\n");
}

return 0;
}
7) Write a C program that swaps the values of two integers using pointers. Prompt the user to
enter two integers, perform the swap using pointers, and print the swapped values.
#include <stdio.h>
void swapIntegers(int *a, int *b);
int main() {
int num1, num2;
printf("Enter the first integer: ");
scanf("%d", &num1);
printf("Enter the second integer: ");
scanf("%d", &num2);
printf("Original values: num1 = %d, num2 = %d\n", num1, num2);
swapIntegers(&num1, &num2);
printf("Swapped values: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}
ACMP 122: STRUCTURED PROGRAMMING

8) Write a C program that dynamically allocates memory for an array of integers of size n (n
is provided by the user). Prompt the user to enter n integers to populate the array, then
print the sum of the elements in the array.

#include <stdio.h>
int main() {
int n, *arr, sum = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);

arr = (int *)malloc(n * sizeof(int));

if (arr == NULL) {
printf("Memory allocation failed. Exiting...");
return 1; // Exiting with error code 1
}
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum of the elements: %d\n", sum);
free(arr);

return 0;
}

You might also like