c++ 如何写一个循环来改变一个二维数组的元素?

nfeuvbwi  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(149)

问题是制作座位表。用户输入数组的一行和一列来选择座位。选择座位后,用X标记位置。重复直到所有座位都被占满,然后结束程序。
我创建了数组,能够选择座位,并用x标记它们。我在创建循环以重复该过程时遇到了麻烦。
这是我的资料。

#include <iostream> 
#include <iomanip>
#include <string>
using namespace std;

int main() {

    //variables

    const int row = 8, col = 10;
    int r = 0, c = 0;
    

    //1. Create an array 

    //title
    cout << "   Available Theater Seating\n\n";

    //create while loop to check for available seats (non X elements)

    string seats[row][col] =
    {   {"$30 ", "$40 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$40 ", "$30"},
        {"$20 ", "$30 ", "$30 ", "$40 ", "$50 ", "$50 ", "$40 ", "$30 ", "$30 ", "$20"},
        {"$20 ", "$20 ", "$30 ", "$30 ", "$40 ", "$40 ", "$30 ", "$30 ", "$20 ", "$20"},
        {"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
        {"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
        {"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
        {"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"},
        {"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"} };


    
    //if (seats > X) {
        //display array
    //do {
        for (int row = 7; row >= 0; row--)
        {
            for (int col = 0; col <= 9; col++)
            {
                cout << seats[row][col];
            }
            cout << "\n";
        }

        //prompt for and get row/column
        cout << "\nPick a seat by row and then column.\n\n";
        cout << "Row: ";
        cin >> r;
        cout << "Column: ";
        cin >> c;

        seats[r - 1][c - 1] = " X  ";

        

        //repeat of display array to show the new x. Delete after making while loop
        for (int row = 7; row >= 0; row--)
        {
            for (int col = 0; col <= 9; col++)
            {
                cout << seats[row][col];
            }
            cout << "\n";
        }
    //} while (seats[][] !"X")
    
    
    return 0;

}

这是我运行到目前为止得到的结果。

Available Theater Seating

$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$20 $20 $30 $30 $40 $40 $30 $30 $20 $20
$20 $30 $30 $40 $50 $50 $40 $30 $30 $20
$30 $40 $50 $50 $50 $50 $50 $50 $40 $30

Pick a seat by row and then column.

Row: 3
Column: 4
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$20 $20 $30  X  $40 $40 $30 $30 $20 $20
$20 $30 $30 $40 $50 $50 $40 $30 $30 $20
$30 $40 $50 $50 $50 $50 $50 $50 $40 $30
bvn4nwqk

bvn4nwqk1#

在乔的建议下,我添加了一个单独的座位计数器。当座位数等于总座位数时,程序将结束。以下是我的结果。

#include <iostream> 
#include <iomanip>
#include <string>
using namespace std;

int main() {

//variables

const int row = 8, col = 10; //array size
int r = 0, c = 0;            //input variables
const int totalSeats = row * col;   //total available seats
int seatsTaken = 0;          //count for seats taken

//1. Create an array 

//title
cout << "   Available Theater Seating\n\n";

//starting array

string seats[row][col] =
{   {"$30 ", "$40 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$40 ", "$30"},
    {"$20 ", "$30 ", "$30 ", "$40 ", "$50 ", "$50 ", "$40 ", "$30 ", "$30 ", "$20"},
    {"$20 ", "$20 ", "$30 ", "$30 ", "$40 ", "$40 ", "$30 ", "$30 ", "$20 ", "$20"},
    {"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
    {"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
    {"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
    {"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"},
    {"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"} };

//start loop
do {
    for (int row = 7; row >= 0; row--)  //2.count backwards to make the top of the array the bottom "front of theater"
    {
        for (int col = 0; col <= 9; col++)
        {
            cout << seats[row][col];     //actual display of array
        }
        cout << "\n";
    }

    //3.,4.prompt for and get row/column
    cout << "\nPick a seat by row and then column.\n\n";
    cout << "Row: ";
    cin >> r;
    cout << "Column: ";
    cin >> c;
    cout << "\n";

    //5.if seat is taken, give error code
    if (seats[r - 1][c - 1] == " X  ") {
        cout << "Unavailable. Try again.\n";
    }
    //if seat is available, mark it with an x and increase count for seats taken
    else {
        seats[r - 1][c - 1] = " X  ";
        seatsTaken++;
    }
    //continue looping until seats taken = total seats
} while (seatsTaken != totalSeats);

    //6.,7.when all seats are taken, give sold out message and end program
    if (seatsTaken == totalSeats) {
        cout << "The theater is sold out.";
        return 0;
    }
}
eqqqjvef

eqqqjvef2#

由于要循环遍历整个数组并打印出来,因此可以计算出有多少个值是X。有一件事会使计算更容易,那就是不要将X存储为包含空格的字符串,可以稍后添加空格。
添加一个变量以跟踪哪些座位已被占用

const int totalSeats = rows * cols;
int seatsTaken = 0;

然后对于数组中的每个元素,计算所有占用的座位。

if (seats[row][col] == "X")
{
   cout << " " << seats[row][col] << "  ";
   seatsTaken++;
}
else
{
   cout << seats[row][col];
}

如果seatsTaken == totalSeats,则您将知道所有座位都已占用

相关问题