[英]How do I store user input into a multidimensional array? I'm working with a 3D array
我了解数组的大小如何工作,但是如果没有这些基本信息,我就无法在代码中将它们组合在一起。 如您所知,我从第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;
}
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
昨晚我想出了答案。 如果有人感兴趣,这是更新的代码。 感谢您尝试帮助所有人。
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.