pointers - C++ Parallel Arrays -
in parallel array can not see or figure out why printing memory address instead of values entered. ask c++ gods enlighten me , point me tutorials issue can learn from. code below.
#include <iostream> #include <ctime> #include <cstdlib> #include <iomanip> #include <string> #include <ios> using namespace std; // assign constant const int rowsize = 6; const int columnsize = 4; void displaywelcome() { cout << "\tcorporation x\n"; cout << "quartily reports 6 entities\n"; cout << "showing highest average out of six\n"; cout << endl; } // assign words in row of 6 string* entitiesarray(string(entities)[rowsize]) { string entitiesname[rowsize] = {"east ","west ","central ","midwest" ,"south ","south east "}; //string entitiesname; for(int i=0; < rowsize; i++) { cout << entitiesname[i] << endl; } return entitiesname; } // assign numbers 6x4 double* entriesarray(double(corparray)[rowsize][columnsize]) { // assign random numbers array srand(time(0)); double rowsum = 0.0, average = 0.0; (int r = 0; r < rowsize; ++r) { rowsum = 0; (int c = 0; c < columnsize; ++c) { corparray[r][c] = rand() % 101; cout << setw(5) << left << corparray[r][c] << " "; rowsum += corparray[r][c]; average = rowsum / 4; } cout << average; cout << endl; } return 0; } // parallel array prints memory address void parray(string &theentitiesarray,double &theentriesarray) { string entity = &theentitiesarray; double entry = &theentriesarray; for(int row_index = 0; row_index < 6; row_index++) { cout << setw(10) << entity <<endl; (int col_index = 0; col_index < 4; col_index++) { cout << setw(10) << entry << endl; } } } int main(int argc, const char * argv[]) { // declare array & variables double corparray[rowsize][columnsize]; string entities[rowsize]; double* theentriesarray; string* theentitiesarray; char parallelarray; displaywelcome(); theentitiesarray = entitiesarray(entities); theentriesarray = entriesarray(corparray); parray(*theentitiesarray, *theentriesarray); cout << endl; return 0; }
@jsf beat me in comment, in line double entry = &theentriesarray;
, you're assigning address of theentriesarray
variable entry
, rather numerical value refers to. change to:
double entry = theentriesarray;
Comments
Post a Comment