【问题标题】:How do I store user input into a multidimensional array? I'm working with a 3D array如何将用户输入存储到多维数组中?我正在使用 3D 数组
【发布时间】:2015-09-19 20:52:20
【问题描述】:

我了解数组的工作原理,尽管它们的尺寸很大,但如果没有这些基本信息,我无法将它组合到代码中。如您所知,我从第 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;
}

【问题讨论】:

  • 不清楚您要做什么,也不清楚您为什么要使用 3D 而不是 2D。
  • 为什么要使用 3D 数组,尤其是当您刚刚学习 C++ 时。您可能想查看struct 来保存数据而不是用于存储的多维数组。
  • 我是新手,所以不让上传图片,但是我的老师要求在这个作业中使用 3x3x2 数组。
  • postimg.org/image/sqj2qr6hp ------------------------- postimg.org/image/nwa8mkv2n 而且我们还没有学过结构。
  • 您的讲师是否覆盖了newdelete

标签: c++ arrays multidimensional-array user-input storing-data


【解决方案1】:

c++ 中的多维数组是 bit more complicated and require extra work,因此使用多维向量可能对您更有利。

   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

【讨论】:

  • 感谢您的尝试,但分配需要一个多维数组,我们还没有学习向量。我评论了一些图片链接,描述了我在最后一个回复中的任务。你能看看那些吗?我只需要知道如何将整数输入存储在多维数组中。
  • @TylerAverette 我包含的链接还解释了如何使用基于指针的 3-d 数组。看看那个 - 他们用它来存储双打。
【解决方案2】:

我昨晚想出了答案。如果有人感兴趣,这是更新的代码。感谢您尝试帮助所有人。

#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;
    }

【讨论】:

    猜你喜欢
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多