Lab3_Q1

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <fstream>; using namespace std; class charMatrix{ private: int xRes; int yRes; char **xtable; public: charMatrix(int xResolution,int yResolution, char bgChar); void setChar(int x,int y,char v); int getChar(int x,int y); ~charMatrix(); void prt(); void saveToFile(string fileName); }; charMatrix::charMatrix(int xResolution,int yResolution, char bgChar){ xRes = xResolution; yRes = yResolution; xtable = new char*[yRes]; for(int i=0;i<yRes;i++) { xtable[i] = new char[xRes]; } for(int i=0;i<yRes;i++){ for(int j=0;j<xRes;j++){ xtable[i][j] = bgChar; } } } charMatrix::~charMatrix(){ for (int i=0; i<yRes; i++) { delete[] xtable[i]; } delete [] xtable; } void charMatrix::setChar(int x,int y,char v){ xtable[y][x] = v; } int charMatrix::getChar(int x,int y){ return xtable[y][x]; } void charMatrix::prt(){ for(int i=0;i < yRes;i++){ for(int j=0;j < xRes;j++){ cout << getChar(j,i) << " "; } cout << endl; } } void charMatrix::saveToFile(string fileName){ ofstream pgmFile(fileName); pgmFile << "P2" << endl; pgmFile << xRes << " " << yRes << endl; pgmFile << 255 << endl; for(int i=0;i<yRes;i++){ for(int j=0;j<xRes;j++){ pgmFile << getChar(j,i) << " "; }// next column pgmFile << endl; }// next row // all done! pgmFile.close(); } int main() { int xR=25, yR=25, myChar='x'; charMatrix c(xR,yR,' '); // fill a 25x25 block with blanks for(int i=0;i < yR;i++) { for(int j=0;j < xR;j++) { // do c.setChar(j,i,myChar) based on a calculation involving i,j } } c.prt(); c.saveToFile("fun.txt"); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines