0% found this document useful (0 votes)
12 views10 pages

Shiv 231 3D

The document contains multiple C programs demonstrating various programming concepts including array manipulation (insertion, deletion, traversal), matrix multiplication, string length calculation and reversal, and a simple sum function. Each program is structured with functions for specific tasks and includes user input for dynamic operation. The document is dated 11/9/2024 and is authored by 'shiv'.

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)
12 views10 pages

Shiv 231 3D

The document contains multiple C programs demonstrating various programming concepts including array manipulation (insertion, deletion, traversal), matrix multiplication, string length calculation and reversal, and a simple sum function. Each program is structured with functions for specific tasks and includes user input for dynamic operation. The document is dated 11/9/2024 and is authored by 'shiv'.

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/ 10

Week 1- Array Class

Program no 1
// shiv

// 2300290120231

// Program for Array Insertion, Deletion and Traversal in Array

// 11/9/2024

#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 size of array: ");

scanf("%d", &n);

int arr[n + 1];

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

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

scanf("%d", &arr[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:

traversal(arr, n);

break;

case 2:

printf("Enter the index of value to be inserted: ");

scanf("%d", &index);

printf("Enter the value to be inserted: ");

scanf("%d", &value);

insertion(arr, &n, index, value);

break;

case 3:

printf("Enter the index of element to be deleted: ");

scanf("%d", &index);

deletion(arr, &n, index);

break;

case 4:

return 0;

default:

printf("Invalid choice!\n");

return 0;
}

// Function to traverse and print the 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);

// 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);

Output

Program no 2
// shiv

// 2300290120231

// program for matrix multiplication

// 11/9/2024

#include <stdio.h>

// Function to multiply two matrices

void multiplyMatrices(int m, int n, int matrix1[m][n], int c, int d, int matrix2[c][d], int result[m][d]) {

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

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


result[i][j] = 0;

for (int k = 0; k < n; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

// Function to input matrix values

void inputMatrix(int rows, int columns, int matrix[rows][columns]) {

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

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

scanf("%d", &matrix[i][j]);

// Function to print a matrix

void printMatrix(int rows, int columns, int matrix[rows][columns]) {

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

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

printf("%d ", matrix[i][j]);

printf("\n");

int main() {
int m, n, c, d;

printf("For matrix mxn:\n");

printf("Enter rows: ");

scanf("%d", &m);

printf("Enter columns: ");

scanf("%d", &n);

printf("For matrix cxd:\n");

printf("Enter rows of the second matrix (c): ");

scanf("%d", &c);

printf("Enter columns of the second matrix (d): ");

scanf("%d", &d);

if (n != c) {

printf("Cannot multiply\n");

return 1;

int matrix1[m][n], matrix2[c][d], result[m][d];

printf("Enter values for the first matrix:\n");

inputMatrix(m, n, matrix1);

printf("Enter values for the second matrix:\n");

inputMatrix(c, d, matrix2);

multiplyMatrices(m, n, matrix1, c, d, matrix2, result);

printf("Result matrix:\n");

printMatrix(m, d, result);

return 0;

}
Output

Program no 3&4
// shiv

// 2300290120231

// program for find the length of string and reversal of string

// 11/9/2024

#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] = "Hello my name is shiv"; // 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;

Output
Program no 5
// shiv

// 2300290120231

// Program to write and call sum function

// 11/9/2024

#include <stdio.h>

int sum(int a, int b) {

return a + b;

int main() {

int num1, num2, result;

printf("Enter numbers to evaluate sum: ");

scanf("%d %d", &num1, &num2);

// Call the sum function

result = sum(num1, num2);

printf("The sum is: %d\n", result);

return 0;
}

Output

You might also like