Vous êtes sur la page 1sur 3

Explore this code:

#include <stdio.h>
#include <conio.h>
void input_array(int [][], int, int);
int display_array(int [][], int, int);
#define ROW 3
#define COLUMN 3
int main () {
int a[ROW][COLUMN];
input_array(a, ROW, COLUMN);
display_array(a, ROW, COLUMN);
getch();
return 0;
}
void input_array(int a[][COLUMN], int r, int c) {
int row, col;
printf("Please enter the array values...\n");
for (row=0; row<r; row++) {
for (col=0; col<c; col++) {
printf("\tEnter value at [%d][%d]: ", row+1, col+1);
scanf("%d", &a[row][col]);
}
}
}
int display_array(int a[][COLUMN], int r, int c) {
int row, col;
printf("\n\nDisplaying the array elements...\n");
for (row=0; row<r; row++) {
printf("\t");
for (col=0; col<c; col++) {
printf("%d ", a[row][col]);
}
printf("\n");
}
}

Programming Exercises 6: 2D Array Implementation


Write a program that implements and uses a 2D array with max dimension of ROW (constant)
by COLUMN (constant). Your program should implement and make use of meaningful
functions. EXCEPT FOR THE CONSTANTS, GLOBAL DECLARATIONS OF NON-FUNCTION
PROGRAMMER-DEFINED ELEMENTS ARE NOT ALLOWED.
Program works details:
When the program is executed, the user should see this menu:
Welcome to my 2D array program
Program menu
[1] Input values to the array
[2] Find smallest value
[3] Count the occurrences of a number
[0] Exit
Enter choice:

[1] Input values to the array


Input values to the array
o

When this is chosen, the program should ask the user for the actual dimension of
the array.
Enter the actual number of rows:
Enter the actual number of columns:

Based on the actual dimension, the program should ask all the values for the
array. The values should be stored to the array.
Please enter the values for the array
Enter value [0][0]:
Enter value [0][1]:

o
o

If there is an error on this part, simply return back to the main menu.
Once this is done, program should return back to the main menu.

[2] Find smallest value


Finding the smallest value in the array
o

When this is chosen, the program should print the array and find and print the
smallest value and its index (first occurrence).
Array:
//print array here, row-by-column style
Smallest value:
Index:

Once this is done, program should return back to the main menu.

[3] Count the occurrences of a number


Counting the occurrences of a number
o

When this is chosen, the program should ask the user for the value to be
searched and count and print the number of times the value occurs in the array.
Print the array content after.
Enter the value to be searched:
The value ___ has occurs __ time(s) in the array:
//print array row-by-column style

Once this is done, program should return back to the main menu.

Implement necessary error-handlings/trappings such as:


o If the user has entered an invalid choice
o If the actual dimension given by the user violates the actual dimension capacity
of the array implemented, prompt an error
o If the user has chosen 2 or 3 and there are no values stored in the array, prompt
an error and the program should not proceed with the work for 2 or 3

Functions to be implemented:
main_menu: for displaying the menu and asking the user for a choice
input_array: for asking the user for the values of and storing them to the array. This
returns nothing.
display_array: for displaying the content of the array row-by-column style. This returns
nothing.
find_smallest: for finding and returning the smallest value in the array.
count_occurrences: for counting the and returning the number of occurrences of the
value in the array