CTSD QUESTIONS
Question 1: create a 2D array and take user input for 2D array and print
sum of all the elements of array.
Question 2: what is an array?
Question 3: how to initialize strings?
Question 4: create a treasure hunt game using an if-else-if statement
and make it simple.
Question 5: write a program using all bitwise operator.
ANSWER 1
#include <stdio.h>
int main() {
int rows, cols, i, j, sum = 0;
// Get dimensions of the 2D array
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int array[rows][cols];
// Input elements for the 2D array
printf("Enter the elements row by row:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i, j);
scanf("%d", &array[i][j]);
sum += array[i][j];
// Print the 2D array
printf("\nThe 2D array is:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", array[i][j]);
printf("\n");
// Print the total sum
printf("\nThe sum of all elements in the array is: %d\n", sum);
return 0;
ANSWER 2
An array is a data structure that stores a collection of elements of the
same data type in contiguous memory locations. It is used to organize
data so that it can be accessed and manipulated efficiently using indices.
Key Features of Arrays:
1. Fixed Size: The size of an array is defined at the time of declaration
and cannot be changed dynamically (in most programming
languages like C).
2. Indexed Access: Array elements are accessed using an index,
starting from 0 (e.g., in int arr[5], arr[0] is the first element, arr[1] is
the second, and so on).
3. Homogeneous Data: All elements in an array must be of the same
data type, such as int, float, or char.
4. Contiguous Memory: The elements of an array are stored in
adjacent memory locations.
Types of Arrays:
1. One-Dimensional Array: A list of elements in a single row.
Copy code
int arr[5] = {1, 2, 3, 4, 5};
2. Two-Dimensional Array: A matrix-like structure (rows and
columns).
Copy code
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
3. Multi-Dimensional Arrays: Arrays with more than two dimensions
(e.g., 3D arrays).
Advantages of Arrays:
Easy to access elements using an index.
Efficient for operations like searching and sorting.
Useful for storing large amounts of data in structured formats (e.g.,
matrices).
Limitations of Arrays:
Fixed size (cannot dynamically grow or shrink in most programming
languages like C).
All elements must be of the same type.
Inserting or deleting elements can be inefficient, as shifting
elements may be required.
Real-Life Example:
Think of an array as a row of lockers, each labeled with a number. You
can store items (values) in specific lockers (indices) and retrieve them by
referring to their number.
ANSWER 3
In C, strings are arrays of characters terminated by a null character (\0).
There are several ways to initialize strings, depending on the specific use
case.
Methods to Initialize Strings
1. Using Double Quotes:
o The most common way to initialize a string is by using double
quotes. This automatically appends the null character \0 at
the end.
Copy code
char str[] = "Hello";
o The size of the array is automatically determined based on the
length of the string (including the \0).
2. Specifying Size with Double Quotes:
o You can also specify the size explicitly, but it must be large
enough to accommodate the string and the \0.
Copy code
char str[6] = "Hello"; // Includes space for '\0'
3. Using Character Arrays:
o A string can be initialized by explicitly specifying each
character, including the \0 at the end.
Copy code
char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
o Omitting \0 results in undefined behavior if you try to treat it
as a string.
4. Pointer to a String Literal:
o You can use a pointer to point to a string literal.
Copy code
char *str = "Hello";
o Note: String literals are stored in read-only memory. Modifying
a string literal through a pointer leads to undefined behavior.
5. Uninitialized Strings (Input Later):
o You can declare a string without initializing it and assign a
value later.
Copy code
char str[20]; // Reserve space for up to 19 characters + '\0'
scanf("%s", str); // Input the string (space-separated)
Examples:
1. Simple Initialization:
Copy code
char greeting[] = "Welcome";
printf("%s\n", greeting); // Output: Welcome
2. Explicit Character Initialization:
Copy code
char word[] = {'C', 'o', 'd', 'e', '\0'};
printf("%s\n", word); // Output: Code
3. Using a Pointer:
c
Copy code
char *message = "Hello, World!";
printf("%s\n", message); // Output: Hello, World!
4. Input and Store:
Copy code
char name[50];
printf("Enter your name: ");
scanf("%s", name); // Accepts input until a space or newline
printf("Hello, %s!\n", name);
Key Notes:
Strings must end with \0 to be treated as valid strings in C.
When using scanf for strings, it doesn't handle spaces. Use gets() or
fgets() for multi-word strings.
Copy code
char sentence[100];
fgets(sentence, sizeof(sentence), stdin); // Read a line of input
printf("%s", sentence);
ANSWER 4
Here’s a simple treasure hunt game implemented in C using if-else-if
statements. The player chooses a path, and the program provides
feedback based on the choice.
Copy code
#include <stdio.h>
int main() {
int choice;
printf("Welcome to the Treasure Hunt Game!\n");
printf("You are standing at the entrance of a mysterious forest.\n");
printf("Choose a path to explore:\n");
printf("1. Go left\n");
printf("2. Go right\n");
printf("3. Go straight\n");
printf("Enter your choice (1, 2, or 3): ");
scanf("%d", &choice);
if (choice == 1) {
printf("You chose to go left.\n");
printf("You find a river, but no treasure. Better luck next time!\n");
else if (choice == 2) {
printf("You chose to go right.\n");
printf("Congratulations! You found the treasure chest filled with gold!\
n");
else if (choice == 3) {
printf("You chose to go straight.\n");
printf("You encounter a sleeping dragon. Run away quickly!\n");
else {
printf("Invalid choice. Please restart the game and choose a valid
path.\n");
printf("Thank you for playing the Treasure Hunt Game!\n");
return 0;
}
Explanation:
1. Choices:
o The player can choose from 3 paths (1, 2, or 3).
o Each path has a unique outcome.
2. Invalid Input:
o If the player enters anything other than 1, 2, or 3, they are
prompted with an error message.
Sample Gameplay:
Example 1 (Player chooses 2):
css
Copy code
Welcome to the Treasure Hunt Game!
You are standing at the entrance of a mysterious forest.
Choose a path to explore:
1. Go left
2. Go right
3. Go straight
Enter your choice (1, 2, or 3): 2
You chose to go right.
Congratulations! You found the treasure chest filled with gold!
Thank you for playing the Treasure Hunt Game!
Example 2 (Invalid choice):
css
Copy code
Welcome to the Treasure Hunt Game!
You are standing at the entrance of a mysterious forest.
Choose a path to explore:
1. Go left
2. Go right
3. Go straight
Enter your choice (1, 2, or 3): 5
Invalid choice. Please restart the game and choose a valid path.
Thank you for playing the Treasure Hunt Game!
ANSWER 5
Here’s a C program demonstrating the use of all bitwise operators with
examples:
Copy code
#include <stdio.h>
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result;
printf("Bitwise Operations in C\n");
printf("a = %d, b = %d\n", a, b);
// Bitwise AND (&)
result = a & b; // 0101 & 0011 = 0001 (1 in decimal)
printf("a & b = %d\n", result);
// Bitwise OR (|)
result = a | b; // 0101 | 0011 = 0111 (7 in decimal)
printf("a | b = %d\n", result);
// Bitwise XOR (^)
result = a ^ b; // 0101 ^ 0011 = 0110 (6 in decimal)
printf("a ^ b = %d\n", result);
// Bitwise NOT (~)
result = ~a; // ~0101 = 1010 (Two's complement: -6 in decimal)
printf("~a = %d\n", result);
// Left Shift (<<)
result = a << 1; // 0101 << 1 = 1010 (10 in decimal)
printf("a << 1 = %d\n", result);
// Right Shift (>>)
result = a >> 1; // 0101 >> 1 = 0010 (2 in decimal)
printf("a >> 1 = %d\n", result);
return 0;
Explanation of Operators:
1. Bitwise AND (&):
o Performs a logical AND operation on each pair of
corresponding bits.
o Example: 5 & 3 = 1 (Binary: 0101 & 0011 = 0001)
2. Bitwise OR (|):
o Performs a logical OR operation on each pair of corresponding
bits.
o Example: 5 | 3 = 7 (Binary: 0101 | 0011 = 0111)
3. Bitwise XOR (^):
o Performs a logical XOR operation (true if the bits are different).
o Example: 5 ^ 3 = 6 (Binary: 0101 ^ 0011 = 0110)
4. Bitwise NOT (~):
o Inverts all bits of the number (one's complement).
o Example: ~5 = -6 (Binary: ~0101 = 1010 in two's
complement)
5. Left Shift (<<):
o Shifts all bits to the left by a specified number of positions,
filling with 0.
o Example: 5 << 1 = 10 (Binary: 0101 << 1 = 1010)
6. Right Shift (>>):
o Shifts all bits to the right by a specified number of positions,
filling with 0 for unsigned numbers.
o Example: 5 >> 1 = 2 (Binary: 0101 >> 1 = 0010)
Output:
css
Copy code
Bitwise Operations in C
a = 5, b = 3
a&b=1
a|b=7
a^b=6
~a = -6
a << 1 = 10
a >> 1 = 2
4o
THE MORE SHORTEN VERSION OF THE QUESTIONS FOLLOWED BY THEIR
RESPECTIVE ANSWERS AND DOWN BELOW
Shortened Answers
ANSWER 1
Copy code
#include <stdio.h>
int main() {
int rows, cols, i, j, sum = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int array[rows][cols];
printf("Enter the elements row by row:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("Enter element at (%d, %d): ", i, j);
scanf("%d", &array[i][j]);
sum += array[i][j];
printf("\nThe 2D array is:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", array[i][j]);
printf("\n");
printf("\nSum of elements: %d\n", sum);
return 0;
ANSWER 2
An array is a collection of elements of the same type stored in contiguous
memory locations.
Key Points:
Fixed Size: Declared size cannot change.
Indexed Access: Elements accessed by index, starting at 0.
Homogeneous Data: All elements are of the same type.
Types of Arrays:
1. 1D Array: A list of elements.
Copy code
int arr[5] = {1, 2, 3, 4, 5};
2. 2D Array: Matrix of rows and columns.
Copy code
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
3. Multi-Dimensional: Arrays with 3 or more dimensions.
Advantages:
Easy access using indices.
Efficient for search and sort.
Limitations:
Fixed size and data type.
Insertion/deletion can be slow.
Think of an array as numbered lockers storing similar items.
ANSWER 3
Strings in C are character arrays ending with a \0 (null character).
Methods to Initialize Strings:
1. Using Double Quotes: Automatically adds \0.
Copy code
char str[] = "Hello";
2. With Size Specified: Includes space for \0.
Copy code
char str[6] = "Hello";
3. Character Array: Explicitly add \0.
Copy code
char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
4. Pointer to String Literal:
Copy code
char *str = "Hello";
5. Uninitialized: Reserve space and assign later.
Copy code
char str[20];
scanf("%s", str);
Example:
Copy code
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
ANSWER 4
Copy code
#include <stdio.h>
int main() {
int choice;
printf("Welcome to the Treasure Hunt Game!\n");
printf("Choose a path:\n");
printf("1. Go left\n2. Go right\n3. Go straight\n");
printf("Enter your choice (1, 2, or 3): ");
scanf("%d", &choice);
if (choice == 1) {
printf("You find a river, but no treasure. Better luck next time!\n");
else if (choice == 2) {
printf("Congratulations! You found the treasure chest filled with gold!\
n");
else if (choice == 3) {
printf("You encounter a sleeping dragon. Run away quickly!\n");
else {
printf("Invalid choice. Restart the game and choose a valid path.\n");
printf("Thank you for playing!\n");
return 0;
ANSWER 5
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 3, result;
printf("Bitwise Operations in C\n");
printf("a = %d, b = %d\n", a, b);
result = a & b;
printf("a & b = %d\n", result);
result = a | b;
printf("a | b = %d\n", result);
result = a ^ b;
printf("a ^ b = %d\n", result);
result = ~a;
printf("~a = %d\n", result);
result = a << 1;
printf("a << 1 = %d\n", result);
result = a >> 1;
printf("a >> 1 = %d\n", result);
return 0;
Explanation:
1. AND (&): Logical AND on corresponding bits.
2. OR (|): Logical OR on corresponding bits.
3. XOR (^): True if bits differ.
4. NOT (~): Inverts all bits.
5. Left Shift (<<): Shifts bits left, filling with 0.
6. Right Shift (>>): Shifts bits right, filling with 0.
Output:
css
Copy code
Bitwise Operations in C
a = 5, b = 3
a&b=1
a|b=7
a^b=6
~a = -6
a << 1 = 10
a >> 1 = 2