arrow
arrow

PF-PUCIT(NEW-CAMPUS)

ASSIGNMENT 08:

2D-Arrays


                            
                               Two Dimensional Arays have x and y axis.
                               Matrix rules mostly apply.
                               for(int i=0;i<row;i++)
                               {
                                for(int j=0;j<column;j++)
                                {
                                    cout<<"*  ";
                                }
                                cout<<endl;
                               }
                            
                        

Task 1. Write a program that subtract 2 nd matrix from 1 st. Take matrix of Size 4x5. Take input from user in both. Finally display the 1 st matrix , 2 nd matrix and resultant matrix after subtraction. Hint: use a 2D Array to store the matrix data


                        
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    const int row = 4;
    const int col = 5;
    int matrix1[row][col];
    int matrix2[row][col];
    int resultant_subMatrix[row][col];
    // Input-matrix1
    cout << "Enter the matrix 1: " << endl;
    for (int i = 0; i < row; i++)
    {
        cout << "Row " << i + 1 << " :" << endl;
        for (int j = 0; j < col; j++)
        {
            cin >> matrix1[i][j];
        }
    }
    cout << endl;
    // Input-matrix2
    cout << "Enter the matrix 2: " << endl;
    for (int i = 0; i < row; i++)
    {
        cout << "Row " << i + 1 << " :" << endl;
        for (int j = 0; j < col; j++)
        {
            cin >> matrix2[i][j];
        }
    }

    // resultant_subMatrix
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            resultant_subMatrix[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }

    // Display Matrix_1
    cout << "Matrix 1:" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << left;
            cout << setw(5) << matrix1[i][j];
        }
        cout << endl;
    }
    cout << endl;
    // Display Matrix_2
    cout << "Matrix 2:" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << left;
            cout << setw(5) << matrix2[i][j];
        }
        cout << endl;
    }
    cout << endl;
    // Display resultant_subMatrix
    cout << "Resultant Subtracted matrix from 2nd to 1st:" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << left;
            cout << setw(5) << resultant_subMatrix[i][j];
        }
        cout << endl;
    }
    return 0;
}
                          
                        
                    

Task 2. Write a program that multiply two matrix. 1 st of size 4x3 and 2 nd of size 3x4. initialized with test data. Finally display the 1 st matrix , 2 nd matrix and resultant matrix after multiplication. Hint: use a 2D Array to store the matrix data.


                        
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    const int row1 = 4;
    const int col1 = 3;
    const int row2 = 3;
    const int col2 = 4;
    int matrix1[row1][col1] = {{1, 2, 3},
                               {4, 5, 6},
                               {7, 8, 9},
                               {2, 5, 9}};
    int matrix2[row2][col2] = {{6, 7, 8, 9},
                               {1, 2, 3, 4},
                               {2, 3, 7, 1}};
    // Resultant_multiMatrix has number of rows equal to matrix1 and columns to matrix2
    int multi_Matrix[row1][col2] = {0};
    // matrix multipliaction
    for (int i = 0; i < row1; i++)
    {
        for (int j = 0; j < col2; j++)
        {
            for (int k = 0; k < row2; k++)
            {
                multi_Matrix[i][j] = multi_Matrix[i][j] + (matrix1[i][k] * matrix2[k][j]);
            }
        }
    }
    // Display Matrix_1
    cout << "Matrix 1:" << endl;
    for (int i = 0; i < row1; i++)
    {
        for (int j = 0; j < col1; j++)
        {
            cout << left;
            cout << setw(5) << matrix1[i][j];
        }
        cout << endl;
    }
    cout << endl;
    // Display Matrix_2
    cout << "Matrix 2:" << endl;
    for (int i = 0; i < row2; i++)
    {
        for (int j = 0; j < col2; j++)
        {
            cout << left;
            cout << setw(5) << matrix2[i][j];
        }
        cout << endl;
    }
    cout << endl;
    // Display resultant_subMatrix
    cout << "Resultant Multiplicated matrix of matrix1_X_matrix2:" << endl;
    for (int i = 0; i < row1; i++)
    {
        for (int j = 0; j < col2; j++)
        {
            cout << left;
            cout << setw(5) << multi_Matrix[i][j];
        }
        cout << endl;
    }
}
                        
                    

