Passing array values between functions.

The purpose of this code is to:

1)Read in an existing text file called "mandelinput.txt", and create an array of those values. Its the, "importFile()" Lines 10 - 32.

2)Using the values now stored in the "testPic[ROWS][COLUMS]" array, I want to print out the values stored in each element of the array. Its the, "printArray()" Lines 35 - 45.

3)Using the values stored in the "testPic[ROWS][COLUMS]" array, I want to export them to a file called "mandeloutput.ppm". Its the, "exportArray()" Lines 48 - 72.

Problems:

I can foresee a problem when I try to read in values in my first function. Lines 1 through 4 of my input document are not values but letters.

My question is, is it possible for me to skip those four first lines before I read in any values into my array?

(Maybe *inputFile << endl << endl << endl << endl;* in between lines 24 and 25.)

The next major problem I'm having is how to handle the array in between functions.

How do I export the values from the array created in my first function, into my other two functions?


Thanks for taking a look.

Input text document:
P3
# The P3 means colors are in ASCII, then 3 column and 10 rows,
# then 255 for max color, then RGB triplets
3 10
255
255 000 000 000 000 000 000 000 000
000 255 000 000 000 000 000 000 000
000 000 000 000 000 000 000 000 000
000 000 255 000 000 000 000 000 000
255 255 000 000 000 000 000 000 000
000 255 255 000 000 000 000 000 000
255 000 255 000 000 000 000 000 000
000 100 000 000 000 000 000 000 000
100 000 100 000 000 000 000 000 000
200 200 000 000 000 000 000 000 000


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>

#define ROWS  10
#define COLUMS  9

using namespace std;

// Read file called mandelinput.txt and put into an array called testPic
void importFile(){

	int testPic[ROWS][COLUMS];
	ifstream inputFile;
	inputFile.open("mandelinput.txt");

	if (!inputFile)
	{
		cout << "Error opening file.\n";
	}

	for (int i = 0; i <= ROWS; i++){ //Rows
		cout << endl;

		for (int j = 0; j <= COLUMS; j++){//Colums
			inputFile >> testPic[i][j];
			// for see an error here, trying to read in lines 1 through 5
		}

	}

	inputFile.close();
}

// Print out the testPic or any other array's
void printArray(int myArray[][ROWS][COLUMS]){
	
	for (int i = 0; i <= ROWS; i++){
		cout << endl;
		for (int j = 0; j <= COLUMS; j++){
			cout << " " << i << " " << j;
		}
	}


}

// Output testPic onto mandleoutput.ppm
void exportArray(int ouputArray[][ROWS][COLUMS]){

	ofstream outputFile;
	outputFile.open("mandeloutput.ppm");

	if (!outputFile){

		cout << "Error opening file.\n";
	}

	outputFile << "P3" << endl;
	outputFile << "# information about the file" << endl;
	outputFile << "# more information about the file" << endl;
	outputFile << ROWS << " " << COLUMS << endl;
	
	for(int i = 0; i <= ROWS; i++){
		cout << endl;
		for (int j = 0; j <= COLUMS; j++){
			outputFile << ouputArray[i][j];
			
		}
	}

	outputFile.close();
}


int main(){

	//Call the importFile() function.
	//Use the array created in importFile() function and use it in printArray() function.

	//I tried printArray(importFile()); doesn't work. Still trying to
	//grasp the concept of fuctions, and sending values.

	//Use the array created in importFile() and use it in exportArray().

	//Eventually I would like to add another function that creates an input
	//for exportArray() that will randomize how the screen looks. 

	return 0;
}
Last edited on
See "Arrays as parameters" in http://www.cplusplus.com/doc/tutorial/arrays/


You need to understand the file format.

"P3" is a string, but it tells about the following data.
The lines starting with "#" are possibly comments; that is one common commenting style.
The two numbers tell how many records the table has. Apparently 30 in the example. The "P3" did tell that one record has three integers with values in range [0..255].
This was just a guess -- you need to know the format.

When you know the format, then you know what to expect and how to read it. For example, if the "#"-lines are indeed comments and optional, then you have to cope with possibility that every line or none could be a comment.
Thanks for the reply.

Is it possible for you to show me a specific example of how I might extract the testPic array into my main function. I believe if I get an example of this I should be able to follow that and use it to send my array values to my printArray() and exportArray();

What I saw in the link shown was similar to this:

int array1 [] = {1,2,3};

Then they defined an array function and transferred the values from main into that function.

printarray (array1, 3)

The function printarray was defined like this:

void printarray (int arg[], int length)

So you set a local array value for this function (int arg[]) and (int length) tells you how large the array is as far as rows. I don't know how to apply this with a two dimensional array. If you could help explain, it would be appreciated.

To the second part, http://en.wikipedia.org/wiki/PPM_image#PPM_example is what that is. This text document represents a picture I think would be the best way to describe it. My biggest concern with this part is that when I start to read values into my array, because this array is defined as int, I don't want it to read in text. So I was hoping to skip the first four lines of code, and go straight to reading in values.
Last edited on
Your example data does not match exactly the PPM format. MaxGrey (or last data value) seems to be missing. See http://www.fileformat.info/format/pbm/egff.htm#PBM-DMYID.3.3

I was right about the comments:
You can include comments in the PBM file. Characters from a # character to the next end-of-line are ignored.


Read a line. Remove possible comments. Convert the rest via stringstream.
Topic archived. No new replies allowed.