Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array Codes
- 1A. Write a program to store the elements in 1-D array and perform the
- operations like searching, sorting and reversing the elements. [Menu Driven]
- #include <stdio.h>
- #include <stdlib.h>
- void bubble_sort(int b[], int n);
- void reverse(int b[], int n);
- void search(int b[], int n);
- void main(){
- int a[7] = {10, 45, 51, 20, 75, 30, 35};
- int i, ch = 1, n;
- n = 7;
- printf("\n A : ");
- for (i = 0; i < n; i++)
- printf("\t %d", a[i]);
- while (ch != 4) {
- printf("\n 1.Sort \n 2.Reverse \n 3.Searching \n 4.Exit \n Select the option : ");
- scanf("%d", &ch);
- switch (ch) {
- case 1:
- bubble_sort(a, n);
- break;
- case 2:
- reverse(a, n);
- break;
- case 3:
- search(a, n);
- break;
- case 4:
- exit(0);
- }
- }
- }
- void bubble_sort(int b[], int n) {
- int p, i, temp;
- for (p = 0; p < n - 1; p++) {
- for (i = 0; i < n - 1; i++) {
- if (b[i] > b[i + 1]) {
- temp = b[i];
- b[i] = b[i + 1];
- b[i + 1] = temp;
- }
- }
- }
- printf("\n Array : ");
- for (i = 0; i < n; i++)
- printf("\n %d", b[i]);
- printf("\n \n Sorted Array : ");
- for (i = 0; i < n; i++) {
- printf("\t %d", b[i]);
- }
- }
- void reverse(int b[], int n) {
- int i;
- printf("\n Array : ");
- for (i = 0; i < n; i++)
- printf("\t %d", b[i]);
- printf("\n \n Reversed Array : ");
- for (i = n - 1; i >= 0; i--)
- printf("\t %d", b[i]);
- }
- void search(int b[], int n) {
- int data, i, flag = 0;
- printf("\n Array : ");
- for (i = 0; i < n; i++)
- printf("\t %d", b[i]);
- printf("\nEnter element to search: ");
- scanf("%d", &data);
- for (i = 0; i < n; i++) {
- if (b[i] == data) {
- printf("\n The element is present at position %d", i + 1);
- flag = 1;
- break;
- }
- }
- if (flag == 0) {
- printf("\n The element is not present in the array!!");
- }
- }
- 1B. Read the two arrays from the user and merge them and display the
- elements in sorted order. [Menu Driven]
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- // Function prototypes
- 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);
- 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 try again.\n");
- }
- getch(); // Pause before clearing the screen
- clrscr(); // Clear the screen for the next operation
- }
- }
- 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]);
- }
- }
- // Perform addition
- 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("\nResult (A + B):");
- print_mat(c);
- }
- void mul_mat() {
- int a[3][3], b[3][3], c[3][3], i, j, k;
- 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]);
- }
- }
- // Perform multiplication
- for (i = 0; i < 3; i++) {
- for (j = 0; j < 3; j++) {
- c[i][j] = 0; // Initialize sum
- for (k = 0; k < 3; k++) {
- c[i][j] += a[i][k] * b[k][j];
- }
- }
- }
- printf("\nMatrix A:");
- print_mat(a);
- printf("\nMatrix B:");
- print_mat(b);
- printf("\nResult (A * B):");
- print_mat(c);
- }
- void trans_mat() {
- int a[3][3], at[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]);
- }
- }
- // Perform transpose
- for (i = 0; i < 3; i++) {
- for (j = 0; j < 3; j++) {
- at[j][i] = a[i][j];
- }
- }
- printf("\nMatrix A:");
- print_mat(a);
- printf("\nTranspose of A:");
- print_mat(at);
- }
- void print_mat(int x[3][3]) {
- int i, j;
- for (i = 0; i < 3; i++) {
- printf("\n");
- for (j = 0; j < 3; j++) {
- printf("\t%d", x[i][j]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement