CSC1321 Programming Assignment: Array Processing

INSTRUCTIONS: Work individually and submit to the Blackboard by the deadline. You are encouraged to start early.

  1. Flip Patterns Use the following skeleton code to complete the missing code marked with dots ...
    1. Define a function called flipPattern that flips a pattern about its first diagonal.
    2. Define another function called printPattern that prints a pattern.
    3. In the main() function, create a triangle pattern (actually a square pattern with symbols on the half and spaces on the other half) and a flag pattern and then store them into two separate 2D char-typed arrays.
    4. Call the flipPattern function to flip the patterns.
    5. Print the patterns before and after flipping for comparison.
    6. Except the code for pattern creation in the main() function, you don't need to modify any code in the main().
  2. //Define a named constant 
    #define SIZE 16
    
    //Function pre-declarations
    void printPattern(char p[][SIZE]);
    void flipPattern(char p[][SIZE]);
    
    int main()
    {
        //Two-Dimensional arrays to hold patterns
        char triangle[SIZE][SIZE];
        char flag[SIZE][SIZE];
        
        //Create a pattern of triangle and store it into the 2D array variable triangle
        // ...
    
        //Create a pattern of flag and store it into the 2D array variable flag 
        // ...
    
        //Print patterns
        printPattern(triangle);
        printPattern(flag);
        
        //Flip patterns
        flipPattern(triangle);
        flipPattern(flag);
        
        cout <<"\nAfter flipping ..." << endl;
        
        //Print patterns after flipping
        printPattern(triangle);
        printPattern(flag);
       
       return 0;
    }
    
    //Function Definitions:
    
    //Print a pattern stored in a 2-D array
    void printPattern(char p[][SIZE])
    {
       // ...
    }
    
    //Flip a pattern along its first diagonal
    void flipPattern(char p[][SIZE])
    {
    //Think about a letter at (i, j) or p[i][j] and a letter at (j, i) or p[j][i]
       // ...
    }
    
    //Sample output:
    Triangle pattern
    * * * * * * * * * * * * * * * *
    - * * * * * * * * * * * * * * *
    - - * * * * * * * * * * * * * *
    - - - * * * * * * * * * * * * *
    - - - - * * * * * * * * * * * *
    - - - - - * * * * * * * * * * *
    - - - - - - * * * * * * * * * *
    - - - - - - - * * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - - * * * * * * *
    - - - - - - - - - - * * * * * *
    - - - - - - - - - - - * * * * *
    - - - - - - - - - - - - * * * *
    - - - - - - - - - - - - - * * *
    - - - - - - - - - - - - - - * *
    - - - - - - - - - - - - - - - *
    
    Flag pattern
    - - - - - - - - * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - * * * * * * * *
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    
    
    After flipping ...
    
    Triangle pattern	 
    * - - - - - - - - - - - - - - -
    * * - - - - - - - - - - - - - -
    * * * - - - - - - - - - - - - -
    * * * * - - - - - - - - - - - -
    * * * * * - - - - - - - - - - -
    * * * * * * - - - - - - - - - -
    * * * * * * * - - - - - - - - -
    * * * * * * * * - - - - - - - -
    * * * * * * * * * - - - - - - -
    * * * * * * * * * * - - - - - -
    * * * * * * * * * * * - - - - -
    * * * * * * * * * * * * - - - -
    * * * * * * * * * * * * * - - -
    * * * * * * * * * * * * * * - -
    * * * * * * * * * * * * * * * -
    * * * * * * * * * * * * * * * *
     
    Flag pattern				  
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - -
    * * * * * * * * * - - - - - - -
    * * * * * * * * * - - - - - - -
    * * * * * * * * * - - - - - - -
    * * * * * * * * * - - - - - - -
    * * * * * * * * * - - - - - - -
    * * * * * * * * * - - - - - - -
    * * * * * * * * * - - - - - - -
    * * * * * * * * * - - - - - - -
    
    
  3. Fit and Reverse Use the following code skeleton to complete the missing code marked with ...
    1. Given a 2D array, store it in a 1D array row by row starting from the top row.
    2. Reverse the 1D array.
    3. Print the 1D array before and after reversing for comparison.
    4. Don't need to modify the main().
  4. //Define named constants 
    #define ROWS 3
    #define COLS 5
    
    //Function predeclarations
    void print1DArray(int oneDarr[], int length);
    void fitIn1DArray(int twoDarr[][COLS], int oneDarr[]);
    void reverse(int oneDarr[], int length);
    
    int main()
    {
        //Two Dimensional array of integers
        int n[ROWS][COLS] = {
            {2, 5, 7, 1, 3},
            {9, 6, 3, 4, 7},
            {8, 2, 5, 5, 0}
        };
        
        int p[ROWS*COLS];
        
        //Move 2D array into 1D array
        fitIn1DArray(n, p);
        
        //Print out the 1D array
        print1DArray(p, ROWS*COLS);
        
        //Reverse 1D array
        reverse(p, ROWS*COLS);
    
        cout <<"\nAfter reversing ..." << endl;
        
        //Print out the 1D array after reversing
        print1DArray(p, ROWS*COLS);
        cout << endl;
       
       return 0;
    }
    
    //Print out the 1D array
    void print1DArray(int oneDarr[], int length)
    {
        //oneDarr is the input array and length is its size
        //...
    }
    
    //Move a 2D array into a 1D array row by row  starting from the first row (top)
    void fitIn1DArray(int twoDarr[][COLS], int oneDarr[])
    {
    //Need to have an index variable for the one-dimentional array
    //Is this helpful? index = i * COLS + j
       // ...
    }
    
    //Reverse a 1D array
    void reverse(int oneDarr[], int length)
    {
    //There are many ways to do this.
    //Think about swapping the heads with the tails of the array
    
      // ...   
    }
    
    
    //Output:
    
    2  5  7  1  3  9  6  3  4  7  8  2  5  5  0
    After reversing ...
    0  5  5  2  8  7  4  3  6  9  3  1  7  5  2
    
    
    
  5. Find the largest number in each column. Add a function that finds the largest number in a specific column in the previous program and then call it from the main().
    //Input: a 2D array (twoDarr) and the column index (col)
    //Output: the largest number in the specific column 
    int findLargest(int twoDarr[][COLS], int col)
    {
       int largest = twoDarr[0][0];
    
       //Find the largest in the column "col"
       //...
       
       return largest;
    }
    
    //Sample output (from the main() function)
    Column:  0, 1, 2, 3, 4
    Largest: 9, 6, 7, 5, 7