如何将用户输入存储到多维数组中?我使用的是一个3D数组。

时间:2021-12-02 01:22:31

I understand how Arrays work despite their dimensions, but I can't make it come together in code without this basic information. As you can tell I tried multiple ways starting with line 13, but they all failed.

我理解数组是如何工作的,尽管它们是维的,但如果没有这些基本信息,我就无法将它们组合在一起。从第13行开始,我尝试了多种方法,但都失败了。

#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main() 

{
    int MasterArray [3][3][2];        // Declaration of the Array
    int one = 0, two = 0, three = 0;  // Declaration of Variables

    cout << "Would you like to edit class 1 or 2?" << endl ; //Ask for input
    cin  >>  one;              // store input in the variable named one
    one -=1;                   // Since c++ is 0 based input - 1 = one
    MasterArray[3][3][2] = {{one, two, three}, {one, two, three}, {one, two}};                      
// Above line attempt to store variable one in array
// Rinse and repeat this process for lower lines, but storing is all that matters.

    cout << "Which student account would you like to access 1, 2, or 3?";
    cin  >> two;
    two -= 1;
    MasterArray[3][3][2] = [one][two][three];

    cout << "Which grade would you like to edit 1, 2, or 3?"; 
    cin  >> three;
    three -= 1;
    MasterArray[3][3][2] = [one][two][three];

    cout << MasterArray[one][two][three];
    return 0;
}

2 个解决方案

#1


0  

Multi-dimensional arrays in c++ are a bit more complicated and require extra work so using a multidimensional vector would probably be better for you.

c++中的多维数组有点复杂,需要额外的工作,所以使用多维向量可能对您更好。

   vector<vector<vector<int> > > yourVector;
   for(int i = 0; i<3; i++)
       for(int j = 0; j < 3; j++)
          for(int k = 0; k <3; k++)
                yourVector[i][j][k] = i + j +k


    cout << yourVector[1][2][3]; //prints 6

#2


0  

I figured out my answer last night. This is the updated code if anyone is interested. Thanks for trying to help everyone.

我昨晚找到了答案。如果有人感兴趣,这是最新的代码。谢谢你试着帮助每一个人。

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

int main()
{
    int one = 0, two = 0, three = 0, x = 0;
    int MasterArray [3][3][2] = {three, two, one};
    float A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0, A6 = 0;
    for (int i = 0;i <18; i++)
    {
    cout << "Enter a class number 1 or 2 : ";
    cin  >> one;
    one -= 1;
    cout << "Enter a student number 1, 2, or 3: ";
    cin  >> two;
    two -= 1;
    cout << "Enter the test number 1, 2, 3 : ";
    cin  >> three;
    three -= 1;
    cout << "Enter a grade for test number  " << three << ": ";
    cin  >> x;
    MasterArray[three][two][one] = x;
    cout <<"Class number: " << one + 1 << " Student number: " << two + 1 << " Test Number: " << three + 1 << " Grade: " << MasterArray[three][two][one] << endl << "\n";
    }
    //Math
    cout.precision(2), cout << fixed;
    A1 = MasterArray[0][0][0]*MasterArray[0][0][1]*MasterArray[0][0][2]/3;
    A2 = MasterArray[0][1][0]*MasterArray[0][1][1]*MasterArray[0][1][2]/3;
    A3 = MasterArray[0][2][0]*MasterArray[0][2][1]*MasterArray[0][2][2]/3;
    A4 = MasterArray[1][0][0]*MasterArray[1][0][1]*MasterArray[1][0][2]/3;
    A5 = MasterArray[1][1][0]*MasterArray[1][1][1]*MasterArray[1][1][2]/3;
    A6 = MasterArray[1][2][0]*MasterArray[1][2][1]*MasterArray[1][2][2]/3;

    cout << "Class" << setw(10) << "Student" << endl;
    cout << "Number" << setw(8) << "Number" << setw(8) <<"Grade 1" << setw(8) <<"Grade 2" << setw(8) <<"Grade 3" << setw(8) << "Average" << endl;
    // class 1
    cout << "1" << setw(8) << "1" << setw(8) << MasterArray[0][0][0] << setw(8) << MasterArray[0][0][1] << setw(8) << MasterArray[0][0][2] << setw(12) << A1 << endl;
    cout << setw(9) <<  "2" << setw(8) << MasterArray[0][1][0] << setw(8) << MasterArray[0][1][1] << setw(8) << MasterArray[0][1][2] << setw(12) << A2 << endl;
    cout << setw(9) <<  "3" << setw(8) << MasterArray[0][2][0] << setw(8) << MasterArray[0][2][1] << setw(8) << MasterArray[0][2][2] << setw(12) << A3 << endl;
    cout << "--------------------------------------------------------------------------------";

    // class 2
    cout << "2" << setw(8) << "1" << setw(8) << MasterArray[1][0][0] << setw(8) << MasterArray[1][0][1] << setw(8) << MasterArray[1][0][2] << setw(12) << A4 << endl;
    cout << setw(9) << "2" << setw(8) << MasterArray[1][1][0] << setw(8) << MasterArray[1][1][1] << setw(8) << MasterArray[1][1][2] << setw(12) << A5 << endl;
    cout << setw(9) << "3" << setw(8) << MasterArray[1][2][0] << setw(8) << MasterArray[1][2][1] << setw(8) << MasterArray[1][2][2] << setw(12) << A6 << endl;
    return 0;
    }

