创建具有x y坐标的动态2D数组,例如图形

时间:2019-01-21 12:19:44

标签: arrays c++11

在显示从左下角开始(x,y)=(0,0)的2D数组时,我需要帮助。

这是我到目前为止所拥有的,我无法将左下角设为(0,0)。我拥有的是

x1y1,x1y2,x1y3,x1y4,x1y5,

x2y1,x2y2,x2y3,x2y4,x2y5,

x3y1,x3y2,x3y3,x3y4,x3y5,

x4y1,x4y2,x4y3,x4y4,x4y5,

x5y1,x5y2,x5y3,x5y4,x5y5,

我想拥有

x1y5,x2y5,x3y5,x4y5,x5y5,

x1y4,x2y4,x3y4,x4y4,x5y4,

x1y3,x2y3,x3y3,x4y3,x5y3,

x1y2,x2y2,x3y2,x4y2,x5y2,

x1y1,x2y1,x3y1,x4y1,x5y1,

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

string ** array2D = nullptr;

void init2DArray(int, int);
void populate2DArray (int, int);
void display2DArray (int, int);
void safelyDeallocateMemory (int, int);

int main() {
   int row, col;
   row = col = 0;
   cout << "Pls enter no. of cols : ";
   cin >> col;
   cout << endl;

   cout << "Pls enter no. of rows : ";
   cin >> row;
   cout << endl;

    init2DArray (col, row);
    populate2DArray (col, row);
    display2DArray (col, row);
    safelyDeallocateMemory (col, row);

    cout << endl;

  return 0;
 }

void init2DArray (int col, int row) {
        array2D = new string * [row];

        for (int i = 0; i < row; i++)
              array2D [i] = new string [col];
}

void populate2DArray (int col, int row) {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {

                ostringstream oss;
                oss << "x" << i + 1 << "y" << j + 1;

                array2D [i][j] = oss.str();
             }
         }
  }

void display2DArray (int col, int row) {
        cout << endl;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                cout << array2D [i][j] << ", ";
            }
            cout << endl;
        }
 }

1 个答案:

答案 0 :(得分:0)

df1 = df.groupby(pd.Grouper(freq='D')).agg({'var1': 'mean','var2': ['mean', 'size']}) 
df1.columns = df1.columns.map('_'.join)
df1 = df1.rename(columns={'var2_size':'count'})
print (df1)
            var1_mean  var2_mean  count
2012-04-05   3.992166   4.968410      3
2012-04-06   6.843105   6.193568      5
2012-04-07   4.568436   3.135089      1

您应该做一些小事情。将字符串推入r = df.resample('D') df1 = r.mean().add_suffix('_mean').join(r.size().rename('count')) print (df1) var1_mean var2_mean count 2012-04-05 7.840487 6.885030 3 2012-04-06 4.762477 5.091455 5 2012-04-07 2.702414 6.046200 1 时,必须将for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { ostringstream oss; oss << "x" << j + 1 << "y" << (row - i); array2D [i][j] = oss.str(); } } 附加到oss上,因为首先要执行内部循环。对于j + 1,您只需要将x更改为y;由于formula(row - i)开始,您可以拥有i。如果05, 4, 3, 2, 1,则您有row,则下一次迭代您将有5,并且如此。