CSCI 152 Introduction to Computer Science
Lab 7
  

Objectives:
two-dimensional arrays and functions

Lab Practice

  1. Program 1 :
    Write a function void ReadArray (int A[ ][COL], int num_row, int num_col) that reads a two dimensional array, COL - is the number of columns in array A. Write a function int even (int A[][COL], int num_rows, int num_cols) that finds and returns the amount of the even elements among the elements of the array A. Write a program that reads five two-dimensional arrays of size 3X4 (3 rows and 4 columns) each and find an array that has a maximal amount of the even elements. Use function even to count the amount of even elements in each array and use function ReadArray to read an input. Try to declare only one two-dimensional array and re-use this memory space 5 times by using loop structure.

  2. Program 2 :
    Write a function void fillRandom(int A[ ][COL], int num_rows, int num_cols) that fills the array A with the random numbers between 0 and 4 (including 0 and 4). Write a program that performs the following:

  3. Program 3:
    Write a function int numNonZero(int A[][COL], int num_rows, int num_cols, int i, int j) that finds and returns the amount of non-zero neighbours elements of the element (i, j) of the two-dimensional array A. Write a program that reads one two-dimensional array of size 3X4 and for prints the amount of non-zero neighbours for each location in the array.

  4. Program 4 :
    Write a function void fillRandom(int A[ ][COL], int num_rows, int num_cols) that fills the array A with the random numbers between -3 and 3 (including -3 and 3). Write a function int numNonZero(int A[][COL], int num_rows, int num_cols) that finds and returns the amount of non-zero elements of the array A. Write a program that performs the following:

    Team Work

    Mini-Sudoku
    To solve a Mini-Sudoku puzzle, one must fill in a 4X4 grid with the integers from 1 to 4 in such a way that each integer appears once in each row, once in each column, and once in each 2X2 subgrid. (Normally Sudoku is played on a 9X9 grid). For example, the following would be a solution.
    1 2 3 4
    3 4 1 2
    2 1 4 3
    4 3 2 1

    Write a program which takes four rows of four integers as input. If the grid represents a legitimate solution, your program should print the words .Valid Solution.; otherwise, it should print one or more of the following, depending on which criteria fail:

    Your program should display each reasons which is responsible for a failure on a separate line, but the same reason should not be given more than once. For example, it might print both 'Invalid row' and 'Invalid subgrid' if both of these reasons apply, but it should print 'Invalid row' only once, regardless of how many rows actually fail. You may assume the input consists only of integers from 1 to 4, and that there are exactly four rows, each with four integers.

    Use two dimensional array to solve this problem

    Here are some sample inputs, along with the corresponding output.
    Input
    1 2 3 4
    3 4 1 2
    2 1 4 3
    4 3 2 1
    Output: VALID

    Input
    1 2 3 4
    4 3 2 1
    1 2 3 4
    4 3 2 1
    Output: Invalid column

    Input
    2 2 2 2
    1 1 1 1
    3 3 3 3
    4 4 4 4
    Output: Invalid row, Invalid subgrid

    Input
    3 3 3 3
    3 3 3 3
    3 3 3 3
    3 3 3 3
    Output: Invalid row, Invalid column, Invalid subgrid