Pointer and function troubles!

Hey Y'all, I was looking for some help. I have to finish this homework assignment by tomorrow but I really a step in the right direction. I am confused like all hell. Can anyone offer suggestions on how to start writing those functions. I really appreciate it guys and sorry if this is ambiguous.

Here my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

string get_filename( );
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix);
void negatePGM(int nc, int nr, int mv, int *pix);
void writePGM(string fname, int nc, int nr, int mv, int *pix);

//.......................................

int main( )
{
    int ncols=0, nrows=0, maxval=255;
    int *pix = NULL;        // instead of static int pix[1024*1024];
    string filename = get_filename( );
    
    readPGM(filename, ncols, nrows, maxval, pix);
    negatePGM(ncols, nrows, maxval, pix);
    writePGM("neg_"+filename, ncols, nrows, maxval, pix);
    
    // Need to delete the pixel array 
}

//.......................................

    // get the filename of an existing file.
string get_filename( )
{
    bool ok = false;
    string fn = "";
    do
    {
        if (fn.size()==0) cout << "filename? ";
        else cout << "can't open "<<fn<<". re-enter filename: ";
        cin >> fn;
        ifstream ifs(fn.c_str());
        if (ifs) ok = true;
    } while (!ok);
    return fn;
}

//readPGM:  opens file, reads header, allocates pixel array, reads in pixels.
//note that nc, nr, mv and pix are all passed by reference.
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix)
{
    // This is where I have trouble
}

//.......................................

//writePGM:  opens output file, outputs header line, outputs pixel array.
void writePGM(string fname, int nc, int nr, int mv, int *pix)
{
    // And here
}
//.......................................

//negatePGM:  note that pix is passed by value.  but since it is a pointer,
//we can still modify the pixel array by indexing it. 
void negatePGM(int nc, int nr, int mv, int *pix)
{
    for (int r=0;r<nr;r++)
        for (int c=0;c<nc;c++)
            pix[r*nc + c] = mv - pix[r*nc + c];
}
 
1
2
3
4
5
6
//readPGM:  opens file, reads header, allocates pixel array, reads in pixels.
//note that nc, nr, mv and pix are all passed by reference.
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix)
{
    // This is where I have trouble
}


Programming is all about breaking things down into smaller, more manageable steps.

That already seems to be done mostly for you here... as it tells you step by step what you need to do in this function:

1) Open the file
2) Read the header
3) Allocate memory for your pixels array
4) Read in pixel data


I'm sure you know how to do at least some of those steps.... like you know how to open a file, right? So start with that. Then do the next step, then the next.
In readPGM() you need to allocate memory, then read data.
You allocate memory by using the new[] operator as in:

pix = new int[1000]; // I don't know how you decide how much to allocate

Then in main() don't forget to delete[] the pix pointer, to de-allocate the memory:

delete[] pix; // Need to delete the pixel array

sorry if this is ambiguous.

You could get better help if you explained what your functions are supposed to do, and also post the homework statement and requirements, and so on.

Personally I find it a bit odd that int is used to store pixel information. It would make sense if each int is broken up into four bytes representing Red, Green, Blue and Alpha.
Last edited on
The only thing the hmwk says is Complete this program, which you will recognize as the PGM negation program, using functions and dynamic memory allocation. Be sure to delete [ ] the array before exiting.
Question: How is it that the function negatePGM(...) can change the actual pixel array, even though the pointer pix is passed by value? The functions are suppose to read and write a pgm
Topic archived. No new replies allowed.