Week 2 programs for performing read, write, addition and multiplication operations on 2D arrays

Source Code

#include <stdio.h>

int A[10][10];
int m, n, i, j;

int B[10][10];
int bm;

int written = 0;

void writeArray() {
	printf("\nEnter the number of rows and columns (maximum 10): ");
	scanf("%d %d", &m, &n);

	if (m <= 10 && n <= 10) {
		for (i = 0; i < n; i++) {
			for (j = 0; j < m; j++) {
				printf("Enter element (%d, %d): ", i, j);
				scanf("%d", &A[i][j]);
			}
		}
		written = 1;
		printf("\nArray entered\n\n");
	} else {
		printf("Limit breached, please try again.");
	}

}

void readArray() {
	if (written) {
		for (i = 0; i < n; i++) {
			for (j = 0; j < m; j++) {
				printf("%d\t", A[i][j]);
			}
			printf("\n");
		}
		printf("\nArray printed\n");
	}
	else {
		printf("\nConsider writing the array first\n\n");
	}
}

void addArray() {
	if (written) {
		printf("\nEnter the second array");

		for (i = 0; i < n; i++) {
			for (j = 0; j < m; j++) {
				printf("Enter element (%d, %d): ", i, j);
				scanf("%d", &B[i][j]);
			}
		}

		for (i = 0; i < m; i++) {
			for (j = 0; j < n; j++) {
				A[i][j] += B[i][j];
			}
		}
		printf("\nArray entered\n\n");
	}
	else {
		printf("\nConsider writing the first array first\n\n");
	}

}

void multiplyArray() {
	if (written) {
		printf("\nEnter the number of columns in the second matrix (maximum 10): ");
		scanf("%d", &bm);
		
		if (bm <= 10 && n <= 10) {
			for (i = 0; i < bm; i++) {
				for (j = 0; j < n; j++) {
					printf("Enter element (%d, %d): ", i, j);
						scanf("%d", &B[i][j]);
				}
			}
			printf("\nSecond array entered\n\n");
		
			int MUL[m][bm], k;
			
			for (i = 0; i < m; i++) {
				for (j = 0; j < bm; j++) {
					for (k = 0; k < n; k++) {
						MUL[i][j] += A[i][k] * B[k][j];
					}
				}
			} 
			printf("\nArray multiplication completed\n\n");

			for (i = 0; i < m; i++) {
				for (j = 0; j < bm; j++) {
					printf("%d\t", MUL[i][j]);
				}
				printf("\n");
			}

		} else {
			printf("Limit breached, please try again.");
		}

	}
	else {
		printf("\nConsider writing the first array first\n\n");
	}

}

void userChoice() {
	int input;
	printf("Enter 1 to write the array\nEnter 2 to read the array\nEnter 3 for array addition\nEnter 4 for array multiplication\nEnter 6 to terminate the program\n\n");
	printf("Enter command: ");
	scanf("%d", &input);

	switch(input) {
	   case(1):
	        printf("Writing the array\n");
	        writeArray();
			userChoice();
			break;
		case(2): 
			printf("Reading the array\n");
			readArray();
			userChoice();
			break;
		case(3):
			printf("Array addition initiated\n");
			addArray();
			userChoice();
		case(4):
			printf("Array multiplication initiated\n");
			multiplyArray();
			userChoice();
		case(6):
			printf("Terminating Program\n");
			break;
	}
}

int main () {
	printf("\n================ 2D Array Operations ================\n\n");

	userChoice();

	return(0);
}

Code Output

================ 2D Array Operations ================     

Enter 1 to write the array
Enter 2 to read the array
Enter 3 for array addition
Enter 4 for array multiplication
Enter 6 to terminate the program

Enter command: 1
Writing the array

Enter the number of rows and columns (maximum 10): 2      
3
Enter element (0, 0): 1
Enter element (0, 1): 2
Enter element (0, 2): 3
Enter element (1, 0): 4
Enter element (1, 1): 5
Enter element (1, 2): 6

Array entered

Enter 1 to write the array
Enter 2 to read the array
Enter 3 for array addition
Enter 4 for array multiplication
Enter 6 to terminate the program

Enter command: 4
Array multiplication initiated

Enter the number of columns in the second matrix (maximum 
10): 1
Enter element (0, 0): 1
Enter element (0, 1): 2
Enter element (0, 2): 3

Second array entered

Array multiplication completed

8192193
131
Enter 1 to write the array
Enter 2 to read the array
Enter 3 for array addition
Enter 4 for array multiplication
Enter 6 to terminate the program

Enter command: 4
Array multiplication initiated

Enter the number of columns in the second matrix (maximum 
10): 3
Enter element (0, 0): 1
Enter element (0, 1): 2
Enter element (0, 2): 3
Enter element (1, 0): 4
Enter element (1, 1): 5
Enter element (1, 2): 6
Enter element (2, 0): 7
Enter element (2, 1): 1
Enter element (2, 2): 4

Second array entered

Array multiplication completed

4201065 25      158
16711747        49      8192259
Enter 1 to write the array
Enter 2 to read the array
Enter 3 for array addition
Enter 4 for array multiplication
Enter 6 to terminate the program

Enter command: 6
Terminating Program