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

#include <iostream> #include <fstream> #include <iomanip> #include <string> usin

ID: 3817516 • Letter: #

Question

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

string get_filename();
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix);
void rotate_a_ninety_degree_PGM(int nc, int nr, int mv, int *pix);
void writePGM(string fname, int nc, int nr, int mv, int *pix);

//.......................................

int main()
{
   int ncols = 0, nrows = 0, maxval = 0;//, maxval = 255;
   int *pix = NULL; // instead of static int pix[1024*1024];
   string filename = get_filename();

   readPGM(filename, ncols, nrows, maxval, *&pix);
   rotate_a_ninety_degree_PGM(ncols, nrows, maxval, *&pix);
   writePGM("90degree_rotation_" + filename, ncols, nrows, maxval, *&pix);

   delete[] pix;
   pix = NULL;
   // add code to delete the pixel array here!
}

//.......................................

//
string get_filename()
{
   bool ok = false;
   string fn = "";
   do
   {
       if (fn.size() == 0) cout << "filename? ";
       else cout << "can't open " << fn << ". re-enter filename: ";
       cin >> fn;
       ifstream ifs(fn); // make sure we can open the file.
       if (ifs) ok = true;
       ifs.close();
   } while (!ok);
   //ifs.close();
   return fn;
}

//............................

//
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix)
{
   // you need to write this!
   delete[] pix;
   //pix = NULL;
   ifstream ifs;
   string p2;
   ifs.open(fname);

   ifs >> p2 >> nc >> nr >> mv; //stor the number in the varb.

   pix = new int[nc*nr]; //the set up size of array
                       //cout << "The value of area is " << (nc*nr) << endl;
   for (int i = 0; i < nc*nr; i++) // store the numbers in to array
   {
       ifs >> pix[i];
   }

   ifs.close();
   cout << "The max number is " << mv << endl;
   cout << "The col value is " << nc << endl;
   cout << "The row value is " << nr << endl;
   /*
   for (int i = 0; i < nc*nr; i++)
   {
   cout << setw(5) << *pix++;
   }*/
   //ifs.close();
}

//.......................................

//
void writePGM(string fname, int nc, int nr, int mv, int *pix)
{
   // you need to write this !
   ofstream ifs;
   ifs.open(fname);


   ifs << "P2" << " " << nc << " " << nr << " " << mv << endl;
   cout << endl;
   cout << endl;


   for (int i = 0; i < nc*nr; i++) //write down the numbers in the file
   {
       ifs << setw(5) << pix[i]++;
       //cout << setw (5) << pix[i];
   }

   ifs.close();

}
//.......................................


void rotate_a_ninety_degree_PGM(int nc, int nr, int maxval, int *pix)
{

   for (int r = 0; r < nr; r++)
   {

       for (int c = 0; c < nc; c++)
       {
           pix[c*nr+(nr-r-1)] = pix[r*nc+c];
          
       }
   }
   /*for (int i = 0; i < nc*nr; i++)
   {
   cout << setw(5) << *pix++;
   }*/
}

Is C++ code, How can I using pointer array to rotate 90 degree of my pgm image?

Here is my code, I don't know how to fix my code. Also, I need help find 270 degrees and 180 degrees?

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

void displayMatrix(unsigned int const *p,

                   unsigned int row,

                   unsigned int col);

void rotate(unsigned int *pS, unsigned int *pD,

           unsigned int row, unsigned int col);

int main()

{

    // declarations

    unsigned int image[][4] = {{1,2,3,4},

                               {5,6,7,8},

                            {9,10,11,12}};

    unsigned int *pSource;

    unsigned int *pDestination;

    unsigned int m, n;

    // setting initial values and memory allocation

    m = 3, n = 4, pSource = (unsigned int *)image;

    pDestination =

           (unsigned int *)malloc(sizeof(int)*m*n);

    // process each buffer

    displayMatrix(pSource, m, n);

    rotate(pSource, pDestination, m, n);

    displayMatrix(pDestination, n, m);

    free(pDestination);

    getchar();

    return 0;

}

void displayMatrix(unsigned int const *p,

                   unsigned int r,

                   unsigned int c)

{

    unsigned int row, col;

    printf(" ");

    for (row = 0; row < r; row++)

    {

        for (col = 0; col < c; col++)

            printf("%d ", *(p + row * c + col));

        printf(" ");

    }

    printf(" ");

}

void rotate(unsigned int *pS, unsigned int *pD,

            unsigned int row, unsigned int col)

{

    unsigned int r, c;

    for (r = 0; r < row; r++)

    {

        for (c = 0; c < col; c++)

        {

            *(pD + c * row + (row - r - 1)) =

                            *(pS + r * col + c);

        }

    }

}