fork download
  1. // Online C compiler to run C program online
  2. #include <stdio.h>
  3.  
  4. #define ARRAY_SIZE 10
  5. #define MATRIX_SIZE 4
  6. //fill array 1
  7. int array[ARRAY_SIZE];
  8. int matrix[MATRIX_SIZE][MATRIX_SIZE];
  9. void fillArray(){
  10.  
  11. // Fill 1-D array
  12. printf("Enter 10 elements for the array:\n");
  13. for (int i = 0; i < ARRAY_SIZE; i++) {
  14. printf("Array [%d]: ", i);
  15. scanf("%d", &array[i]);
  16. }
  17. }
  18. void fillMatrix(){
  19. printf("\nEnter elements for the 4x4 matrix:\n");
  20. for (int i = 0; i < MATRIX_SIZE; i++) {
  21. for (int j = 0; j < MATRIX_SIZE; j++) {
  22. printf("Matrix [%d][%d]: ", i, j);
  23. scanf("%d", &matrix[i][j]);
  24. }
  25. }
  26. }
  27.  
  28. void shiftArrayLeft(int *arr, int size, int shifts) {
  29. shifts %= size; // Handle shifts greater than array size
  30. int temp[ARRAY_SIZE];
  31.  
  32. for (int i = 0; i < size; i++) {
  33. temp[i] = arr[(i + shifts) % size];
  34. }
  35.  
  36. for (int i = 0; i < size; i++) {
  37. arr[i] = temp[i];
  38. }
  39. }
  40.  
  41. void transposeMatrix(int matrix[MATRIX_SIZE][MATRIX_SIZE]) {
  42. for (int i = 0; i < MATRIX_SIZE; i++) {
  43. for (int j = 0; j < MATRIX_SIZE; j++) {
  44. printf("%d ", matrix[j][i]);
  45. }
  46. printf("\n");
  47. }
  48. }
  49.  
  50. int searchFirstOccurrence(int *arr, int size, int element) {
  51. for (int i = 0; i < size; i++) {
  52. if (arr[i] == element) {
  53. return i;
  54. }
  55. }
  56. return -1;
  57. }
  58.  
  59. void searchAllOccurrences(int *arr, int size, int element) {
  60. int found = 0;
  61. for (int i = 0; i < size; i++) {
  62. if (arr[i] == element) {
  63. printf("%d ", i);
  64. found = 1;
  65. }
  66. }
  67. if (!found) {
  68. printf("None");
  69. }
  70. printf("\n");
  71. }
  72.  
  73. void multiplyMatrix(int matrix[MATRIX_SIZE][MATRIX_SIZE]) {
  74. int result[MATRIX_SIZE][MATRIX_SIZE] = {0};
  75.  
  76. for (int i = 0; i < MATRIX_SIZE; i++) {
  77. for (int j = 0; j < MATRIX_SIZE; j++) {
  78. for (int k = 0; k < MATRIX_SIZE; k++) {
  79. result[i][j] += matrix[i][k] * matrix[k][j];
  80. }
  81. }
  82. }
  83.  
  84. for (int i = 0; i < MATRIX_SIZE; i++) {
  85. for (int j = 0; j < MATRIX_SIZE; j++) {
  86. printf("%d ", result[i][j]);
  87. }
  88. printf("\n");
  89. }
  90. }
  91. //display menue
  92. void displayMenu() {
  93. printf("\n=== Array Operations Menu ===\n");
  94. printf("1. Shift 1-D Array Left\n");
  95. printf("2. Transpose of 2-D Array\n");
  96. printf("3. Search in 1-D Array (First Occurrence)\n");
  97. printf("4. Search in 1-D Array (All Occurrences)\n");
  98. printf("5. Matrix Multiplication\n");
  99. printf("6. Exit\n");
  100. }
  101.  
  102. int main() {
  103. fillArray();
  104. fillMatrix();
  105. int choice;
  106. while (1) {
  107. displayMenu();
  108. printf("Enter your choice: ");
  109. scanf("%d", &choice);
  110.  
  111. switch (choice) {
  112. case 1: {
  113. int shifts;
  114. printf("Enter the number of shifts: ");
  115. scanf("%d", &shifts);
  116. shiftArrayLeft(array, ARRAY_SIZE, shifts);
  117. printf("Shifted Array: ");
  118. for (int i = 0; i < ARRAY_SIZE; i++) {
  119. printf("%d ", array[i]);
  120. }
  121. printf("\n");
  122. break;
  123. }
  124. case 2:
  125. printf("Transposed Matrix:\n");
  126. transposeMatrix(matrix);
  127. break;
  128. case 3: {
  129. int element;
  130. printf("Enter the element to search: ");
  131. scanf("%d", &element);
  132. int index = searchFirstOccurrence(array, ARRAY_SIZE, element);
  133. if (index != -1) {
  134. printf("Element found at index: %d\n", index);
  135. } else {
  136. printf("Element not found.\n");
  137. }
  138. break;
  139. }
  140. case 4: {
  141. int element;
  142. printf("Enter the element to search: ");
  143. scanf("%d", &element);
  144. printf("Element found at indexes: ");
  145. searchAllOccurrences(array, ARRAY_SIZE, element);
  146. break;
  147. }
  148. case 5:
  149. printf("Result of Matrix Multiplication:\n");
  150. multiplyMatrix(matrix);
  151. break;
  152. case 6:
  153. printf("Exiting the program. Goodbye!\n");
  154. return 0;
  155. default:
  156. printf("Invalid choice. Please try again.\n");
  157. }
  158. }
  159.  
  160. return 0;
  161.  
  162. }
