|
|
|
|
|
|
Please use int main(), not void main(). Please use <cstsdlib>, not <stdlib.h>. |
#include <iostream> #include <conio.h> #include <fstream> #include <stdlib.h> #define ROWMAX 200 #define COLMAX 200 using namespace std; ifstream inFile; ofstream outFile; // Prototype declarations int avgFilter(); //====== main() void main() { int greyVal[ROWMAX][COLMAX], // original values read from the pgm file meanVal[ROWMAX][COLMAX]; // storing modified grey values for the output pgm file char ftype[5]; // file type in pgm file: P2 // add other necessary variables declarations here int ROW , COL , MAXval ; // Open two different external files for reading and writing, and check files' status. Add your statements here. inFile.open("Q.pgm"); outFile.open("newQ.txt"); if (inFile.fail() || outFile.fail()) { cerr << " Program cannot read in the value ! " << endl; _getch(); exit (-1); } // start reading files, read file type: P2; inFile >> ftype; // read in values of 3 more integers here inFile >> ROW >> COL >> MAXval ; // by using nested for loops, write statements here to read grey values from input file to store in greyVal array // read greyscale values for( int i = 0 ; i < ROW ; i++ ) { for( int j = 0 ; j < COL ; j++ ) { inFile >> greyVal[i][j]; } } for( int i = 0 ; i < ROW ; i++ ) { for( int j = 0 ; j < COL ; j++ ) { meanVal[i][j] = greyVal[i][j]; } } // Output to external file, must be in exactly the same format as the input pgm file outFile << ftype << endl; // This is the first line of the file. // Write column and row to second line of the file outFile << ROW << " " << COL << " " << endl ; // Write the third line (255 as in Q.pgm) outFile << MAXval << endl ; // Write the contents of meanVal by using nested for loops for( int i = 0 ; i < ROW ; i++ ) { for( int j = 0 ; j < COL ; j++ ) { outFile << meanVal[i][j] << " "; } // <==== add a space after each entry outFile << endl; // <==== move here } outFile << endl; inFile.close(); outFile.close(); cout << "Done! Please check output file."; _getch(); } |
P2 10 11 255 0 0 0 0 0 0 0 0 0 0 0 0 0 150 150 150 150 0 0 0 0 0 255 0 0 0 0 255 0 0 0 200 0 0 0 0 0 0 200 0 0 200 0 0 0 0 0 0 200 0 0 200 0 0 0 0 0 0 200 0 0 200 0 0 0 255 0 0 200 0 0 200 0 0 0 0 255 0 200 0 0 0 255 0 0 0 0 255 0 0 0 0 0 150 150 150 150 0 255 0 0 0 0 0 0 0 0 0 0 0 |
(2) Your file Q.pgm says 10 ROWS and 11 COLUMNS ... but if you count them you have 11 ROWS and 10 COLUMNS. Believe me: this matters! |