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

C++ question Hi, I am trying to create a program that will run like this. with o

ID: 3749699 • Letter: C

Question

C++ question

Hi, I am trying to create a program that will run like this.

with output file showing

#include<iostream>

#include<fstream>

#include<cstdlib>

#include<string>

using namespace std;

typedef struct ppm {

string type;

int width;

int height;

int max;

int im[100][100][3];

}PPM;

void readImage(PPM &image, ifstream &ifs) {

ifs >> image.type;

if (image.type == "P1" || image.type == "P2" || image.type == "P3") {

ifs >> image.width >> image.height;

if (image.width > 1000 || image.height > 1000) {

cout << " Image Hieght or Width is out of Bound";

exit(-1);

}

ifs >> image.max;

for (int i = 0; i < image.height; i++) {

for (int j = 0; j < image.width; j++) {

ifs >> image.im[i][j][0] >> image.im[i][j][1] >> image.im[i][j][2];

}

}

}

else {

cout << " Image Type is not Valid";

exit(-1);

}

}

void convertGrayScale(PPM &image) {

if (image.type == "P2") {

return;

}

image.type = "P2";

int tot = 0;

for (int i = 0; i < image.height; i++) {

for (int j = 0; j < image.width; j++) {

tot = image.im[i][j][0] + image.im[i][j][1] + image.im[i][j][2];

image.im[i][j][0] = tot / 3;

image.im[i][j][1] = tot / 3;

image.im[i][j][2] = tot / 3;

}

}

}

void invertRed(PPM &image) {

for (int i = 0; i < image.height; i++) {

for (int j = 0; j < image.width; j++) {

image.im[i][j][0] = image.max - image.im[i][j][0];

}

}

}

void invertBlue(PPM &image) {

for (int i = 0; i < image.height; i++) {

for (int j = 0; j < image.width; j++) {

image.im[i][j][1] = image.max - image.im[i][j][1];

}

}

}

void invertGreen(PPM &image) {

for (int i = 0; i < image.height; i++) {

for (int j = 0; j < image.width; j++) {

image.im[i][j][2] = image.max - image.im[i][j][2];

}

}

}

void invertAll(PPM &image) {

for (int i = 0; i < image.height; i++) {

for (int j = 0; j < image.width; j++) {

image.im[i][j][0] = image.max - image.im[i][j][0];

image.im[i][j][1] = image.max - image.im[i][j][1];

image.im[i][j][2] = image.max - image.im[i][j][2];

}

}

}

void writeImage(PPM &image, ofstream &ofs) {

ofs << image.type << endl;

ofs << image.width << " " << image.height << endl;

ofs << image.max << endl;

for (int i = 0; i < image.height; i++) {

for (int j = 0; j < image.width; j++) {

ofs << image.im[i][j][0] << " " << image.im[i][j][1] << " " << image.im[i][j][2] << " ";

}

ofs << endl;

}

}

void menu() {

cout << " 1- Convert to GrayScale";

cout << " 2- Invert Red";

cout << " 3- Invert Green";

cout << " 4- Invert Blue";

cout << " 5- Invert All";

}

int main() {

PPM image;

string ofile;

string ifile;

ifstream ifs;

ofstream ofs;

cout << " Enter image file name (ppm): ";

cin >> ifile;

ifs.open(ifile.c_str());

if (!ifs.is_open()) {

cout << " In File Opening error ";

return -1;

}

cout << " Enter output image file name: ";

cin >> ofile;

ofs.open(ofile.c_str());

if (!ofs.is_open()) {

cout << " Out File Opening error ";

return -1;

}

readImage(image, ifs);

ifs.close();

int choice;

menu();

cin >> choice;

switch (choice) {

case 1:convertGrayScale(image); break;

case 2:invertRed(image); break;

case 3:invertBlue(image); break;

case 4:invertGreen(image); break;

case 5:invertAll(image); break;

}

writeImage(image, ofs);

ofs.close();

}

Why am I and how to fix errors like this? I only get it working with bigger size pictures.

Plus pls help me format it better with it asking Enter choice at the end.

Thank you

.7 Select C:UsersiChadDesktopC++ progNew folderNmageModder-SampleProgram.exe Portable Pixmap (PPM) Image Modder Enter image file name (ppm):cake.ppm Enter output file name(ppm):cake Magic: P3 Width, Height: 720, 540 Number of Pixels: 388800 Max Color: 255 Image Processing Choices 1. Convert to grayscale 2. Invert Red 3. Invert Green 4. Invert Blue 5. Invert All Enter Choice: 1

Explanation / Answer

Hello,

Kindly compile your code with static linking.

In your visual studio, go to Project tab -> properties - > configuration properties -> C/C++ -> Code Generation on runtime library choose /MTd for debug mode and /MT for release mode.

This will cause the compiler to embed the runtime into the code. The executable will be significantly bigger, but it will run without any need of runtime dlls.

and for the access violation,

the information is not provided properly so I am giving the conclusion based on your given picture.

Here in the if condition you have'nt initialised properly so its better to initialise the variables in a constructor. Your declared variables is not able to access the location and thats why it has gone out of scope.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote