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