pe6.pdf

Upload: benz-lystin-carcuevas-ybanez

Post on 03-Mar-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

  • Explore this code:

    #include #include 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

  • 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:

    o 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 If there is an error on this part, simply return back to the main menu. o 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:

    o 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

    o 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