Task 3. Write a program that creates a two-dimensional array of size 7 rows and 5 columns, initialized with test data. Use any data type you wish. The program should do the following functionalities: • Get-Total
Display the total of all the values in the array.
• Get-Average
Display the average of all the values in the array.
• Get-RowTotal
Take an integer from user as subscript of a row in the array. Then display the total of the values in the specified row.
• Get-ColumnTotal
Take an integer from user as subscript of a column in the array. Then display the total of the values in the specified column.
• Get-HighestInRow
Take an integer from user as subscript of a row in the array. Then display highest value in the specified row of the array.
• Get-LowestInRow
Take an integer from user as subscript of a row in the array. Then display lowest value in the specified row of the array.


                        
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    const int row = 7;
    const int col = 5;
    int matrix[row][col] = {{1, 2, 3, 4, 5},
                            {6, 7, 8, 9, 10},
                            {11, 12, 13, 14, 15},
                            {16, 17, 18, 19, 20},
                            {21, 22, 23, 24, 25},
                            {26, 27, 28, 29, 30},
                            {31, 32, 33, 34, 35}};
    int total = 0;
    float average;
    int rowTotal = 0;
    int colTotal = 0;
    int high_inRow;
    int low_inRow;
    int colNum;
    int rowNum;
    // Display
    cout << "Matrix :" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << left;
            cout << setw(5) << matrix[i][j];
        }
        cout << endl;
    }
    cout << endl;

    // get-total
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            total += matrix[i][j];
        }
    }
    cout << "The total sum of the all values = " << total << endl;

    // get-average
    average = float(total) / (row * col);
    cout << "The average of the all values = " << average << endl;

    // get-rowTotal
    cout << "Enter the row number whose sum you want to find:" << endl;
    cin >> rowNum;
    rowNum--;
    for (int i = 0; i < col; i++)
    {
        rowTotal += matrix[rowNum][i];
    }
    cout << "The sum of the row " << ++rowNum << " = " << rowTotal << endl;

    // get-colTotal
    cout << "Enter the col number whose sum you want to find:" << endl;
    cin >> colNum;
    colNum--;
    for (int i = 0; i < row; i++)
    {
        colTotal += matrix[i][colNum];
    }
    cout << "The sum of the col " << ++colNum << " = " << colTotal << endl;

    // get-HighinRow
    cout << "Enter the row whose highest value that you want to display:" << endl;
    cin >> rowNum;
    high_inRow = matrix[rowNum][0];
    for (int i = 0; i < col; i++)
    {
        if (matrix[rowNum][i] > high_inRow)
        {
            high_inRow = matrix[rowNum][i];
        }
    }
    cout << "The highest value in the row " << rowNum << " :" << high_inRow << endl;

    // get-lowinRow
    cout << "Enter the row whose highest value that you want to display:" << endl;
    cin >> rowNum;
    low_inRow = matrix[rowNum][0];
    for (int i = 0; i < col; i++)
    {
        if (matrix[rowNum][i] < low_inRow)
        {
            low_inRow = matrix[rowNum][i];
        }
    }
    cout << "The lowest value in the row " << rowNum << " :" << low_inRow << endl;
}
                        
                    

