C++并行数组



在这个并行数组中,我看不到或不明白为什么要打印内存地址而不是输入的值。我请求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 Xn";
   cout << "Quartily Reports for the six Entitiesn";
   cout << "Showing you the Highest Average out of the sixn";
   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;
}

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

double entry = theEntriesArray;

您在初始化数组的函数中遇到了一些问题,现在可以读取指针了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 Xn";
    cout << "Quartily Reports for the six Entitiesn";
    cout << "Showing you the Highest Average out of the sixn";
    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

相关内容

  • 没有找到相关文章

最新更新