Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <cstdlib> //libraries
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. const int resolution = 10; //constant
  8.  
  9. ifstream infile;
  10. ofstream outfile; //if and ofstream initializers
  11.  
  12. void readData(int header[2], int image[resolution][resolution],int &count);
  13. void print(int image[resolution][resolution], int count); //function
  14. //prototypes
  15. int header[2], image[resolution][resolution]; //arrays
  16.  
  17. int main()
  18. {
  19. int count = 0;
  20. outfile.open("output.txt");
  21. infile.open("image.txt");
  22. if(!infile.is_open()) {
  23. cout << endl << "ERROR: Unable to open file" << endl;
  24. system("PAUSE");
  25. return 1;
  26. }
  27.  
  28. readData(header, image, count); //calls functions
  29. print(image, count);
  30.  
  31. infile.close();
  32. outfile.close();
  33. system("PAUSE");
  34. return 0;
  35. }
  36.  
  37. void readData(int header[2], int image[resolution][resolution], int &count)
  38. {
  39. infile >> header[0] >> header[1]; //reads in headers
  40.  
  41. while(!infile.eof()&& infile >> image[count][0]
  42. && infile >> image[count][1] //reads values into image
  43. && infile >> image[count][2]
  44. && infile >> image[count][3])
  45. count++;
  46. }
  47. void print(int image[resolution][resolution], int count)
  48. {
  49. int i, j, counter = 0;
  50. outfile << header[0] << " " << header[1] << endl; //prints header
  51. outfile.width(10);
  52.  
  53. for(i = 0; i < count; i++) { //prints arrays
  54. if(counter >= header[0]) {
  55. outfile << "\n" << endl; //puts a new line between each color
  56. counter = 0;
  57. }
  58.  
  59. for (j = 0; j < 4; j++) {
  60. outfile.width(10);
  61. outfile << image[i][j];
  62. }
  63. outfile << endl;
  64. counter++;
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement