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

Im writing a C++ program to take the negative of a PGM file and output it, but i

ID: 3638212 • Letter: I

Question

Im writing a C++ program to take the negative of a PGM file and output it, but im having trouble reading the input into a vector of vectors so i can access each element later to print/flip them for future programs. This is where im having trouble

#include <vector>
int main()
{
vector < vector<int> > picture;
while (!cin.eof())
{ /* checks for P2, rows, columns */
cin.clear();
picture.resize(rows);
while (cin >> pixels)
{     calc = (255-pixels);

      opposite = abs(calc);
       picture[r][c].push_back(opposite);
 /* This is where im having trouble, how can i assign the opposite pixels to the vector so they will be indexed correctly, without going into an infinite loop/segment fault? The for loop just puts the same value into the vector over and over or just crashes. */

Explanation / Answer

Here is your problem picture[r][c].push_back(opposite); It should be picture[r].push_back(opposite);