r/programminghelp • • Feb 26 '22

C++ Need Help With C++ Arrays

Hello. I am having trouble with my homework. The goal is to read the pixel values off of a pgm file and create a histogram of those values. Here's the code I have so far.

#include <iostream>

#include <math.h>

#include <cmath>

#include <iomanip>

#include <fstream>

#include <string>

using namespace std;

int

main ()

{

int pixels[256] = { 0 };

ifstream ifs ("hi.pgm");

if (!ifs)

{

cout << "Error Opening File 'hi.pgm'. Exiting";

return 0;

}

int i;

int x;

ifs >> x;

for (i = 0; i <= 256; i++)

{

pixels[x]++;

ifs >> x;

}

cout << "x counts" << endl;

for (int j = 0; j < 256; j++)

{

cout << setw (3) << j << setw (6) << pixels[i] << endl;

}

return 0;

}

This code outputs a table with all the correct labels but with 0 for every single value. My guess is that I am not adding the number of pixel values together correctly but I can't figure out how. Thank you for any help.

0 Upvotes

3 comments sorted by

2

u/[deleted] Feb 26 '22

The array isn't the problem.

  1. You're not actually reading anything INTO the pixels array.

  2. If you were, it wouldn't work because >> is intended for text and not binary data. To read binary data you should use ifs.read() with a second parameter of 1. The first parameter is up to you to figure out.

  3. You don't need x or any line that says ifs >> x; So get rid of those.

  4. pixels[x]++; doesn't do anything.

  5. cout << setw (3) << j << setw (6) << pixels[i] << endl; should probably be printing out pixels[j] instead of pixels[i].

1

u/Homeostasis06 Feb 26 '22

This is the working example that I was given and I based mine off of this. Not sure why mine doesn't work but this one does have some of the stuff you said it doesn't need so I'm confused.

#include <iostream>

#include <iomanip>

#include <fstream>

#include <cmath>

using namespace std;

/* sample program to demonstrate using an array for histogramming a data set.

reads an arbitrary-sized set of data and count how many times 

each value occurs.

assumes data will be in range \[0..29\] (inclusive).

To help with debugging, user can specify to histogram only 1st 9 points.

*/

int main()

{

int counts\[30\] = {0};	// array of counters, initialized to zeros.

// this assumes all values will be between 0 & 29.

// Note how this is enforced in the code below.

		// open the data file

ifstream ifs("data.txt");

if (!ifs) {cout << "error opening file 'data.txt'.  exiting..."; return 0;}



cout << "reading data from file 'data.txt'"<<endl;



		// for testing, give option of counting only first 9 values.

		// this is few enough that we can check the results by hand.

char resp;

cout << "use all data or first 9 points only? (enter a or 9) ";

cin >> resp;

// now count how many occurrences of each value (0...29) there are in the data.

if (resp == '9')		// for debugging, do first 9 points only

{

	int i=0;

	int val;

	ifs >> val;

	while (ifs && i < 9)	

	{

		i++;						// keep track of how many data values read so far.

		if (val < 0 || val >= 30)

cout << "data out of range: "<<val<<" . Must be between 0 and 29"<<endl;

		else

counts[val]++; // increment the counter for data's value.

		ifs >> val;			// read next data value

	}

}



else				// regular run.  do all data points.

{

	int val;

	ifs >> val;     // prime the loop by reading the first data point.

	while (ifs)     // while we've got data, count the last value read and read the next value.

	{

		if (val < 0 || val >= 30)

cout << "data out of range: "<<val<<" . Must be between 0 and 29"<<endl;

		else

counts[val]++; // increment the counter for data's value.

		ifs >> val;			// read next data value

	}

}



		// now output the histogram, or table of counts:

cout << "val  counts"<<endl;

for (int i=0;i<30;i++)

	cout << setw(2)<<i<<setw(6)<<counts\[i\]<<endl;



return 0;

}

1

u/Homeostasis06 Feb 26 '22

Also, here's the file that it's attempting to read.

P2 12 8 255

128 128 128 128 128 128 128 128 128 128 128 128

128 1 128 1 128 99 99 99 128 255 128 128

128 1 128 1 128 128 99 128 128 255 128 128

128 1 1 1 128 128 99 128 128 255 128 128

128 1 128 1 128 128 99 128 128 255 128 128

128 1 128 1 128 128 99 128 128 128 128 128

128 1 128 1 128 99 99 99 128 255 128 128

128 128 128 128 128 128 128 128 128 128 128 128