Page 295 - Computer_Science_F5
P. 295

Computer Science  The  following C++ Program  Example      Activity 4.8:   9
           4.34 demonstrates an example of a two-
           dimensional array. The output is shown
                                                       Filling number in the table
           in Figure 4.64.
                                                      Consider  a  box  of  9 grid  divided
          FOR ONLINE READING ONLY
                   Program Example 4.34:
                                                      into  3  small  boxes as shown in
                                                              3
                                                      Table 4.17.
            Two-dimensional array
                                                      Table 4.17: Sudoku tables

             #include<iostream>                        6       9      4              1
             #include<iomanip>                         7   1      5       9  6
             using namespace std;
             int main()                                    5
             {                                         2       7      8          9
                    int i, j, matrix[3][3];
                    for(i=0; i<3; i++)                                6          2   4
                    for(j=0; j<3; j++)                     6      9                  8
                    matrix[i][j]=i*3+j+1;
                    cout<<”Display an array: “<<endl;          8             3
                    for(i=0; i<3; i++)                            4                  7
             {
                    for(j=0; j<3; j++)                                5

                    cout<<setw(2)<<matrix[i][j];
                    cout<<endl;                       (a) Fill all blanks where each row and
             }                                            column should complete number 1
                    return 0;
             }                                            to 9 without repeating any number.
                                                      (b) Write a C++ program using array
                                                          that prompts a user to fill the box.
             Output:
                                                          The program should prompt the
                                                          user to enter the given number to
                                                          each box and display the completely
                                                          filled table.




                                                        Exercise 4.15
                Figure 4.64: Output of the program
                                                      1.  Write a C++ program using array
            Note:   A function  setw(2)  creates  a       that allows a teacher to enter the
                    gap of 2 size between two             number of students and their
                    numbers as it is displayed in         marks. Then, the program should
                    the output.                           find average class marks and use


                                                 286
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   286                                                     23/07/2024   12:34
   290   291   292   293   294   295   296   297   298   299   300