0
#pragma once

namespace My2DArray {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label1;
    protected: 
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::TextBox^  txtTable;

    private: System::Windows::Forms::Label^  label4;
    private: System::Windows::Forms::Label^  label5;
    private: System::Windows::Forms::Button^  btn2D;
    private: System::Windows::Forms::Label^  label6;
    private: System::Windows::Forms::TextBox^  rowSum1;

    private: System::Windows::Forms::TextBox^  rowSum2;
    private: System::Windows::Forms::TextBox^  colSum1;



    private: System::Windows::Forms::TextBox^  colSum4;

    private: System::Windows::Forms::TextBox^  colSum5;




    private: System::Windows::Forms::Label^  label7;
    private: System::Windows::Forms::Label^  label8;
    private: System::Windows::Forms::TextBox^  colSum2;

    private: System::Windows::Forms::TextBox^  colSum3;



    private: System::Windows::Forms::Label^  label9;
    private: System::Windows::Forms::TextBox^  rowSum3;




    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label3 = (gcnew System::Windows::Forms::Label());
            this->txtTable = (gcnew System::Windows::Forms::TextBox());
            this->label4 = (gcnew System::Windows::Forms::Label());
            this->label5 = (gcnew System::Windows::Forms::Label());
            this->btn2D = (gcnew System::Windows::Forms::Button());
            this->label6 = (gcnew System::Windows::Forms::Label());
            this->rowSum1 = (gcnew System::Windows::Forms::TextBox());
            this->rowSum2 = (gcnew System::Windows::Forms::TextBox());
            this->colSum1 = (gcnew System::Windows::Forms::TextBox());
            this->colSum4 = (gcnew System::Windows::Forms::TextBox());
            this->colSum5 = (gcnew System::Windows::Forms::TextBox());
            this->label7 = (gcnew System::Windows::Forms::Label());
            this->label8 = (gcnew System::Windows::Forms::Label());
            this->colSum2 = (gcnew System::Windows::Forms::TextBox());
            this->colSum3 = (gcnew System::Windows::Forms::TextBox());
            this->label9 = (gcnew System::Windows::Forms::Label());
            this->rowSum3 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(163, 34);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(51, 13);
            this->label1->TabIndex = 0;
            this->label1->Text = L"Column 1";
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(279, 34);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(51, 13);
            this->label2->TabIndex = 1;
            this->label2->Text = L"Column 2";
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(409, 34);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(51, 13);
            this->label3->TabIndex = 2;
            this->label3->Text = L"Column 3";
            // 
            // txtTable
            // 
            this->txtTable->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 29.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->txtTable->Location = System::Drawing::Point(91, 50);
            this->txtTable->Multiline = true;
            this->txtTable->Name = L"txtTable";
            this->txtTable->Size = System::Drawing::Size(644, 170);
            this->txtTable->TabIndex = 3;
            // 
            // label4
            // 
            this->label4->AutoSize = true;
            this->label4->Location = System::Drawing::Point(26, 61);
            this->label4->Name = L"label4";
            this->label4->Size = System::Drawing::Size(38, 13);
            this->label4->TabIndex = 4;
            this->label4->Text = L"Row 1";
            // 
            // label5
            // 
            this->label5->AutoSize = true;
            this->label5->Location = System::Drawing::Point(26, 106);
            this->label5->Name = L"label5";
            this->label5->Size = System::Drawing::Size(38, 13);
            this->label5->TabIndex = 5;
            this->label5->Text = L"Row 2";
            // 
            // btn2D
            // 
            this->btn2D->Location = System::Drawing::Point(343, 355);
            this->btn2D->Name = L"btn2D";
            this->btn2D->Size = System::Drawing::Size(88, 34);
            this->btn2D->TabIndex = 6;
            this->btn2D->Text = L"Make Table";
            this->btn2D->UseVisualStyleBackColor = true;
            this->btn2D->Click += gcnew System::EventHandler(this, &Form1::btn2D_Click);
            // 
            // label6
            // 
            this->label6->AutoSize = true;
            this->label6->Location = System::Drawing::Point(747, 34);
            this->label6->Name = L"label6";
            this->label6->Size = System::Drawing::Size(58, 13);
            this->label6->TabIndex = 7;
            this->label6->Text = L"Row Sums";
            // 
            // rowSum1
            // 
            this->rowSum1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->rowSum1->Location = System::Drawing::Point(750, 50);
            this->rowSum1->Multiline = true;
            this->rowSum1->Name = L"rowSum1";
            this->rowSum1->Size = System::Drawing::Size(64, 47);
            this->rowSum1->TabIndex = 8;
            // 
            // rowSum2
            // 
            this->rowSum2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->rowSum2->Location = System::Drawing::Point(750, 111);
            this->rowSum2->Multiline = true;
            this->rowSum2->Name = L"rowSum2";
            this->rowSum2->Size = System::Drawing::Size(64, 47);
            this->rowSum2->TabIndex = 9;
            // 
            // colSum1
            // 
            this->colSum1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->colSum1->Location = System::Drawing::Point(150, 238);
            this->colSum1->Multiline = true;
            this->colSum1->Name = L"colSum1";
            this->colSum1->Size = System::Drawing::Size(64, 47);
            this->colSum1->TabIndex = 10;
            // 
            // colSum4
            // 
            this->colSum4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->colSum4->Location = System::Drawing::Point(534, 238);
            this->colSum4->Multiline = true;
            this->colSum4->Name = L"colSum4";
            this->colSum4->Size = System::Drawing::Size(64, 47);
            this->colSum4->TabIndex = 11;
            // 
            // colSum5
            // 
            this->colSum5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->colSum5->Location = System::Drawing::Point(671, 238);
            this->colSum5->Multiline = true;
            this->colSum5->Name = L"colSum5";
            this->colSum5->Size = System::Drawing::Size(64, 47);
            this->colSum5->TabIndex = 12;
            // 
            // label7
            // 
            this->label7->AutoSize = true;
            this->label7->Location = System::Drawing::Point(547, 34);
            this->label7->Name = L"label7";
            this->label7->Size = System::Drawing::Size(51, 13);
            this->label7->TabIndex = 13;
            this->label7->Text = L"Column 4";
            // 
            // label8
            // 
            this->label8->AutoSize = true;
            this->label8->Location = System::Drawing::Point(668, 34);
            this->label8->Name = L"label8";
            this->label8->Size = System::Drawing::Size(51, 13);
            this->label8->TabIndex = 14;
            this->label8->Text = L"Column 5";
            // 
            // colSum2
            // 
            this->colSum2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->colSum2->Location = System::Drawing::Point(282, 238);
            this->colSum2->Multiline = true;
            this->colSum2->Name = L"colSum2";
            this->colSum2->Size = System::Drawing::Size(64, 47);
            this->colSum2->TabIndex = 15;
            // 
            // colSum3
            // 
            this->colSum3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->colSum3->Location = System::Drawing::Point(396, 238);
            this->colSum3->Multiline = true;
            this->colSum3->Name = L"colSum3";
            this->colSum3->Size = System::Drawing::Size(64, 47);
            this->colSum3->TabIndex = 16;
            // 
            // label9
            // 
            this->label9->AutoSize = true;
            this->label9->Location = System::Drawing::Point(26, 145);
            this->label9->Name = L"label9";
            this->label9->Size = System::Drawing::Size(38, 13);
            this->label9->TabIndex = 17;
            this->label9->Text = L"Row 3";
            // 
            // rowSum3
            // 
            this->rowSum3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->rowSum3->Location = System::Drawing::Point(750, 173);
            this->rowSum3->Multiline = true;
            this->rowSum3->Name = L"rowSum3";
            this->rowSum3->Size = System::Drawing::Size(64, 47);
            this->rowSum3->TabIndex = 18;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(817, 423);
            this->Controls->Add(this->rowSum3);
            this->Controls->Add(this->label9);
            this->Controls->Add(this->colSum3);
            this->Controls->Add(this->colSum2);
            this->Controls->Add(this->label8);
            this->Controls->Add(this->label7);
            this->Controls->Add(this->colSum5);
            this->Controls->Add(this->colSum4);
            this->Controls->Add(this->colSum1);
            this->Controls->Add(this->rowSum2);
            this->Controls->Add(this->rowSum1);
            this->Controls->Add(this->label6);
            this->Controls->Add(this->btn2D);
            this->Controls->Add(this->label5);
            this->Controls->Add(this->label4);
            this->Controls->Add(this->txtTable);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->label1);
            this->Name = L"Form1";
            this->Text = L"2D Array Program";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
        Random^ randomNumGen;

    private: System::Void btn2D_Click(System::Object^  sender, System::EventArgs^  e) {
                 // Declare Variables
                 const int numRows = 3;
                 const int numCols = 5;
                 int row, col; 
                 int total;
                 int arr2d[numRows][numCols] = {{5, 0, 8, 2, 1}, {6, 7, 6, 9, 8}, {1, 2, 3, 4, 5}};
                 int rowSum[] = {0 , 0};
                 String^ strOut = "";

                 // Display results
                 for (row = 0; row < numRows; row++)
                 {
                     for (col = 0; col < numCols; col++)
                         strOut += "         " + (arr2d[row][col]);
                     strOut += "\r\n";
                 }
problem below here ---------------------------------------------------------

我想创建一个循环(哪种对我来说都无关紧要)循环遍历每一列并总结每一列中的总数,然后在每一行中我的问题是我不知道什么样的循环和什么变量使用请帮忙!总计 = arr2d[0][0] + arr2d[1][0] + arr2d[2][0];colSum1->Text = total.ToString();

                 total = arr2d[0][1] + arr2d[1][1] + arr2d[2][1];
                 colSum2->Text = total.ToString();

                  total = arr2d[0][2] + arr2d[1][2] + arr2d[2][2];
                 colSum3->Text = total.ToString();

                 total = arr2d[0][3] + arr2d[1][3] + arr2d[2][3];
                 colSum4->Text = total.ToString();

                 total = arr2d[0][4] + arr2d[1][4] + arr2d[2][4];
                 colSum5->Text = total.ToString();

                 total = arr2d[0][0] + arr2d[0][1] + arr2d[0][2] + arr2d[0][3] + arr2d[0][4];
                 rowSum1->Text = total.ToString();

                 total = arr2d[1][0] + arr2d[1][1] + arr2d[1][2] + arr2d[1][3] + arr2d[1][4];
                 rowSum2->Text = total.ToString();

                 total = arr2d[2][0] + arr2d[2][1] + arr2d[2][2] + arr2d[2][3] + arr2d[2][4];
                 rowSum3->Text = total.ToString();

                 txtTable->Text = strOut;
             }

};
}
4

1 回答 1

0

首先学习for循环: http ://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

然后,您将需要知道网格的高度和宽度。从那里,您将不得不嵌套类似于以下的 for 循环:

textOutput;

//first for loop iterates through each row
for(int row = 0; row< height; row++)
{
    currentRowTotal = 0;

    //nested for loop iterates through each col
    for(int col = 0; col < width; col++)
    {
        //do stuff with the current rowTotal
    }
    //current row totaled, change text output
}

//then do it again for each column (inverse of above)
for(int col = 0; col< width; col++)
{
    currentColTotal = 0;
    for(int row = 0; col < height; row++)
    {
        //do stuff with the current colTotal
    }
    //current col totaled, change text output
}

值得注意的是,在 C++ 中,由于数据局部性,第一个函数比第二个函数快。这意味着,通常,迭代 2d|3d|4d|ect 的最后一部分。如果您希望它最快,请在最里面的 for 循环中使用数组。

于 2013-05-28T18:33:56.000 回答