#1


0  

Multi-dimensional arrays in c++ are a bit more complicated and require extra work so using a multidimensional vector would probably be better for you.

c++中的多维数组有点复杂,需要额外的工作,所以使用多维向量可能对您更好。

   vector<vector<vector<int> > > yourVector;
   for(int i = 0; i<3; i++)
       for(int j = 0; j < 3; j++)
          for(int k = 0; k <3; k++)
                yourVector[i][j][k] = i + j +k


    cout << yourVector[1][2][3]; //prints 6

#2


0  

I figured out my answer last night. This is the updated code if anyone is interested. Thanks for trying to help everyone.

我昨晚找到了答案。如果有人感兴趣,这是最新的代码。谢谢你试着帮助每一个人。

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

int main()
{
    int one = 0, two = 0, three = 0, x = 0;
    int MasterArray [3][3][2] = {three, two, one};
    float A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0, A6 = 0;
    for (int i = 0;i <18; i++)
    {
    cout << "Enter a class number 1 or 2 : ";
    cin  >> one;
    one -= 1;
    cout << "Enter a student number 1, 2, or 3: ";
    cin  >> two;
    two -= 1;
    cout << "Enter the test number 1, 2, 3 : ";
    cin  >> three;
    three -= 1;
    cout << "Enter a grade for test number  " << three << ": ";
    cin  >> x;
    MasterArray[three][two][one] = x;
    cout <<"Class number: " << one + 1 << " Student number: " << two + 1 << " Test Number: " << three + 1 << " Grade: " << MasterArray[three][two][one] << endl << "\n";
    }
    //Math
    cout.precision(2), cout << fixed;
    A1 = MasterArray[0][0][0]*MasterArray[0][0][1]*MasterArray[0][0][2]/3;
    A2 = MasterArray[0][1][0]*MasterArray[0][1][1]*MasterArray[0][1][2]/3;
    A3 = MasterArray[0][2][0]*MasterArray[0][2][1]*MasterArray[0][2][2]/3;
    A4 = MasterArray[1][0][0]*MasterArray[1][0][1]*MasterArray[1][0][2]/3;
    A5 = MasterArray[1][1][0]*MasterArray[1][1][1]*MasterArray[1][1][2]/3;
    A6 = MasterArray[1][2][0]*MasterArray[1][2][1]*MasterArray[1][2][2]/3;

    cout << "Class" << setw(10) << "Student" << endl;
    cout << "Number" << setw(8) << "Number" << setw(8) <<"Grade 1" << setw(8) <<"Grade 2" << setw(8) <<"Grade 3" << setw(8) << "Average" << endl;
    // class 1
    cout << "1" << setw(8) << "1" << setw(8) << MasterArray[0][0][0] << setw(8) << MasterArray[0][0][1] << setw(8) << MasterArray[0][0][2] << setw(12) << A1 << endl;
    cout << setw(9) <<  "2" << setw(8) << MasterArray[0][1][0] << setw(8) << MasterArray[0][1][1] << setw(8) << MasterArray[0][1][2] << setw(12) << A2 << endl;
    cout << setw(9) <<  "3" << setw(8) << MasterArray[0][2][0] << setw(8) << MasterArray[0][2][1] << setw(8) << MasterArray[0][2][2] << setw(12) << A3 << endl;
    cout << "--------------------------------------------------------------------------------";

    // class 2
    cout << "2" << setw(8) << "1" << setw(8) << MasterArray[1][0][0] << setw(8) << MasterArray[1][0][1] << setw(8) << MasterArray[1][0][2] << setw(12) << A4 << endl;
    cout << setw(9) << "2" << setw(8) << MasterArray[1][1][0] << setw(8) << MasterArray[1][1][1] << setw(8) << MasterArray[1][1][2] << setw(12) << A5 << endl;
    cout << setw(9) << "3" << setw(8) << MasterArray[1][2][0] << setw(8) << MasterArray[1][2][1] << setw(8) << MasterArray[1][2][2] << setw(12) << A6 << endl;
    return 0;
    }