Task 4. Tic-Tac-Toe Game
Write a program that allows two players to play a game of tic-tac-toe. Use a two dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that • Displays the contents of the board array
• Allows player 1 to select a location on the
board for an X. The program should ask the user to enter the row and column number. • Allows player 2 to select a location on the board for an O. The program should ask
the user to enter the row and column number. • Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the
program should say so and end. Player 1 wins when there are three Xs in a row on the game board. The Xs can appear
in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner;


                        
                            // Tic-tac-toe game
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    // varaibles
    const int row = 3;
    const int col = 3;
    char board[row][col];
    char start;
    bool flag = false;
    bool player1_win = false;
    bool player2_win = false;
    bool match_tie = false;

    //--Iniliaze board with *
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            board[i][j] = '*';
        }
    }
    for (int i = 0; i < 10; i++)
    {
        cout << endl;
    }
    cout << "\t\t\t\t ________________________________________________" << endl;
    cout << "\t\t\t\t|                                                |" << endl;
    cout << "\t\t\t\t|                 WELCOME PLAYERS!               |" << endl;
    cout << "\t\t\t\t|                        TO                      |" << endl;
    cout << "\t\t\t\t|            TIC_TAC_TOE-----G_A_M_E             |" << endl;
    cout << "\t\t\t\t|                                                |" << endl;
    cout << "\t\t\t\t|   SIR, PLEASE PRESS \"S\" TO START THE GAME:     |" << endl;
    cout << "\t\t\t\t|________________________________________________|" << endl;
    start = getch();
    start = tolower(start);
    while (start != 's')
    {
        cout << endl;
        cout << "\t\t\t\t\tInvalid input!:" << endl;
        cout << endl;
        cout << "\t\t\t\t\tSir,please enter \'s\' to start the game:";
        start = getch();
        start = tolower(start);
    }
    system("cls");
    do
    {

        // loop variables
        int player1_rowNum = 0;
        int player1_colNum = 0;
        int player2_rowNum = 0;
        int player2_colNum = 0;

        // Display board contents
        for (int i = 0; i < 10; i++)
        {
            cout << endl;
        }
        for (int i = 0; i < row; i++)
        {
            cout << "\t\t\t\t\t-------------------" << endl;
            cout << "\t\t\t\t\t";
            for (int j = 0; j < col; j++)
            {
                cout << "|  ";
                cout << board[i][j] << "  ";
            }
            cout << "|";
            cout << endl;
        }
        cout << "\t\t\t\t\t-------------------" << endl;

        // for space between borad and input
        for (int i = 0; i < 3; i++)
        {
            cout << endl;
        }

        // Player-1_Turn
        cout << "\t\t\t\t\t***Player 1 turn****" << endl;
        cout << endl;
        cout << "\t\t\t\t\t=>Enter row number:";
        cin >> player1_rowNum;
        while (player1_rowNum < 1 || player1_rowNum > 3)
        {
            cout << "\t\t\t\t\tInvalid:input!" << endl;
            cout << "\t\t\t\t\tPlease Enter the valid Input!";
            cin >> player1_rowNum;
        }
        cout << "\t\t\t\t\t=>Enter column number:";
        cin >> player1_colNum;
        while (player1_colNum < 1 || player1_colNum > 3)
        {
            cout << "\t\t\t\t\tInvalid:input!" << endl;
            cout << "\t\t\t\t\tPlease Enter the valid Input!";
            cin >> player1_colNum;
        }
        // already used index validation
        if (board[player1_rowNum - 1][player1_colNum - 1] == '*')
        {
            board[player1_rowNum - 1][player1_colNum - 1] = 'X';
        }
        else
        {
            while (board[player1_rowNum - 1][player1_colNum - 1] != '*')
            {
                cout << "\t\t\tThis index is already used please enter the valid index:" << endl;
                cout << endl;
                cout << "\t\t\t\t\t=>Enter row number:";
                cin >> player1_rowNum;
                while (player1_rowNum < 1 || player1_rowNum > 3)
                {
                    cout << "\t\t\t\t\tInvalid:input!" << endl;
                    cout << "\t\t\t\t\tPlease Enter the valid Input!";
                    cin >> player1_rowNum;
                }
                cout << "\t\t\t\t\t=>Enter column number:";
                cin >> player1_colNum;
                while (player1_colNum < 1 || player1_colNum > 3)
                {
                    cout << "\t\t\t\t\tInvalid:input!" << endl;
                    cout << "\t\t\t\t\tPlease Enter the valid Input!";
                    cin >> player1_colNum;
                }
            }
            board[player1_rowNum - 1][player1_colNum - 1] = 'X';
        }

        // Logic-for-row-check_player_1
        for (int i = 0; i < row; i++)
        {
            if (board[i][0] == 'X')
            {
                if ((board[i][0] == board[i][1]) && (board[i][0] == board[i][2]))
                {
                    flag = true;
                    player1_win = true;
                }
            }
        }

        // Logic-for-col-check_player_1
        for (int i = 0; i < col; i++)
        {
            if (board[0][i] == 'X')
            {
                if ((board[0][i] == board[1][i]) && (board[0][i] == board[2][i]))
                {
                    flag = true;
                    player1_win = true;
                }
            }
        }

        // Logic-for-left_diagonal-check_player_1
        if (board[0][0] == 'X')
        {
            if ((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]))
            {
                flag = true;
                player1_win = true;
            }
        }

        // Logic-for-Right_diagonal-check_player_1
        if (board[0][2] == 'X')
        {
            if ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]))
            {
                flag = true;
                player1_win = true;
            }
        }
        cout << endl;
        // To check match is tie or not
        int count = 0;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (board[i][j] != '*')
                {
                    count++;
                }
            }
        }
        if (count == 9 && player1_win == false)
        {
            if (player2_win == false)
            {
                flag = true;
                match_tie = true;
            }
        }

        // If player-1 is not win the match,and match is not tie, than player 2 play
        if (player1_win == false && flag == false)
        {
            // To clear the screen on every updation
            system("cls");

            // Display board contents
            for (int i = 0; i < 10; i++)
            {
                cout << endl;
            }
            for (int i = 0; i < row; i++)
            {
                cout << "\t\t\t\t\t-------------------" << endl;
                cout << "\t\t\t\t\t";
                for (int j = 0; j < col; j++)
                {
                    cout << "|  ";
                    cout << board[i][j] << "  ";
                }
                cout << "|";
                cout << endl;
            }
            cout << "\t\t\t\t\t-------------------" << endl;

            // Player-2_Turn
            // for space between borad and input
            for (int i = 0; i < 3; i++)
            {
                cout << endl;
            }
            cout << "\t\t\t\t\t***Player 2 turn****" << endl;
            cout << endl;
            cout << "\t\t\t\t\t=>Enter row number:";
            cin >> player2_rowNum;
            while (player2_rowNum < 1 || player2_rowNum > 3)
            {
                cout << "\t\t\t\t\tInvalid:input!" << endl;
                cout << "\t\t\t\t\tPlease Enter the valid Input!";
                cin >> player2_rowNum;
            }
            cout << "\t\t\t\t\t=>Enter column number:";
            cin >> player2_colNum;
            while (player2_colNum < 1 || player2_colNum > 3)
            {
                cout << "\t\t\t\t\tInvalid:input!" << endl;
                cout << "\t\t\t\t\tPlease Enter the valid Input!";
                cin >> player2_colNum;
            }
            // already used index validation
            if (board[player2_rowNum - 1][player2_colNum - 1] == '*')
            {
                board[player2_rowNum - 1][player2_colNum - 1] = 'O';
            }
            else
            {
                while (board[player2_rowNum - 1][player2_colNum - 1] != '*')
                {
                    cout << "\t\t\tThis index is already used please enter the valid index:" << endl;
                    cout << endl;
                    cout << "\t\t\t\t\t=>Enter row number:";
                    cin >> player2_rowNum;
                    while (player2_rowNum < 1 || player2_rowNum > 3)
                    {
                        cout << "\t\t\t\t\tInvalid:input!" << endl;
                        cout << "\t\t\t\t\tPlease Enter the valid Input!";
                        cin >> player2_rowNum;
                    }
                    cout << "\t\t\t\t\t=>Enter column number:";
                    cin >> player2_colNum;
                    while (player2_colNum < 1 || player2_colNum > 3)
                    {
                        cout << "\t\t\t\t\tInvalid:input!" << endl;
                        cout << "\t\t\t\t\tPlease Enter the valid Input!";
                        cin >> player2_colNum;
                    }
                }
                board[player2_rowNum - 1][player2_colNum - 1] = 'O';
            }

            // Logic-for-row-check_player_2
            for (int i = 0; i < row; i++)
            {
                if (board[i][0] == 'O')
                {
                    if ((board[i][0] == board[i][1]) && (board[i][0] == board[i][2]))
                    {
                        flag = true;
                        player2_win = true;
                    }
                }
            }

            // Logic-for-col-check_player_2
            for (int i = 0; i < col; i++)
            {
                if (board[0][i] == 'O')
                {
                    if ((board[0][i] == board[1][i]) && (board[0][i] == board[2][i]))
                    {
                        flag = true;
                        player2_win = true;
                    }
                }
            }

            // Logic-for-left_diagonal-check_player_2
            if (board[0][0] == 'O')
            {
                if ((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]))
                {
                    flag = true;
                    player2_win = true;
                }
            }

            // Logic-for-Right_diagonal-check_player_2
            if (board[0][2] == 'O')
            {
                if ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]))
                {
                    flag = true;
                    player2_win = true;
                }
            }
        }
        if (flag == false)
        {
            // To clear the screen on every updation
            system("cls");
        }

        // Display when player_1 wins or player_2 wins or tie
        if (match_tie == true || player2_win == true || player1_win == true)
        {
            // To clear the screen on every updation
            system("cls");
            // Display board contents
            for (int i = 0; i < 10; i++)
            {
                cout << endl;
            }
            for (int i = 0; i < row; i++)
            {
                cout << "\t\t\t\t\t-------------------" << endl;
                cout << "\t\t\t\t\t";
                for (int j = 0; j < col; j++)
                {
                    cout << "|  ";
                    cout << board[i][j] << "  ";
                }
                cout << "|";
                cout << endl;
            }
            cout << "\t\t\t\t\t-------------------" << endl;
        }

    } while (flag == false);

    // Player-1 wins display
    if (player1_win == true && player2_win == false)
    {
        for (int i = 0; i < 3; i++)
        {
            cout << endl;
        }
        cout << "\t\t\t\t\t*********Congratulations Player 1 win the game***************" << endl;
        for (int i = 0; i < 4; i++)
        {
            cout << endl;
        }
    }
    // Player-2 wins display
    if (player2_win == true && player1_win == false)
    {
        for (int i = 0; i < 3; i++)
        {
            cout << endl;
        }
        cout << "\t\t\t\t\t*********Congratulations Player 2 win the game***************" << endl;
        for (int i = 0; i < 4; i++)
        {
            cout << endl;
        }
    }

    // Match tie display
    if (match_tie == true)
    {
        for (int i = 0; i < 3; i++)
        {
            cout << endl;
        }
        cout << "\t\t\t\t\t******Match is Tie*****" << endl;
        for (int i = 0; i < 4; i++)
        {
            cout << endl;
        }
    }
    return 0;
}
                        
                    

