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

C++ program on visual studio. numbers inside (data.txt) could not attach the fil

ID: 3798211 • Letter: C

Question

C++ program on visual studio.

numbers inside (data.txt) could not attach the file.

16
17
9
16
0
17
19
16
24
3
8
12
14
5
20
6
2
11
20
17
2
11
4
2
3
7
21
16
18
20
22
1
21
13
19
12
17
24
10
19
3
11
22
8
23
14
16
11
3
18
22
19
12
7
12
9
23
16
4
3
16
10
15
17
13
6
15
17
14
23
21
5
15
4
20
0
6
1
18
23
4
23
9
4
6
15
16
1
6
8
19
14
1
23
12
13
18
7
4
16

Part IV: counting values. The fifth sample program above should help you with this part of the lab. Here's data.txt for use with the sample program. Use an array to count how many pixels are zero, how many are 1, how many are 2, etc., in a PGM image. Output a histogram table of the image. Prompt the user for the name of the PGM file to histogram. The fifth sample program does almost everything you need for this. You just need to figure out how to read (and make use of the header info of the PGM, instead of reading exactly 30 values from a file. You can assume that all PGM images I give you have pixel values between 0 & 255 inclusive. Your table should have several columns of counts (so the user doesn't need to scroll through 250+ lines of output, but it may be helpful to start with a single column, till you get the counting part right. Then go back and get the table looking pretty (you figured out how to write data to a table in last week's lab...) Then add code so find some image statistics: the min, max, mean and median (middle value

Explanation / Answer

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
int row = 0, col = 0, num_of_rows = 0, num_of_cols = 0;
stringstream ss;
ifstream infile("testfile.pgm", ios::binary);

string inputLine = "";

getline(infile,inputLine);
if(inputLine.compare("P5") != 0) cerr << "Version error" << endl;
cout << "Version : " << inputLine << endl;

getline(infile,inputLine);
cout << "Comment : " << inputLine << endl;

ss << infile.rdbuf();   
ss >> num_of_cols >> num_of_rows;
cout << num_of_cols << " columns and " << num_of_rows << " rows" << endl;

int max_val;
ss >> max_val;
cout<<max_val;

char pixel;
unsigned int pixel-value[num_of_rows][num_of_cols];
unsigned int integral[num_of_rows][num_of_cols];
for(row=0;row<num_of_rows;row++)
{
for(col=0;col<num_of_colss;col++)
{
ss>>pixel;
pixel_value[row][col]=pixel;
}
cout<<endl;
}


int **pixel_value = new int*[num_of_rows];
for(int i = 0; i < num_of_rows; ++i)
{
pixel_value[i] = new int[num_of_cols];
}

int **integral = new int*[num_of_rows];
for(int i = 0; i < num_of_rows; ++i)
{
integral[i] = new int[num_of_cols];
}


integral[0][0]=pixel_value[0][0];
for(int i=1; i<num_of_cols;i++)
{
integral[0][i]=integral[0][i-1]+pixel_value[0][i];
}   
for (int i=1;i<num_of_rows; i++)
{
integral[i][0]=integral[i-1][0]+pixel_value[i][0];
}
for (int i = 1; i < num_of_rows; i++)
{
for (int j = 1; j < num_of_cols; j++)
{
integral[i][j] = integral[i - 1 ,j] + integral [i,j - 1] - integral[i - 1,j - 1] + pixel_value[i,j];   
}
}

ofstream output1("pixel_value.txt");
for (int k=0; k<num_of_rows; k++)
{
for (int r=0; r<num_of_cols; r++)
{
output1 << pixel_value[k][r] << " ";
}
output1 << ";" << endl;
}

ofstream output2("integral_value.txt");
for (int a=0; a<num_of_rows; a++)
{
for (int b=0; b<num_of_cols; b++)
{
output2 << integral[a][b] << " ";
}
output2 << ";" << endl;
}

for(int i = 0; i < num_of_rows; ++i) {
delete [] pixel_value[i];
}
delete [] pixel_value;

for(int i = 0; i < num_of_rows; ++i) {
delete [] integral[i];
}
delete [] integral;

infile.close();
system("pause");
return 0;

void histo_2 (int values[], int sz)
{
int level = max_arr(values,sz);
for(; level > 0; level—) {
for(int i = 0; i < sz; i++)
if (values[i] >= level) cout << "**** ";
else cout << " ";
cout << endl;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote