简体   繁体   中英

How do I store user input into a multidimensional array? I'm working with a 3D array

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.

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

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.

   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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM