在 C++ 中操作 2d 数组,并且无法遍历所有数据来创建新数组/遍历操作循环

问题描述 投票:0回答:1

我正在为 C++ 编写一个程序,该程序接受用户输入,第一行包含 1-9 之间的 n 个整数,该整数告知以下数据的网格大小。然后是具有 n 列和行的网格。接下来是一个整数 m ,详细说明数组上将发生多少个操作,其中每个操作都在下一行的 m 行中进行。我认为我在提取数据并告诉循环停止并再次重复方面遇到困难。我的代码如下:

#include <iostream>
#include <string>
#include <array>

using namespace std;
// GLOBAL CONSTANTS
const size_t MAX = 9;               /// Maximum dimension of a grid

// PROTOTYPES HERE
void swap_rows(int grid[][MAX], size_t a, size_t b, size_t size);
void swap_columns(int grid[][MAX], size_t a, size_t b, size_t size);
void increment_grid(int grid[][MAX], size_t size);
void decrement_grid(int grid[][MAX], size_t size);
void transpose_grid(int grid[][MAX], size_t size);

int main() {
    // Variable declaration
int case_num = 1;
int grid[MAX][MAX];
int n;                          // size of array taken from int
int m;
string op;

    // ** input *****************************************************
    do {
        cin >> n;
        cin.clear();
        if (n < 0 && n > 9){
            cin.fail();
        }
    // ** process ***************************************************
        for (size_t i = 0; i < MAX; i++){
            for (size_t j = 0; j < MAX; j++) {
                cin >> grid[i][j];            // take in data to make grid
            }
        }
        cin.clear();
        cin >> m;            // input num of operations

        // using for loop to apply operation to grid and print
        for (int i = 0; i < m; i++) {      //iterate through list m times
            cin >> op;
            if (op == "row") {
                int a;
                int b;
                cin >> a >> b;
                swap_rows(grid, a, b, n);    // for zero-based inputs
            } else if (op == "col"){
                int a;
                int b;
                cin >> a >> b;
                swap_columns(grid, a, b, n);
            } else if (op == "inc"){
                increment_grid(grid, n);
            } else if (op == "dec"){
                decrement_grid(grid, n);
            } else if (op == "transpose"){
                transpose_grid(grid, n);
            }
        }
    // ** Output ****************************************************
            cout << "Case #" << case_num << endl;
            case_num++;

            for (size_t i = 0; i < MAX; i++){        // print new array
                for (size_t j = 0; j < MAX; j++){
                    cout << grid[i][j];
                    cout << '\n';
                }
            }
            cout << endl;
        } while (cin.good());                                  // continue loop
    return 0;
}

/// @brief the function swaps two rows in a given grid.
///
/// @param [in,out] grid - the grid in which rows are to be swapped
/// @param a - the first row to be swapped (zero-based index)
/// @param b - the second row to be swapped (zero-based index)
/// @param size - The size of the grid (number of rows/ columns).
/// @return none
void swap_rows(int grid[][MAX], size_t a, size_t b, size_t size) {
    int temp;
    // go through every column
    for (size_t col = 0; col < size; col++) {
    // swap [a] [col] with [b][col] use temp variable to store a variable
        temp = grid[a][col];
        grid[a][col] = grid[b][col];
        grid[b][col] = temp;        // declare temp variable to [b]
    }

}

/// @brief the function swaps two columns in a given grid.
///
/// @param [in,out] grid - the grid in which columns are to be swapped
/// @param a - the first column to be swapped (zero-based index)
/// @param b - the second column to be swapped (zero-based index)
/// @param size - The size of the grid (number of rows/ columns).
/// @return none
void swap_columns(int grid[][MAX], size_t a, size_t b, size_t size) {
    int temp;
    // go through every row
    for (size_t row = 0; row < size; row++) {
    // swap [row] [a] with [row][b] use temp variable to store a variable
        temp = grid[row][a];
    // declare b variable to [a] declare temp variable to [b]
        grid[row][a] = grid[row][b];
        grid[row][b] = temp;
    // increase i and repeat loop until array is finished
    }
}

/// @brief the function increments each element in the grid by 1, wrapping 
/// around to 0 at 10
///
/// @param [in,out] grid - the grid to be incremented
/// @param size - The size of the grid (numbers of rows/columns
/// @return none
void increment_grid(int grid[][MAX], size_t size) {
    // go through every row in every column
    for (size_t i = 0; i < size; i++) {
        for (size_t j = 0; j < size; j++) {
            grid[i][j]++;       //increment the element

            if (grid[i][j] > 9) {
                grid[i][j] = 0;
            }
        }
    }
}

/// @brief the function decrements each element in the grid by 1, wrapping
/// around to 9 at -1
///
/// @param [in,out] grid - the grid to be decremented
/// @param size - The size of the grid (numbers of rows/columns
/// @return none
void decrement_grid(int grid[][MAX], size_t size) {
    // go through every row in every column
    for (size_t i = 0; i < size; i++) {
        for (size_t j = 0; j < size; j++) {
            grid[i][j]--;       //decrement the element

            if (grid[i][j] < 0) {
                grid[i][j] = 9;
            }
        }
    }
}

/// @brief the function transposes the given grid
///
/// @param [in,out] grid - the grid to be incremented
/// @param size - The size of the grid (numbers of rows/columns
/// @return none
void transpose_grid(int grid[][MAX], size_t size) {
    int temp;
    // loop should start at row 2, go through every row
    for (size_t i = 1; i < size; i++){
        for (size_t j = 1; j < i; j++){
            temp = grid[i][j];
            grid[j][i] = grid[i][j];
            grid[i][j] = temp;
        }
    }
}

我尝试更改循环变量以及 while 语句将保存的内容。我还尝试调试错误,因为数组难以选择正确的数据。

示例输入如下所示:

4
1234
5678
1234
5678
1
transpose
3
000
111
000
2
row 1 2
inc
0

我怎样才能更好地修复我的代码以产生正确的结果并正常运行

c++ arrays loops cin
1个回答
0
投票

阅读主要内容第一行后的一些评论:

if (n < 0 && n > 9)

我猜你的意思是|| :这个说法不可能

您的示例输入无效:大小 4 表示预期有 4 x 4 = 16 个输入的网格,您太早输入“转置”了...

   // ** process ***************************************************
        for (size_t i = 0; i < MAX; i++){
            for (size_t j = 0; j < MAX; j++) {
                cin >> grid[i][j];            // take in data to make grid
            }

此外,因为您要求大小(用户输入“n”变量),但在循环中使用 MAX...

这与我的第一条评论相关:我建议你让你的程序说出它期望的输入,以便你在使用调试器之前更好地理解正在发生的事情,这将是“我怎样才能更好地修复我的问题”的进一步步骤代码”,但我认为还没有必要。

另外,正如 @mikeb 所说,“阅读关于作业的公开信:https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems”

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.