Its convenient for some variables to be global(declared outside the body of any function). The ones shown here are good candidates. Excessive use of global variables is not a good idea though. int inrows; int incols; int outrows; int outcols; unsigned char *in_image; unsigned char *out_image; You need a function to read the color image into memory. int loadColorImage() { /* read ppm header from stdin */ --- paste last codelab assignment solution here --- incols = vals[0]; inrows = vals[1]; in_image = (unsigned char *)malloc(); fread(in_image, 3, incols * inrows, stdin); /* end with some type of test for success */ } Here is the basic structure of the code needed to build the grayscale image. void makeGS_image () { ----- start by writing a ppm header --- /* write ppm (P5 header) */ --- allocate space for output_image out_image = ( ) malloc( ); row = 0; while (row < outrows) { make-gs-row(row); row = row + 1; } fwrite(out_image,,,) } --- this function creates a single row of the --- output image void make-gs-row(int row) { unsigned char pixval; int col = 0; while (col < outcols) { pixval = make_gs_pixel(row, col); out_image[row * outcols + col] = pixval; col = col + 1; } } ------ This function creates a single gray --- ------ pixel from r, g, b values of the corresponding ----- location in in the INPUT image. unsigned char make_gs_pixel(int row, int col) { float r, g, b; unsigned char outpix; int inrow; int incol; inrow = correspRow(row); incol = correspCol(col); r = in_image[3 * inrow * incols + 3 * incol]; g = b = outpix = 0.3*r + 0.5*g + 0.2*b; return(outpix) } Step 1 in testing should be to ensure that you can simply read and write a colur image: The following main can be used for that. int main( int argc, char *argv[]) { int rc; rc = loadColorImage(); /* 2 level nesting ok here */ if (rc != 0) exit (1); /* after image is loaded, check to see if it is actually correct */ fprintf(stdout, "P6 %d %d 255\n", incols, inrows); fwrite(in_image, 3, inrows * incols, stdout); return(0); /* if this works then move on to the next stage of the program */ dd makeGs_image(); return(0); } ------------------------------------------------------- ------------------------------------------------------- Step 2 should be that you show you can correctly make a gray scale image. For this test you should use the following (intentionally defective) make_gs_pixel() This version should create a picture in which the top row is back and the rows become increasingly bright until row 256 is reached and then the process repeats. unsigned char make_gs_pixel(int row, int col) { return(row % 256); } int main( int argc, char *argv[]) { int rc; rc = loadColorImage(); /* 2 level nesting ok here */ if (rc != 0) exit (1); makeGs_image(); return(0); } ------------------------------------------------- In step 3 use the "real" make_gs_pixel() but use the following for corresp_row and corresp_col int corresp_row( int row) { return(row) This one should create a correct output gs image if (and only if) the input and output dimensions are identical ------------------------------------------------- In step 4 use the "real" corresp_row and column. In this be careful not to use integer division where you need floating point!!! The value of row / outrows * inrows is ALWAYS 0!!!!!!!!