Data Structure 1
Data Structure 1
Data Structure
(23BCA402DS02)
CLASS :
Roll no. :
Submitted by Submitted to
9. Write a Program for Matrix Multiplication.
#include<stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,j,k;
// enter the values of first matrix
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\nenter the values of first matrix :");
scanf(" %d",&a[i][j]);}}
//enter the values of second matrix
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\nenter the values of second matrix :");
scanf(" %d",&b[i][j]);}}
//printing the first matrix
printf("\nthe first matrix is :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d\t",a[i][j]);}}
//printing the value of second matrix
printf("\nthe second matrix is :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d\t",b[i][j]); }}
//multiplication of two matrix
printf("\nthe multiplication of two matrix are :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
c[i][j]=0;
for(k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j]; }
printf("%d\t",c[i][j]);}}
return 0;}
output:
enter the values of first matrix :1
enter the values of first matrix :2
enter the values of first matrix :3
enter the values of first matrix :4
enter the values of first matrix :5
enter the values of first matrix :6
enter the values of first matrix :7
enter the values of first matrix :8
enter the values of first matrix :9
enter the values of second matrix :1
enter the values of second matrix :2
enter the values of second matrix :3
enter the values of second matrix :4
enter the values of second matrix :5
enter the values of second matrix :6
enter the values of second matrix :7
enter the values of second matrix :8
enter the values of second matrix :9
the first matrix is :
1 2 3
4 5 6
7 8 9
the second matrix is :
1 2 3
4 5 6
7 8 9
the multiplication of two matrix are :
30 36 42
66 81 96
102 126 150
output:
enter the values of first matrix:1
enter the values of first matrix:2
enter the values of first matrix:3
enter the values of first matrix:4
enter the values of first matrix:5
enter the values of first matrix:6
enter the values of first matrix:7
enter the values of first matrix:8
enter the values of first matrix:9
enter the values of second matrix:1
enter the values of second matrix:2
enter the values of second matrix:3
enter the values of second matrix:4
enter the values of second matrix:5
enter the values of second matrix:6
enter the values of second matrix:7
enter the values of second matrix:8
enter the values of second matrix:9
the first matrix is :
1 2 3
4 5 6
7 8 9
int main() {
int myArray[] = {64, 34, 25, 12, 22, 11, 90, 5};
int n = sizeof(myArray) / sizeof(myArray[0]);
quicksort(myArray, 0, n - 1);
// Function to insert a new element at the end of the singly linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Driver Code
int main() {
struct Node* head = NULL;
insertAtFirst(&head, 10);
printf("Linked list after inserting the node:10 at the beginning \n");
print(head);
return 0;
}
OUTPUT:
Linked list after inserting the node:10 at the beginning
10 -> NULL
Linked list after inserting the node:20 at the end
10 -> 20 -> NULL
Linked list after inserting the node:5 at the end
10 -> 20 -> 5 -> NULL
Linked list after inserting the node:30 at the end
10 -> 20 -> 5 -> 30 -> NULL
Linked list after inserting the node:15 at position 2
10 -> 20 -> 15 -> 5 -> 30 -> NULL
Linked list after deleting the first node:
20 -> 15 -> 5 -> 30 -> NULL
Linked list after deleting the last node:
20 -> 15 -> 5 -> NULL
Linked list after deleting the node at position 1:
20 -> 5 -> NULL
17.write a program of tree traversals.
ANSWER:
// Tree traversal in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
return newNode;
}
int main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);