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

write a C program to transform a given picture into an up-side-down picture by p

ID: 3934706 • Letter: W

Question

write a C program to transform a given picture into an up-side-down picture by pseudo-code:

Explanation / Answer

#include #include struct pixel { char r, g, b; }; // global data int g_width, g_height; //prototypes void parseHeader( FILE *input ); void parseImage( FILE *input, struct pixel *theArray ); void print(struct pixel a[]); void my_Mirror(struct pixel a[]); void rotate(struct pixel a[]); void my_Flip(struct pixel a[]); #include "transform.h" int main (int argc, char *argv[]) { // declarations here FILE *inFile; // open input file inFile = fopen(argv[2],"r"); if (inFile == NULL) { fprintf(stderr, "File open error. Exiting program "); exit(1); } // implied else, fopen successful // set g_width and g_height parseHeader(inFile); // malloc space for the array (example given in assignment write-up) struct pixel * pImage; if(NULL == (pImage = malloc(sizeof(struct pixel) * g_width * g_height)) ) { // then, malloc failed perror( "malloc failed for image size" ); exit( EXIT_FAILURE ); } // implied else, malloc successful // parseImage function call here parseImage(inFile, image); fclose(inFile); // cleanup if( 2 > arvc ) { // then not enough parameters given on command line printf( "format is: %s followed by a number in the range 1..3 ", argv[0] ); exit( EXIT_FAILURE ); } // implied else, valid command line input // manipulate the image according to command-line parameter // 1: mirror image // 2: upside down image // 3: rotate to the right 90 degrees switch(atoi(argv[1]) ) { case 1: my_Mirror(pImage); break; case 2: my_Flip(pImage); break; case 3: rotate(pImage); break; default: printf( "invalid command line parameter "); printf( "parameter must be in range 1..3 inclusive "); printf( "exiting " ); free( pImage ); // cleanup exit( EXIT_FAILURE ); break; } // end switch print(image); free( pImage ); // cleanup return 0; } // end function: main void my_Mirror(struct pixel a[]) { int col,row; struct pixel temp; // note: following works irregardless if even or odd number of columns for(row = 0; row