Programming skill
Journal-1
Q1 Write a program in C to declare a 3x3 numeric array, initialize it
with values, and display its elements.
AN #include <stdio.h>
S
void main() {
int arr1[3][3], i, j;
printf("\n\nRead a 2D array of size 3x3 and print the matrix :\
n");
printf(" \n");
printf("Input elements in the matrix :\n"); // Semicolon added
here
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &arr1[i][j]);
}
}
printf("\nThe matrix is : \n");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++)
printf("%d\t", arr1[i][j]);
}
printf("\n\n");
}
****OUTPUT****
The matrix is :
2 4 6
8 6 3
6 7 3
Q2 Write a menu-driven program in C to perform operations on 3x3
247:NAVED PATHAN 1
Programming skill
Journal-1
numeric arrays, including addition, subtraction, multiplication, and
finding the transpose.
AN #include <stdio.h>
S void add(int arr1[3][3], int arr2[3][3])
{ int result[3][3]; for (int i = 0; i < 3;
i++) { for (int j = 0; j < 3; j++)
{ result[i][j] = arr1[i][j] + arr2[i]
[j]; } }
printf("Sum of the matrices:\n");
for (int i = 0; i < 3; i++) { for (int
j = 0; j < 3; j++) { printf("%d
", result[i][j]);
}
printf("\n");
}
void subtract(int arr1[3][3], int
arr2[3][3]) { int result[3][3]; for (int i
= 0; i < 3; i++) { for (int j = 0; j < 3;
j++) { result[i][j] = arr1[i][j] - arr2[i]
[j]; }
}
printf("Difference of the
matrices:\n"); for (int i = 0; i < 3;
i++){ for (int j = 0; j < 3; j++)
printf("%d ", result[i][j]);
}
printf("\n");
}
void multiply(int arr1[3][3], int arr2[3][3])
{ int result[3][3] = {0}; for (int i = 0; i < 3;
i++) { for (int j = 0; j < 3; j++) { for (int k =
0; k < 3; k++) { result[i][j] += arr1[i][k] *
arr2[k][j];
}
}
} printf("Product of the matrices:\n"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++)
{ printf("%d ", result[i][j]);
} printf("\n");
}}
void transpose(int arr[3][3]) { int result[3][3]; for (int i = 0; i < 3;
i++) { for (int j = 0; j < 3; j++) { result[i][j] = arr[j][i];
}
} printf("Transpose of the matrix:\n"); for (int i = 0; i < 3; i++) { for
(int j = 0; j < 3; j++) { printf("%d
", result[i][j]);
} printf("\n");
247:NAVED PATHAN 2
Programming skill
Journal-1
}}int main() {
int arr1[3][3], arr2[3][3], choice;
printf("Enter the elements of the first 3x3 matrix:\n");
for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { scanf("%d",
&arr1[i][j]);
}
}
printf("Enter the elements of the second 3x3 matrix:\n");
for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { scanf("%d",
&arr2[i][j]);
}
}
do{
printf("\nMenu:\n"); printf("1. Add matrices\n");
printf("2. Subtract matrices\n"); printf("3. Multiply matrices\n");
printf("4. Transpose matrices\n"); printf("5. Exit\n");
printf("Enter your choice: "); scanf("%d", &choice);
switch (choice) {case 1: add(arr1, arr2); break; case 2: subtract(arr1, arr2); break;
case 3: multiply(arr1, arr2); break; case 4: printf("Transpose of the first
matrix:\n"); transpose(arr1);
printf("Transpose of the second matrix:\n"); transpose(arr2);
break;
case 5: printf("Exiting...\n"); break; default: printf("Invalid choice!\n");
}
} while (choice = 5); return 0;
}
/*
*****OUTPUT*****
Enter the elements of the first 3x3 matrix:
4 5 6
7 8 9
1 2 3
Enter the elements of the second 3x3 matrix:
7 4 1
8 5 2
9 6 3
Menu:
1. Add matrices
2. Subtract matrices
3. Multiply matrices
4. Transpose matrices
247:NAVED PATHAN 3
Programming skill
Journal-1
5. Exit
Enter your choice: 1
Sum of the matrices:
11 9 7
15 13 11
10 8 6
247:NAVED PATHAN 4
Programming skill
Journal-1
Q3 Write a program in C to compute and display the memory address
of elements in a 3x3 numeric array using row-major and column-
major formulas.
AN #include <stdio.h> int main() { int arr[3][3]
S = {{1, 5, 7}, {8, 2, 9}, {3, 4, 9}}; int
base_address = (int)&arr[0][0]; int
element_size = sizeof(int);
printf("Memory Addresses in Row-Major Order:\n");
for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int
address = base_address + (i * 3 + j) * element_size;
printf("Element [%d][%d]: %d\n", i, j, address);
}
}
printf("\nMemory Addresses in Column-Major Order:\
n"); for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { int
address = base_address + (j * 3 + i) * element_size;
printf("Element [%d][%d]: %d\n", i, j, address);
}
}
return 0;
}
/*
*****OUTPUT*****
Memory Addresses in Row-Major Order:
Element [0][0]: -1863031376
Element [0][1]: -1863031372
Element [0][2]: -1863031368
Element [1][0]: -1863031364
Element [1][1]: -1863031360
Element [1][2]: -1863031356
Element [2][0]: -1863031352
Element [2][1]: -1863031348
Element [2][2]: -1863031344
247:NAVED PATHAN 5
Programming skill
Journal-1
Q4 Write a Menu-Driven Program for Operations on 2D Character
Arrays
• Input and store a list of strings in a 2D character array, and
display the stored strings.
• Search for a specific string in a 2D character array and
display its position if found.
• Copy a 2D character array into another and merge two 2D
character arrays into one.
• Calculate and display the length of each string stored in a 2D
character array.
Exit the program.
AN #include <stdio.h>
S #include <string.h>
#define MAX 5
#define LENGTH 100
void displayStrings(char arr[MAX]
[LENGTH]) { printf("Stored Strings:\
n"); for (int i = 0; i < MAX; i++)
{ printf("%s\n", arr[i]);
}
}
void searchString(char arr[MAX][LENGTH], char query[LENGTH]) {
for (int i = 0; i < MAX; i++) { if (strcmp(arr[i], query) == 0)
{ printf("String found at position %d\n", i); return; }
}
printf("String not found.\n");
}
int main() { char
strings[MAX][LENGTH];
char query[LENGTH];
int choice;
printf("Enter %d strings:\n",
MAX); for (int i = 0; i < MAX;
i++) { scanf("%s", strings[i]);
}
printf("\nMenu:\n");
printf("1. Display
Strings\n");
printf("2. Search for a String\n");
printf("3. Exit\n");
do { printf("Enter your choice: ");
247:NAVED PATHAN 6
Programming skill
Journal-1
scanf("%d", &choice);
switch (choice) { case 1:
displayStrings(strings); break;
case 2: printf("Enter the string to search: "); scanf("%s", query);
searchString(strings, query); break;
case 3: printf("Exiting program.\n"); break;
default: printf("Invalid choice. Try again.\n");
}
} while (choice != 3); return 0;
}
****OUTPUT*****
Enter 5 strings:
String 1: LION
String 2: TIGER
String 3: FOX
String 4: WOLF String 5: OX
Menu:
1. Display Strings
2. Search for a String 3. Exit
Enter your choice: 2
Enter the string to search: WOLF
String found at position 4
/*
247:NAVED PATHAN 7
Programming skill
Journal-1
Q5 Write a program to display name of students starting with 'R' from
given list of 10 students.
AN #include <stdio.h>
S #include <string.h>
int main() {
char students[10][40];
printf("Enter names of 10 students:\n");
for (int i = 0; i < 10; i++) {
printf("Student %d: ", i + 1);
fgets(students[i], 40, stdin);
students[i][strcspn(students[i], "\n")] = '\0';
}
printf("\nStudents whose names start with 'R':\n");
for (int i = 0; i < 10; i++) {
if (students[i][0] == 'R' || students[i][0] == 'r') {
printf("%s\n", students[i]);
}
}
return 0;
}
/*
*****OUTPUT*****
Enter names of 10 students:
Student 1: KRISHNA
Student 2: NAKUL
Student 3: ROHIT
Student 4: RAHUL
Student 5: JAYA
Student 6: ARHAAN
Student 7: OM
Student 8: ROHAN
Student 9: KRISH
Student 10: SHREYA
Students whose names start with 'R':
RAHUL
ROHAN
/*
247:NAVED PATHAN 8
Programming skill
Journal-1
Q6 Write a program in C to define a structure Employee containing the
AN #include <stdio.h>
S
struct Employee {
int emp_id;
char full_name[40];
float monthly_income;
};
int main() {
struct Employee employees[5];
printf("Enter details of 5 employees:\n");
for (int i = 0; i < 5; i++) {
printf("\nEmployee %d:\n", i + 1);
printf("ID: ");
scanf("%d", &employees[i].emp_id);
printf("Full Name: ");
scanf(" %[^\n]%*c", employees[i].full_name);
printf("Monthly Income: ");
scanf("%f", &employees[i].monthly_income);
}
printf("\nEmployees earning more than ₹20,000:\n");
for (int i = 0; i < 5; i++) {
if (employees[i].monthly_income > 20000) {
printf("ID: %d, Name: %s, Income: ₹%.2f\n", employees[i].emp_id,
employees[i].full_name, employees[i].monthly_income);
}
}
return 0;
}
*****OUTPUT*****
Employees earning more than ₹20,000:
ID: 1, Name: RAJ SINGH, Income: ₹25000.00
ID: 2, Name: RAJBEER LABANA, Income: ₹50000.00
Q7 Write a program in C to define a union Customer with the following
fields:
247:NAVED PATHAN 9
Programming skill
Journal-1
AN #include <stdio.h>
S #include <string.h>
union Customer {
int customer_id;
char customer_name[50];
float account_balance;
};
int main() {
union Customer c;
printf("Enter customer details:\n");
printf("Enter Customer ID: ");
scanf("%d", &c.customer_id);
printf("Customer ID: %d\n", c.customer_id);
printf("\nEnter Customer Name: ");
scanf(" %[^\n]%*c", c.customer_name);
printf("Customer Name: %s\n", c.customer_name);
printf("\nEnter Account Balance: ");
scanf("%f", &c.account_balance);
printf("Account Balance: ₹%.2f\n", c.account_balance);
printf("\nNote: Union shares memory, so only the last value remains accessible.\n");
printf("Customer Name after updating balance: %s (Corrupted Data)\n",
c.customer_name);
return 0;
}
/*
*****OUTPUT*****
Enter customer details:
Enter Customer ID: RAKESH PATEL
Customer ID: 0
Enter Customer Name: Customer Name: RAKESH PATEL
Enter Account Balance: 140000
Account Balance: ₹140000.00
Note: Union shares memory, so only the last value remains accessible.
Customer Name after updating balance: (Corrupted Data)
247:NAVED PATHAN 10
Programming skill
Journal-1
247:NAVED PATHAN 11