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 code skeleton to complete the missing code marked with dots ...
    1. Create a triangle pattern and a flag pattern and store them in two separate 2D char-typed arrays.
    2. Call the flipPattern function to flip the patterns. The function flips patterns along the first diagonal.
    3. Print the patterns before and after flipping to compare.
    4. Except the pattern creation parts in the main() function, you don't need to modify any code in it.
  2. //Define a named constant 
    #define SIZE 10
    
    //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
        // ...
    
        //Create a pattern of 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 to compare
    4. Don't need to modify the main().
  4. //Define named constants 
    #define ROWS 3
    #define COLS 4
    
    //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},
            {9, 6, 3, 4},
            {8, 2, 5, 5}
        };
        
        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  9  6  3  4  8  2  5  5
    After reversing ...
    5  5  2  8  4  3  6  9  1  7  5  2