Question:
C++ assignment multidimensional array help?
James
2010-11-28 16:08:14 UTC
For my assignment, we are given a 6x4 array in a text file. How can i bring up all of these values and find the average. When i try to do it, every time i rerun, the average number keeps increasing, and the value for the first time isnt even correct. Also, i need to be able to allow the user to change any of the values in the array. So i need to be able to ask which index location they want to change and then ask for new value. All of this has to be using functions too. How would you ask for location to change in the array, and pass those values to the function? Thanks for your help.
Three answers:
MichaelInScarborough
2010-12-01 21:40:42 UTC
/* The following code uses vectors in order to obtain data from a file (which hopefully has sufficient data) and writes this data, when your data manipulation is over to the vector and then to a file. As you can see the array logic is redundant and not needed, but complies with your assignment. I did not do the full program. What you still have to do is to read the coordinates and to assign the values in the format you pointed out in your additional details. In case you have any additional questions, feel free to email me. All the best

*/

#include

#include

#include

#include

#include



using namespace std;

const int ciWidth = 4;

const int ciOne = 4;

const int ciTwo = 6;



void write_vector(const string& strFile, const vector &v)

{

fstream fs(strFile, ifstream::out);

copy(v.begin(), v.end(), ostream_iterator(fs, " "));

}



void read_vector(const string& strFile, vector& v)

{

fstream fs(strFile, ifstream::in);

copy(istream_iterator(fs),istream_iterator(), back_inserter(v));

}



void read_4x6(const vector & v, int ar[ciOne][ciTwo])

{

for (int i = 0; i < ciOne; i++) {

for (int j = 0; j < ciTwo; j++) {

ar[i][j] = v[i * ciOne + j];

}

}

}



void write_4x6(vector & v, const int ar[ciOne][ciTwo])

{

for (int i = 0; i < ciOne; i++) {

for (int j = 0; j < ciTwo; j++) {

v[i * ciOne + j] = ar[i][j];

}

}

}



void display_array(int ar[ciOne][ciTwo])

{

for (int i = 0; i < ciOne; i++) {

for (int j = 0; j < ciTwo; j++) {

cout << setw(ciWidth) << right << ar[i * ciOne + j];

}

cout << "\n";

}

}



int get_number(int ar[ciOne][ciTwo])

{

int x, y, i;

do {

x = -1; y = -1; i = -1;

display_array(ar);

// Here yo have to perform the checking of the indices and values input by the user i.e.:

cout << "Enter x and y and a value: (-1 for x quits) > ";

cin >> x >> y >> i;

if (x == -1) return -1;

// after checking, etc

ar[x][y] = i;

}

return x;

}



int main()

{

vector v;

int ar[ciOne][ciTwo];

read_vector("numbers.dat", v);

read_4x6(v, ar);

do {

if (get_number() == -1) break;

} while(true);

// after all your number manipulation is done, the vector is filled using write_4x6(v,ar) and written to the file using write_vector("numbers.dat", v);

write_4x6(v, ar);

write_vector("numbers.dat", v);

return 0;

}
JoelKatz
2010-11-28 16:12:45 UTC
Paste your code. There's no way we can make suggestions to fix code we can't see.



If the array is always six by four, then you can pretty easily print out the array with headings, like this:





.........1.............2............3.............4

A......17...........23...........34...........57

B......14....



Use spaces instead of dots. I'm just using dots because of Y!A reformatting. Then ask person for a cell name like 'B2' and the new value. (Or follow the form used in the example run if the problem included one.)



Update: Yes, basically. Post some code with comments for the code you're not sure how to do or your best guess. We will wind up making things harder for you if we push you in a direction you are not comfortable going because then you will have the added work of trying to understand how we planned to do it.
farley
2016-10-18 12:53:04 UTC
the easy answer is. char c[] and char* c are in truth equivalent. once you define char c[10], you're allocating 10 bytes, beginning at address c. getting access to c[5] as an occasion must be achieved by asserting *(c+5). whilst char c[10][10] is defined, you're coming up, at address c, 10 new regulations, each of which element to an array of 10 bytes. the cost in c[a million] is a pointer to the ten bytes in the 2d length of the array. An equivalent occasion to the a million dimensional array, you need to in many circumstances get right of entry to the char at c[4][6]. The pointer technique could be to declare *(*(c+4)+6); defining char* sunday = "sunday" is analagous to defining char sunday[] = { "sunday" }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...