Task 5. Write the code to create a 3D array of size 4 X 9 X 7. Later using three nested loops, populate the whole array with random numbers between 11 and 99. Later output the data in above array in the form of 4 stacked 2D arrays of size 9 X 7.


                        
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    const int row = 4;
    const int col = 9;
    const int z = 7;
    int matrix[row][col][z];
    //input
    for (int k = 0; k < z; k++)
    {
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                matrix[i][j][k] = (rand() % (99 - 11 + 1)) + 11;
            }
        }
    }
    // Display
    cout << "3D data :" << endl;
    for (int k = 0; k < z; k++)
    {
      cout<<"Stacked no "<<k+1<<" :"<<endl;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                cout << left;
                cout << setw(5) << matrix[i][j][k];
            }
            cout << endl;
        }
        cout << endl;
    }

    return 0;
}
                        
                    

Task 6. Create a 2D array with appropriate random data of size 12 rows and X columns, where X should be at least 5. The 12 rows represent the month number (0 to 11) and columns represent budget heads, e.g., travelling, eating, books and stationery, charity, mobile package, etc. Then compute the
a. Total budget of the year
b. The month with maximum budget
c. The total charity
d. The month with maximum of travelling


                        
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    const int row = 12;
    const int col = 7;
    int matrix[row][col];
    int total_Budget = 0;
    int month_budgetMax;
    int total_charity;
    int month_travelMax;
    int budget_everyMon[12] = {0};
    int index = 0;
    int index_travel = 0;
    for (int i = 0; i < row; i++)
    {
        matrix[i][0] = i + 1;
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 1; j < col; j++)
        {
            matrix[i][j] = (rand() % 1000 - 200 + 1) + 200;
            // calculate total_budget
            total_Budget += matrix[i][j];
        }
    }
    // calculate_Charity
    for (int i = 0; i < row; i++)
    {
        total_charity += matrix[i][5];
    }
    // calculate month_with_maximum_budget
    for (int i = 0; i < row; i++)
    {
        for (int j = 1; j < col; j++)
        {
            budget_everyMon[i] += matrix[i][j];
        }
    }
    month_budgetMax = budget_everyMon[0];
    for (int i = 0; i < row; i++)
    {
        if (budget_everyMon[i] > month_budgetMax)
        {
            month_budgetMax = budget_everyMon[i];
            index = i;
        }
    }
    // calculate month_with_Maximum_Travelling
    month_travelMax = matrix[0][1];
    for (int i = 0; i < row; i++)
    {
        if (matrix[i][1] > month_travelMax)
        {
            month_travelMax = matrix[i][1];
            index_travel = i;
        }
    }
    // display
    cout<<"*************************************YERALY BUGDET SYSTEM***********************************************"<<endl;
    cout << " _____________________________________________________________________________________________________" << endl;
    cout << "|Month #:    | Travelling   |   Eating    |    Books   |   Stationary   |   Charity   |Mobile Package:|" << endl;
    for (int i = 0; i < row; i++)
    {

        cout << "-------------------------------------------------------------------------------------------------------" << endl;
        for (int j = 0; j < col; j++)
        {
            cout << "|      ";
            cout << matrix[i][j] << "     ";
        }
        cout << "|";
        cout << endl;
    }
    cout << "-------------------------------------------------------------------------------------------------------" << endl;
    //=>display-data
    for (int i = 0; i < 3; i++)
    {
        cout << endl;
    }
    cout << "\t\t\tThe total budget of the year = " << total_Budget << endl;
    cout << "\t\t\tThe month with maximum budget = " << index+1 << " with the value of " << month_budgetMax << endl;
    cout << "\t\t\tThe total charity of the year = " << total_charity << endl;
    cout << "\t\t\tThe month with maximum travelling = " << index_travel+1 << " with the value of " << month_travelMax << endl;
    cout<<endl;

    return 0;
}
                        
                    

Whats make this website differents from others?

Amazing Feature That Increase your Productivity

If you don't find any problem,in this website and if you have a very difficult assignment question that is not present on internet ,then you dont bother yourself,we are here to solve every problem of your life you just click on the Problem asking button and write your problem we slove the code of your problem and sent you on your email addres that you put in the sin up form.