KEMBAR78
Tushar 261 3d Array Class | PDF | String (Computer Science) | Matrix (Mathematics)
0% found this document useful (0 votes)
9 views13 pages

Tushar 261 3d Array Class

The document contains multiple C programming examples, including matrix multiplication, array insertion, deletion, traversal, string length calculation, string reversal, and a simple sum function. Each program is structured with input prompts, processing logic, and output displays. The code snippets demonstrate fundamental programming concepts and operations on arrays and strings.

Uploaded by

thepsyforge
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)
9 views13 pages

Tushar 261 3d Array Class

The document contains multiple C programming examples, including matrix multiplication, array insertion, deletion, traversal, string length calculation, string reversal, and a simple sum function. Each program is structured with input prompts, processing logic, and output displays. The code snippets demonstrate fundamental programming concepts and operations on arrays and strings.

Uploaded by

thepsyforge
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/ 13

Array 1 week class

// 2300290120261
// Tushar Kumar
//SEC d

Program no 1 Matrix multiplication

#include <stdio.h>
#define MAX 10
int main() {
int mat1[MAX][MAX], mat2[MAX][MAX], result[MAX]
[MAX];
int r1, c1, r2, c2;

printf("Enter rows and columns for the first matrix:


");
scanf("%d %d", &r1, &c1);

printf("Enter elements of the first matrix (%d x %d):\


n", r1, c1);
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
scanf("%d", &mat1[i][j]);
}
}

printf("Enter rows and columns for the second


matrix: ");
scanf("%d %d", &r2, &c2);

if (c1!=r2) {
printf("Matrix multiplication is not possible.\n");
return 1;
}

printf("Enter elements of the second matrix (%d x


%d):\n", r2, c2);
for (int i =0; i <r2; i++) {
for (int j=0; j <c2; j++) {
scanf("%d", &mat2[i][j]);
}
}

for (int i= 0; i < r1; i++) {


for (int j=0;j < c2; j++) {
result[i][j] = 0;
}
}

for (int i = 0; i < r1; i++) {


for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

printf("Resulting matrix (%d x %d):\n", r1, c2);


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}
Program 2 . Insertion , deletion , traversal in array

// 2300290120261
// Tushar Kumar
// Program for Insertion, Deletion and Traversal in Array
#include <stdio.h>
void traversal(int arr[], int size);
void insertion(int arr[], int *size, int index, int value);
void deletion(int arr[], int *size, int index);
int main() {
int n, choice, index, value;
printf("Enter array size : ");
scanf("%d", &n);
int a[n + 1];

printf("Enter array elements: \n");


for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

while (1) {
printf("\nMenu:\n");
printf("1. Traverse\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the index of value to be inserted: ");
scanf("%d", &index);
printf("Enter the value to be inserted: ");
scanf("%d", &value);
insertion(a, &n, index, value);
break;
case 2:
traversal(a, n);
break;
case 3:
printf("Enter the index of element to be deleted: ");
scanf("%d", &index);
deletion(a, &n, index);
break;
case 4:
return 0;
default:
printf("Choice is invalid !\n");
}
}

return 0;
}

// Function to delete an element from a given index


void deletion(int arr[], int *size, int index) {
for (int i = index; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
printf("The new array is:\n");
traversal(arr, *size);
}
// Function to traverse array
void traversal(int arr[], int size) {
printf("Elements of the array are as follows:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

// Function to insert an element at a given index


void insertion(int arr[], int *size, int index, int value) {
for (int i = *size; i > index; i--) {
arr[i] = arr[i - 1];
}
arr[index] = value;
(*size)++;
printf("The new array is:\n");
traversal(arr, *size);
}
Program 3 ,4 String

#include <stdio.h>
// Function to calculate the length of a string
int string_length(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
// Function to reverse a string
void reverse_string(char str[], char reversed[]) {
int length = string_length(str);
for (int i = 0; i < length; i++) {
reversed[i] = str[length - i - 1];
}
reversed[length] = '\0';
}
int main() {
char str1[100] = "KIET College"; // Example string
char str2[100];
// Display the length of the string
int length = string_length(str1);
printf("Length of the string is: %d\n", length);
reverse_string(str1, str2);
printf("Reverse of the string is: %s\n", str2);
return 0;
}
Program 5 Sum
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}

int main() {
int number1, number2, result;
printf("Enter numbers to sum: ");
scanf("%d %d", &number1, &number2);
result = sum(number1, number2);
printf("Sum is : %d\n", result);
return 0;
}

You might also like