Passing array to functions with pointers

I really need some help with my last assignment of the semester. I cant figure out how to pass arrays to functions. I have been thinking it would work best if I used pointers but I really dont know hoe to use them very well. I just need my program to...
1. Read 100 integers from a txt file.
2. Put them into a 10 x 10 array
3. Then pass the array to displayArray where it displays 10 numbers across & 10
down

So I started out by writing everything in intmain() to make sure I could at least read and display everything. Then I started breaking it all up. So really the only problems should be in calling functions and/or passing the array.

I am probably way off and could really use help. Here is my code ATM. I will update it if I feel I have made a bit more progress on my own.
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
#include <iostream>
	#include <fstream>
	#include <sstream>
	#include <iomanip>

	using namespace std;
	int loadArray(int *);
	int displayArray(int *, int, int);
	int main()
	{
		const int rowConst = 10;
		const int colConst = 10;
		int numarray[rowConst][colConst];		//declare array
		loadArray(numarray);					// call load fucntion
		return 0;
	}
	int loadArray (int *lArray[10][10])
	{
		ifstream myfile ("Prj6TestData.txt");		//read file
		if (myfile.is_open())					
		{
				cout << "MY TABLE\n\n";
				for (int row=0; row<10; row++)
				{	for (int col=0; col<10; col++)
					{		
						myfile >> lArray[row][col];	
						displayArray(lArray, row, col);		// Call display function
						if (myfile.eof())
							col = 10, row = 10;
					}
				cout << endl;
				}
		myfile.close();
		}
		else cout << "Unable to open file\n";
		return 0;
	}
	int displayArray(int *dArray[10][10], int row, int col)
	{
		cout << setw(5) << dArray[row][col];		//display
	}

Last edited on
Scroll towards the bottom of this tutorial or if you have the time, read the whole thing. It sounds like you need a basic tutorial on passing arrays to functions. The solution will be obvious once you read the information at this link.
http://cplusplus.com/doc/tutorial/arrays/
well after reading that and doing some more research I have made it farther now and think that I am extremely close to having it work.



My current problem is that it compiles just fine but instead of filling the table with numbers from the text file, it is filling it up with "-858993460" for all 100 integers. So I think its some small error that I am just not seeing. There are 0 errors or warnings.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;

const int NUM_COLS  = 10;
const int TBL_ROWS = 10;

void loadArray(int [][NUM_COLS], int);
void displayArray(int [][NUM_COLS], int, int);

int main()
{

	int table[TBL_ROWS][NUM_COLS];

	loadArray(table, TBL_ROWS);
	return 0;
}

void loadArray(int numarray[][NUM_COLS], int numRows)
{
		ifstream myfile ("Prj6TestData.txt");
		if (myfile.is_open())					
		{
				cout << "MY TABLE\n\n";
				for (int row=0; row<10; row++)
				{	for (int col=0; col<10; col++)
					{		
						myfile >> numarray[row][NUM_COLS];
						displayArray(numarray, row, col);
					}
				cout << endl;
				
				}
myfile.close();
		}
		else cout << "Unable to open file\n"; 
}
void displayArray(int numarray[][NUM_COLS], int rows, int col)
{
	cout << setw(5) << numarray[rows][col];
}

instead of myfile >> numarray....

Edit: nvm, didn't read first post properly. Although I now have some work to challenge myself.

I still think that declaration won't work as you saying put the whole file into the current element.
remember myfile is the file, so basically what you want to do is split myfile up
you can use getline(myfile, line); but this will give you each line in the file as a string. so you would need to split this up further and cast to int. There's probably a better way of doing that.


Also being that your array is using constants, your functions can use those constants as well, this doesn't make any difference to your program, however, just a tip. you can also make your array a class field which means you wouldn't need to pass it to each function and could continue just using the constants. (yep I'm lazy :) )
Last edited on
Topic archived. No new replies allowed.