Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <fstream> using namespace std; class RGB { public:

ID: 2247104 • Letter: #

Question

#include <iostream>
#include <fstream>

using namespace std;

class RGB
{
public:
unsigned int r,g,b;
RGB(){
r = 0, g = 0, b = 0;
}
RGB(unsigned int red, unsigned int green, unsigned int blue){
r = red;
g = green;
b = blue;
}

unsigned int getred(){
return r;
}
unsigned int getgreen(){
return g;
}
unsigned int getblue(){
return b;
}


};

class Imageppm: public RGB
{
public:
unsigned int h,w,pixmap;
Imageppm(unsigned int height, unsigned int width)
{

h = height;
w = width;

pixmap = RGB[h];
for(unsigned int i = 0; i < w; i++){
pixmap[i] = RGB[w];
}
}
void writeppm(string filename){
ofstream out(filename.c_str());
out << "P3 " << endl;
out << "Created by " << endl;
out << h << " " << w << endl;
out << 255 << endl;
out << " " << endl;
for(int i = 0; i < h; h++){
for(int k = 0; k < w; k++){

}
}
}

};

int main()
{

return 0;
}

****************help c++ LINE 42 EXPECT PRIMARY EXPRESS BEFORE 44 INVALID TYPE UNSIGNED INT FOR ARRAY SUBSCRIP THANKS

Explanation / Answer

#include <string>

#include <iostream>
#include <fstream>

using namespace std;
//RBG is a class
class RGB {
    public:
        //constructor thet initialises red, grren , blue to 0 if no value is passed
        //default values are 0

        RGB(unsigned char red = 0, unsigned char green = 0, unsigned char blue = 0)
            : r(red), g(green), b(blue) {}

       //public methods that returns red
        unsigned char red() {
            return r;
        }
       //public methods that returns green
        unsigned char green() {
            return g;
        }
         //public methods that returns blue
        unsigned char blue() {
            return b;
        }

    private:
        //r,g, b are variables of type unsigned character
        unsigned char r, g, b;
};

//Class PPMImage
class PPMImage {
    public:
        //Constructor that initalises pixmap which hold address of RGB objects initialised by new
        PPMImage(unsigned int width, unsigned int height): w(width), h(height) {
            pixmap = new RGB*[h]; //We created a RGB object of size h
            //Loop will run from 0 to w-1 and in this way
            //it creates a 2D array where each row is of size w and column is of size h

            for (unsigned int i = 0; i < w; ++i)
                pixmap[i] = new RGB[w]; //Created a pixmap[w][h] its size is wxh
        }
        //Its a destructor that is used to destroy objects created by constructor
        ~PPMImage() {
            //The way we created objects , Similary we need to destroy them
            //Loop from 0 to w-1 and keep on deleting using delete opearor
            //delete[] pixmap[i] will delete ith position of size w

            for (unsigned int i = 0; i < w; i++)
                delete[] pixmap[i];                                   
      
            delete[] pixmap;
            w = h = 0;
        }

        void put(unsigned int y, unsigned int x, RGB rgb) {
            if (x > w || y > h)
                return;

            pixmap[y][x] = rgb;
        }

        bool printToFile(string filename) {
           //ofstream is a utilyty in C++ that is used to output data in a file
            ofstream out(filename.c_str());
            if (!out)
                return false;
            //<< operator is used to append data in file if we use outstream
            //P3 is appended to file
            out << "P3" << endl;
            //w and then h is appended to file
            out << w << " " << h << endl;
            //255 is appended to file
            out << 255 << endl;
            //We run the nested loop , Outer loop runs from 0 to h-1
            //Inner loop runs from 0 to w-1
            //pixmap[i][j] is assigned to variable of class Type and then corresponding functions
            //of RGB class are called

            for (unsigned int i = 0; i < h; ++i) {
                for (unsigned int j = 0; j < w; ++j) {
                    RGB &rgb = pixmap[i][j];
                  //R,G AND B ARE appended to file in integer form
                    out << (int)rgb.red() << " " << (int)rgb.green() << " " << (int)rgb.blue() << endl;
                }
            }

            return true;
        }


    private:

        unsigned int w, h;
        RGB **pixmap;
};

int main() {
    //w and h are initialised to 100
    int w = 100, h = 100;
    //img is an object and when we do PPMImage img(w, h), Corresponding constructor of
    //PPMImage is called.

    PPMImage img(w, h);  
    //We run the nested loop , Outer loop runs from 0 to h-1
    //Inner loop runs from 0 to w-1

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            //Object of rbg is created and default constructor is called and r,g,b as 0
            RGB rgb;
            //Based on i and j manipulation
            if (i / 10 % 2 == j / 10 % 2)
                // rgb is instantiated as to 255 as red, 0 as blue and green
                rgb = RGB(255, 0, 0);
            else
            //otherwise rgb is instantiated as to 0 as red, 0 as green and 255 blue
            rgb = RGB(0, 0, 255);

            img.put(i, j, rgb);
        }
    }

    string filename = "img.ppm";
    if (img.printToFile(filename))
        cout << "Succesfully create image!" << endl;
    else cout << "Couldn't write to file '" << filename << "'..." << endl;

    return 0;
}