Success #stdin #stdout 0.02s 26128KB
stdin
Standard input is empty
stdout
// Online C compiler to run C program online
#include <stdio.h>

#define ARRAY_SIZE 10
#define MATRIX_SIZE 4
//fill array 1
int array[ARRAY_SIZE];
int matrix[MATRIX_SIZE][MATRIX_SIZE];
    void fillArray(){
     
          // Fill 1-D array
    printf("Enter 10 elements for the array:\n");
    for (int i = 0; i < ARRAY_SIZE; i++) {
        printf("Array [%d]: ", i);
        scanf("%d", &array[i]);
    }
    }
    void fillMatrix(){
          printf("\nEnter elements for the 4x4 matrix:\n");
    for (int i = 0; i < MATRIX_SIZE; i++) {
        for (int j = 0; j < MATRIX_SIZE; j++) {
            printf("Matrix [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }
    }
    
    void shiftArrayLeft(int *arr, int size, int shifts) {
    shifts %= size;  // Handle shifts greater than array size
    int temp[ARRAY_SIZE];

    for (int i = 0; i < size; i++) {
        temp[i] = arr[(i + shifts) % size];
    }

    for (int i = 0; i < size; i++) {
        arr[i] = temp[i];
    }
}

void transposeMatrix(int matrix[MATRIX_SIZE][MATRIX_SIZE]) {
    for (int i = 0; i < MATRIX_SIZE; i++) {
        for (int j = 0; j < MATRIX_SIZE; j++) {
            printf("%d ", matrix[j][i]);
        }
        printf("\n");
    }
}

int searchFirstOccurrence(int *arr, int size, int element) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == element) {
            return i;
        }
    }
    return -1;
}

void searchAllOccurrences(int *arr, int size, int element) {
    int found = 0;
    for (int i = 0; i < size; i++) {
        if (arr[i] == element) {
            printf("%d ", i);
            found = 1;
        }
    }
    if (!found) {
        printf("None");
    }
    printf("\n");
}

void multiplyMatrix(int matrix[MATRIX_SIZE][MATRIX_SIZE]) {
    int result[MATRIX_SIZE][MATRIX_SIZE] = {0};

    for (int i = 0; i < MATRIX_SIZE; i++) {
        for (int j = 0; j < MATRIX_SIZE; j++) {
            for (int k = 0; k < MATRIX_SIZE; k++) {
                result[i][j] += matrix[i][k] * matrix[k][j];
            }
        }
    }

    for (int i = 0; i < MATRIX_SIZE; i++) {
        for (int j = 0; j < MATRIX_SIZE; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }
}
//display menue 
void displayMenu() {
    printf("\n=== Array Operations Menu ===\n");
    printf("1. Shift 1-D Array Left\n");
    printf("2. Transpose of 2-D Array\n");
    printf("3. Search in 1-D Array (First Occurrence)\n");
    printf("4. Search in 1-D Array (All Occurrences)\n");
    printf("5. Matrix Multiplication\n");
    printf("6. Exit\n");
}

int main() {
fillArray();
fillMatrix();
int choice;
 while (1) {
        displayMenu();
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1: {
                int shifts;
                printf("Enter the number of shifts: ");
                scanf("%d", &shifts);
                shiftArrayLeft(array, ARRAY_SIZE, shifts);
                printf("Shifted Array: ");
                for (int i = 0; i < ARRAY_SIZE; i++) {
                    printf("%d ", array[i]);
                }
                printf("\n");
                break;
            }
            case 2:
                printf("Transposed Matrix:\n");
                transposeMatrix(matrix);
                break;
            case 3: {
                int element;
                printf("Enter the element to search: ");
                scanf("%d", &element);
                int index = searchFirstOccurrence(array, ARRAY_SIZE, element);
                if (index != -1) {
                    printf("Element found at index: %d\n", index);
                } else {
                    printf("Element not found.\n");
                }
                break;
            }
            case 4: {
                int element;
                printf("Enter the element to search: ");
                scanf("%d", &element);
                printf("Element found at indexes: ");
                searchAllOccurrences(array, ARRAY_SIZE, element);
                break;
            }
            case 5:
                printf("Result of Matrix Multiplication:\n");
                multiplyMatrix(matrix);
                break;
            case 6:
                printf("Exiting the program. Goodbye!\n");
                return 0;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }

    return 0;

}