C++ 如何将用户输入存储到多维数组中?I';我正在使用3D阵列
我理解数组是如何工作的,尽管它们的维度不同,但如果没有这些基本信息,我无法让它们在代码中组合在一起。正如你所知,我从第13行开始尝试了多种方法,但都失败了C++ 如何将用户输入存储到多维数组中?I';我正在使用3D阵列,c++,arrays,multidimensional-array,user-input,storing-data,C++,Arrays,Multidimensional Array,User Input,Storing Data,我理解数组是如何工作的,尽管它们的维度不同,但如果没有这些基本信息,我无法让它们在代码中组合在一起。正如你所知,我从第13行开始尝试了多种方法,但都失败了 #include <iostream> #include <math.h> #include <string> using namespace std; int main() { int MasterArray [3][3][2]; // Declaration of the Ar
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int MasterArray[3][3][2];//数组的声明
int one=0,two=0,three=0;//变量声明
cout one;//将输入存储在名为one的变量中
1=1;//因为C++是基于0的输入- 1=1
主数组[3][3][2]={{1,2,3},{1,2,3},{1,2};
//行上方尝试在数组中存储变量1
//冲洗并对较低的管线重复此过程,但储存才是最重要的。
不能超过两个;
2-=1;
主阵列[3][3][2]=[1][2][3];
不能超过三个;
3-=1;
主阵列[3][3][2]=[1][2][3];
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
vector你的vector;
对于(int i=0;i我昨晚算出了答案。如果有人感兴趣,这是更新的代码。感谢您尝试帮助大家
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int一=0,二=0,三=0,x=0;
int MasterArray[3][3][2]={3,2,1};
浮动A1=0,A2=0,A3=0,A4=0,A5=0,A6=0;
对于(int i=0;i 1;
1-=1;
不能超过两个;
2-=1;
不能超过三个;
3-=1;
CUT,你不知道你在做什么,也不知道为什么要用3D而不是2D。为什么使用3D数组,尤其是当你正在学习C++的时候。你可能想查看<代码> Stutt保存数据而不是多维数组来存储。它不会让我上传图片,因为我是新的,但是我的导师需要一个3x3x2数组。本作业。---------------------------此外,我们还没有学习结构。您的讲师是否介绍了新建
和删除
?感谢您的尝试,但作业需要多维数组,我们也没有学习向量。我对上一次响应中描述我的分配的一些图像链接进行了注释。请看一下。我需要知道的是如何在多维数组中存储整数输入。@TylerAverette我包含的链接还解释了如何使用基于指针的三维数组。请看一下-他们使用它来存储双精度。