In: Computer Science
In this project you will implement the DataFrame class along
with the all the methods that are represented in the class
definition. DataFrame is a table with rows and columns – columns
and rows have names associated with them also. For
this project we will assume that all the values that are stored in
the columns and rows are integers.
After you have tested your class, you have to execute the main
program that is also given below.
DataFrame Class
#include
using namespace std;
class DataFrame {
protected:
int** table;
int noRows, noCols;
char** colNames;
char** rowNames;
public:
//Constructors
DataFrame ();
DataFrame (int rows, int cols);
//Output Method
void display();
//Setters
void setRowName(int row, char* name);
void setColName(int col, char* name);
int* operator[] (int i); //get row i;
//Getters
char** getRowNames();
char** getColNames();
int getNumberRows();
int getNumberCols();
DataFrame* getColumns(int* columns, int cLen);
DataFrame* getRows(int* rows, int rLen);
DataFrame* getRowsCols(int* rows, int rLen, int* cols, int
cLen);
//Destructor
~DataFrame();
};
The main function
int main () {
int c, r;
int selectC[3];
int selectR[10];
// Read the dataframe from input
// First line: two numbers seperated by space;
// first number is the number of rows (r) and
// second number is the number of columns (c)
cin >> r >> c;
DataFrame* firstDF = new DataFrame(r,c);
// Second line: strings seperated by a comma (c of them);
representing column names
// Third line: strings seperated by a comma (r of them);
representing row names
// Fourth line and more: c number of integers in each of the r rows
(seperated by)
// a space between integers in the same row.
// TODO: Student completes code for the above comment block to get
them as input
// using the display method, print (in the same format as the
input):
// - the column names of the dataframe
// - the row names of the dataframe
// - the contents of the table in dataframe
// TODO: Student completes code for the above comment block
// Execute the following code
// Read the column numbers that you want to extract
for (int i=0; i < 3; i++)
cin >> selectC[i];
DataFrame* tempColumns = (*firstDF).getColumns(selectC, 3);
(*tempColumns).display();
// Change the row names of select rows
(*tempColumns).setRowName(2, "Jack");
(*tempColumns).setRowName(3, "Peter");
(*tempColumns).display();
// Read the row numbers that you want to extract
for (int i=0; i < 10; i++)
cin >> selectR[i];
DataFrame* tempRows = (*firstDF).getRows(selectR, 10);
(*tempRows).display();
// Change the column names of selected columns
(*tempRows).setColName(2, "Scores");
(*tempRows).setColName(3, "Values");
(*tempRows).display();
// Extract the rows in SelectR and columns in SelectC
DataFrame* tempColsRows = (*firstDF).getRowsCols(selectR, 10,
selectC, 3);
(*tempColsRows).display();
delete tempRows;
delete tempColumns;
delete tempColsRows;
// Sample Code for you and you must execute this
DataFrame* myTable = new DataFrame(5,5);
for (int i =0; i < 5; i++) {
for (int j=0; j < 5; j++) {
(*myTable)[i][j] = i*j;
}
}
(*myTable).display();
delete myTable;
}
CODE:
#include <iostream>
using namespace std;
class DataFrame {
public:
int** table;
int noRows,
noCols;
char** colNames;
char** rowNames;
public:
DataFrame () {
table=NULL;
noRows=0;
noCols=0;
colNames=NULL;
rowNames=NULL;
}
DataFrame (int rows,
int cols) {
noRows=rows;
noCols=cols;
}
void display() {
cout<<noRows<<" "<<noCols<<endl;
for(int i=0;i<noCols;i++)
cout<<colNames[i]<<" ";
cout<<endl;
for(int i=0;i < noRows;i++)
cout<<rowNames[i]<<"";
cout<<endl;
for(int i=0;i<noRows;i++)
for(int j=0;j<noCols;j++)
cout<<table[i][j]<<" ";
cout<<endl;
}
void setRowName(int row,
char* name) {
rowNames[row]=name;
}
void setColName(int
col, char* name) {
colNames[col]=name;
}
int* operator[] (int
i){
return table[i];
}
char** getRowNames()
{
return rowNames;
}
char** getColNames()
{
return colNames;
}
int getNumberRows()
{
return noRows;
}
int getNumberCols()
{
return noCols;
}
DataFrame*
getColumns(int* columns, int cLen) {
DataFrame *d=new
DataFrame(cLen,sizeof(columns)/sizeof(*columns));
for(int i=0;i<sizeof(columns)/sizeof(*columns);i++)
d->setColName(i,colNames[columns[i]]);
for(int i=0;i<cLen;i++)
d->setRowName(i,rowNames[i]);
for(int i=0;i<cLen;i++)
for(int j=0;j<sizeof(columns)/sizeof(*columns);j++)
d->table[i][j]=table[i][columns[j]];
return d;
}
DataFrame*
getRows(int* rows, int rLen) {
int len=sizeof(rows)/sizeof(* rows);
DataFrame *d=new DataFrame(len,rLen);
for(int i=0;i<noCols;i++)
d->setColName(i,colNames[i]);
for(int i=0;i<rLen;i++)
d->setRowName(i,rowNames[rows[i]]);
for(int i=0;i<noCols; i++)
for(int j=0;j<len;j++)
d->table[i][j]=table[rows[j]][i];
return d;
}
DataFrame*
getRowsCols(int* rows, int rLen, int* cols, int cLen) {
DataFrame *d=new DataFrame(rLen,cLen);
for(int i=0;i<cLen;i++)
d->setColName(i,colNames[cols[i]]);
for(int i=0;i<rLen;i++)
d->setRowName(i,rowNames[rows[i]]);
for(int i=0;i<rLen;i++)
for(int j=0;j<cLen;j++)
d->table[i][j]=table[rows[i]][cols[j]];
return d;
}
~DataFrame() {
delete table;
delete rowNames;
delete colNames;
}
};
int main () {
int c, r;
int selectC[3];
int selectR[10];
cin >> r >>
c;
DataFrame* firstDF =
new DataFrame(r,c);
char* name;
int ele;
for(int i=0;i<c;i++)
{
cin>> name;
firstDF->setColName(i,name);
}
for(int
i=0;i<r;i++) {
cin>> name;
firstDF->setRowName(i,name);
}
for(int
i=0;i<r;i++){
for(int j=0;j<c;j++) {
cin>>ele;
firstDF->table[i][j]=ele;
}
}
return 0;
}