0

在这个并行数组中,我看不到或弄清楚为什么要打印内存地址而不是输入的值。求C++大神赐教,指点一些关于这个问题的好教程,以便我学习。代码如下。

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ios>

using namespace std;

// Assign a constant
const int rowSize = 6;
const int columnSize = 4;

void displayWelcome()
{
   cout << "\tCorporation X\n";
   cout << "Quartily Reports for the six Entities\n";
   cout << "Showing you the Highest Average out of the six\n";
   cout << endl;
}

// Assign Words in a row of 6
string* entitiesArray(string(entities)[rowSize])
{
    string entitiesName[rowSize] = {"East ","West ","Central ","Midwest"
      ,"South ","South East "};
    //string entitiesName;
    for(int i=0; i < rowSize; i++)
    {
        cout << entitiesName[i] << endl;
    }
    return entitiesName;
}

// Assign numbers to a 6x4
double* entriesArray(double(corpArray)[rowSize][columnSize])
{
    // Assign Random Numbers to the Array
    srand(time(0));
    double rowSum = 0.0, average = 0.0;

    for (int r = 0; r < rowSize; ++r)
    {
        rowSum = 0;
        for (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  This is where is prints the 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;
        for (int col_index = 0; col_index < 4; col_index++)
        {
            cout << setw(10) << entry << endl;
        }
    }
}

int main(int argc, const char * argv[])
{
    // Declare an 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;
}
4

2 回答 2

1

您在初始化数组的函数中存在一些问题,现在可以尝试阅读指针 1- http://www.cplusplus.com/doc/tutorial/pointers/

2- http://www.tutorialspoint.com/cplusplus/cpp_pointer_to_pointer.htm

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ios>

using namespace std;

// Assign a constant
const int rowSize = 6;
const int columnSize = 4;

void displayWelcome()
{
    cout << "\tCorporation X\n";
    cout << "Quartily Reports for the six Entities\n";
    cout << "Showing you the Highest Average out of the six\n";
    cout << endl;
}

// Assign Words in a row of 6
string* entitiesArray(string(entities)[rowSize])
{
    string* entitiesName = new string[rowSize];
    string temp[]= { "East ", "West ", "Central ", "Midwest"
        , "South ", "South East " };
    for (int i = 0; i < rowSize; i++)
    {
        entitiesName[i] = temp[i];
    }
    //string entitiesName;
    for (int i = 0; i < rowSize; i++)
    {
        cout << entitiesName[i] << endl;
    }
    return  entitiesName;
}

// Assign numbers to a 6x4
double** entriesArray(double(corpArray)[rowSize][columnSize])
{
    double** tempcorpArray = new double*[rowSize];// [columnSize];
    for (int i = 0; i < rowSize; ++i)
    {
        tempcorpArray[i] = new double[columnSize];
    }
    // Assign Random Numbers to the Array
    srand(time(0));
    double rowSum = 0.0, average = 0.0;

    for (int r = 0; r < rowSize; ++r)
    {
        rowSum = 0;
        for (int c = 0; c < columnSize; ++c)
        {
            tempcorpArray[r][c] = rand() % 101;
            cout << setw(5) << left << tempcorpArray[r][c] << " ";
            rowSum += tempcorpArray[r][c];
            average = rowSum / 4;
        }
        cout << average;
        cout << endl;
    }
    return tempcorpArray;
}

// Parallel Array  This is where is prints the 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[row_index]) << endl;
        for (int col_index = 0; col_index < 4; col_index++)
        {
            cout << setw(10) << (entry[row_index][col_index]) << endl;
        }
    }
}

int main(int argc, const char * argv[])
{
    // Declare an Array & Variables
    double corpArray[rowSize][columnSize];
    string entities[rowSize]; 
    char parallelArray;

    displayWelcome();
    string* theEntitiesArray = entitiesArray(entities);
    double** theEntriesArray = entriesArray(corpArray);

    pArray(theEntitiesArray, theEntriesArray);

    cout << endl; 
    return 0;
}

如果有任何问题,请随时询问:D

于 2015-07-14T13:45:43.153 回答
1

@JSF 在评论中击败了我,但在这一行double entry = &theEntriesArray;中,您将地址分配给theEntriesArray变量entry,而不是它所指的数值。将其更改为:

double entry = theEntriesArray;
于 2015-07-14T13:07:24.677 回答