CP CT 2 Important Questions Answers
CP CT 2 Important Questions Answers
C Programming (FEC205)
Important questions for Class Test 2
1. Explain compile time and run time initialization of an array with proper
example.
#include <stdio.h>
int main() {
// Compile-time initialization of an array
int numbers[] = {1, 2, 3, 4, 5};
return 0;
}
In this example:
The array numbers is initialized at compile time with the values {1, 2, 3, 4, 5}.
The compiler knows the size and values of the array at compile time, so it can
allocate memory accordingly.
Run Time Initialization:
Run-time initialization refers to initializing the array elements with values obtained
during program execution. The values are not known to the compiler during
compilation and are determined dynamically during program execution.
#include <stdio.h>
int main() {
int n;
return 0;
}
In this example:
The size of the array numbers is determined at run time based on the user input.
The array elements are initialized with values entered by the user during program
execution.
Since the size and values of the array are determined dynamically at run time, it's
called run-time initialization.
2. Write a C program that uses functions to perform Multiplication of Two
Matrices.
#include <stdio.h>
int main() {
int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE],
result[MAX_SIZE][MAX_SIZE];
int rows1, cols1, rows2, cols2;
// Multiply matrices
matrix_multiply(mat1, mat2, result, rows1, cols1, rows2, cols2);
return 0;
}
3. Explain following functions with proper example.
a. strcmp
b. strrev
c. strstr
d. strcat
e. strcpy
a. strcmp (String Compare):
The strcmp function compares two strings lexicographically (i.e., based on their alphabetical order). It
returns an integer value based on the comparison result:
int main() {
char str1[] = "apple";
char str2[] = "banana";
if (result == 0) {
printf("Both strings are equal.\n");
} else if (result < 0) {
printf("%s comes before %s in lexicographical order.\n", str1, str2);
} else {
printf("%s comes after %s in lexicographical order.\n", str1, str2);
}
return 0;
}
Output:
apple comes before banana in lexicographical order.
#include <stdio.h>
#include <string.h>
int main()
{
char str[50] = "geeksforgeeks";
return 0;
}
Output:
c. strstr()
The strstr function in C is used to find the first occurrence of a substring within a larger string. It stands for
"string find". The function takes two arguments: the string in which to search and the substring to search
for. It returns a pointer to the first occurrence of the substring within the string, or NULL if the substring is
not found.
Syntax
char *strstr (const char *s1, const char *s2);
Parameters
s1: This is the main string to be examined.
s2: This is the sub-string to be searched in string.
#include <stdio.h>
#include <string.h>
int main()
{
// Take any two strings
char s1[] = "GeeksforGeeks";
char s2[] = "for";
char* p;
return 0;
}
Output
String found
First occurrence of string 'for' in 'GeeksforGeeks' is 'forGeeks'
d. strcat
The strcat function in C is used to concatenate (i.e., append) one string to the end of another string. It stands
for "string concatenation". The function takes two arguments: the destination string (the string to which the
other string will be appended) and the source string (the string that will be appended to the destination
string). The destination string must have enough space to accommodate the concatenated string.
#include <stdio.h>
#include <string.h>
int main() {
char dest[20] = "Hello, ";
const char src[] = "world!";
return 0;
}
Output:
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, world!";
char destination[20]; // Make sure destination has enough space to hold the source string
return 0;
}
Output:
Copied string: Hello, world!
4. Define what are arrays? Explain how we declare and initialize 1D and 2D arrays
with simple example.
Arrays in C are a collection of elements of the same data type that are stored in
contiguous memory locations. They provide a convenient way to store and
manipulate multiple values of the same type under a single name.
1D arrays
In C, you can declare and initialize a 1D array using the following syntax:
data_type array_name[array_size] = {value1, value2, ..., valueN};
Here's a breakdown of each part:
• data_type: Specifies the data type of the elements in the array.
• array_name: Specifies the name of the array.
• array_size: Specifies the number of elements in the array.
• {value1, value2, ..., valueN}: Represents the initial values of the elements in the
array.
Here's a simple example demonstrating how to declare and initialize a 1D array of
integers:
#include <stdio.h>
int main() {
// Declare and initialize a 1D array of integers
int numbers[5] = {1, 2, 3, 4, 5};
return 0;
}
Output:
Elements of the array: 1 2 3 4 5
2D arrays
Here's how you declare and initialize a 2D array in C:
data_type array_name[row_size][column_size] = {
{val_11, val_12, ..., val_1N},
{val_21, val_22, ..., val_2N},
...
{val_M1, val_M2, ..., val_MN}
};
• data_type: Specifies the data type of the elements in the array.
• array_name: Specifies the name of the array.
• row_size and column_size: Specify the number of rows and columns in the
array, respectively.
• val_ij: Represents the value of the element at the ith row and jth column.
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
return 0;
}
5. Why we need structures. Define Structure and explain the syntax of declaration of
Structure with example.
1. Organizing Data: Structures allow you to organize related data items into a
single unit, making it easier to manage and manipulate complex data.
4. Passing Complex Data: Structures enable you to pass complex data structures
as arguments to functions, making it easier to work with large datasets.
#include <stdio.h>
int main() {
// Declare a variable of type Point
struct Point p1;
return 0;
}
int main() {
// Declare an array of structures to store information of 10 students
struct Student students[10];
return 0;
}
7. Write a C program to find both the largest and smallest number in a list of integers.
#include <stdio.h>
int main() {
int num;
int largest, smallest;
// Initialize largest and smallest with the first element of the array
largest = smallest = arr[0];
return 0;
}
Output
Enter 7 integers:
10 23 54 67 54 23 32
The largest number is: 67
The smallest number is: 10
#include <stdio.h>
int main() {
int num;
float sum = 0.0, average;
return 0;
}
Output
Enter the number of elements: 5
Enter 5 elements:
10
12
32
43
13
The average of the elements is: 22.00
9. Write a program to check whether the entered string is palindrome or not.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, j;
int isPalindrome = 1; // Assume initially that the string is a palindrome
return 0;
}
Output :
Enter a string (in lowercase): nitin
The entered string is a palindrome.