Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Matrix Code
- 1C. Write a program to perform the Matrix addition, Multiplication and
- Transpose Operation. [Menu Driven]
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- void add_mat();
- void mul_mat();
- void trans_mat();
- void print_mat(int x[3][3]);
- void main()
- {
- int ch = 0;
- clrscr(); // Clear the screen
- while (ch != 4)
- {
- printf("\n1. Addition\n2. Multiplication\n3. Transpose\n4. Exit");
- printf("\nSelect the operation: ");
- scanf("%d", &ch); // Corrected `scanf` syntax
- switch (ch)
- {
- case 1:
- add_mat();
- break;
- case 2:
- mul_mat();
- break;
- case 3:
- trans_mat();
- break;
- case 4:
- exit(0);
- default:
- printf("\nInvalid choice! Please select again.");
- }
- getch(); // Pause for user to view results
- }
- }
- void add_mat()
- {
- int a[3][3], b[3][3], c[3][3], i, j;
- printf("\nEnter the matrix A (3x3):\n");
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- scanf("%d", &a[i][j]);
- }
- }
- printf("\nEnter the matrix B (3x3):\n");
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- scanf("%d", &b[i][j]);
- }
- }
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- c[i][j] = a[i][j] + b[i][j];
- }
- }
- printf("\nMatrix A:");
- print_mat(a);
- printf("\nMatrix B:");
- print_mat(b);
- printf("\nA + B:");
- print_mat(c);
- }
- void mul_mat()
- {
- int i, j, k, sum;
- int a[3][3], b[3][3], c[3][3];
- printf("\nEnter the matrix A (3x3):\n");
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- scanf("%d", &a[i][j]);
- }
- }
- printf("\nEnter the matrix B (3x3):\n");
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- scanf("%d", &b[i][j]);
- }
- }
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- sum = 0; // Initialize sum for each element
- for (k = 0; k < 3; k++)
- {
- sum += a[i][k] * b[k][j];
- }
- c[i][j] = sum;
- }
- }
- printf("\nMatrix A:");
- print_mat(a);
- printf("\nMatrix B:");
- print_mat(b);
- printf("\nA * B:");
- print_mat(c);
- }
- void trans_mat()
- {
- int i, j;
- int a[3][3], at[3][3];
- printf("\nEnter the matrix A (3x3):\n");
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- scanf("%d", &a[i][j]);
- at[j][i] = a[i][j]; // Calculate transpose directly
- }
- }
- printf("\nMatrix A:");
- print_mat(a);
- printf("\nTranspose of A:");
- print_mat(at);
- }
- void print_mat(int x[3][3])
- {
- int i, j;
- printf("\n");
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- {
- printf("\t%d", x[i][j]);
- }
- printf("\n");
- }
- }
- 6A. : Write a program to find the Adjacency Matrix
- #include <stdio.h>
- #include <conio.h> // For Turbo C-specific functions like clrscr() and getch()
- #define V 4 // Fixing the definition of V
- // Function to initialize the adjacency matrix with 0s
- void init(int array[][V]) {
- int i, j;
- for (i = 0; i < V; i++) {
- for (j = 0; j < V; j++) {
- array[i][j] = 0;
- }
- }
- }
- // Function to add an edge between vertices i and j
- void addEdge(int array[][V], int i, int j) {
- array[i][j] = 1;
- array[j][i] = 1;
- }
- // Function to print the adjacency matrix
- void printAdjMatrix(int array[][V]) {
- int i, j;
- for (i = 0; i < V; i++) {
- printf("%d: ", i);
- for (j = 0; j < V; j++) {
- printf("%d ", array[i][j]);
- }
- printf("\n");
- }
- }
- int main() {
- int adjMatrix[V][V]; // Initialize the adjacency matrix
- clrscr(); // Clear the screen (Turbo C-specific)
- init(adjMatrix); // Initialize matrix elements to 0
- addEdge(adjMatrix, 0, 1);
- addEdge(adjMatrix, 0, 2);
- addEdge(adjMatrix, 0, 3);
- addEdge(adjMatrix, 1, 2);
- addEdge(adjMatrix, 2, 0);
- printAdjMatrix(adjMatrix); // Print the adjacency matrix
- getch(); // Wait for a key press before exiting (Turbo C-specific)
- return 0; // Return 0 to indicate successful execution
- }
- 6B. Write a program for shortest path matrix using Warshall’s Algorithm.
- #include <stdio.h>
- #include <conio.h> // Include for Turbo C-specific functions like getch()
- #define INF 1000 // A large value to represent infinity
- int main() {
- int n = 5, i, j, k;
- int w[5][5] = {{0, 7, 12, 9, INF}, {5, 0, INF, 11, INF}, {INF, 9, 0, INF, 7}, {INF, INF, 5, 0, INF}, {INF, 8, 8, INF, 0}};
- int s[5][5] = {{0, 7, 12, 9, INF}, {5, 0, INF, 11, INF}, {INF, 9, 0, INF, 7}, {INF, INF, 5, 0, INF}, {INF, 8, 8, INF, 0}};
- clrscr(); // Clear the screen (Turbo C-specific)
- // Print the initial weight matrix W
- printf("\nW: ");
- for (i = 0; i < n; i++) {
- printf("\n");
- for (j = 0; j < n; j++) {
- printf("\t%d", w[i][j]);
- }
- }
- // Print the initial shortest path matrix S
- printf("\nS: ");
- for (i = 0; i < n; i++) {
- printf("\n");
- for (j = 0; j < n; j++) {
- printf("\t%d", s[i][j]);
- }
- }
- // Floyd-Warshall algorithm for shortest path
- for (k = 0; k < n; k++) {
- for (i = 0; i < n; i++) {
- for (j = 0; j < n; j++) {
- if (s[i][j] > (s[i][k] + s[k][j])) {
- s[i][j] = s[i][k] + s[k][j];
- }
- }
- }
- // Print the shortest path matrix after the k-th iteration
- printf("\nS %d: ", k);
- for (i = 0; i < n; i++) {
- printf("\n");
- for (j = 0; j < n; j++) {
- printf("\t%d", s[i][j]);
- }
- }
- }
- getch(); // Wait for the user to press a key before exiting (Turbo C-specific)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement