
December 4th, 2008, 06:43 PM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 1
Time spent in forums: 5 m 23 sec
Reputation Power: 0
|
|
|
2D Arrays and functions
Quote: /* This program creates a 2-D array with a max size of 20x20. These squares are 'magic'. Each number is presented once from 1-nth times in the magic square. */
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int readMagic(ifstream ins, int &magic[], int sides);
void printMagic(int &magic[], int sides, ifstream &ins);
int main() {
int magic[20][20],
ifstream ins;
int sides ;
ins >> sides;
if(sides > 25)
{
cout << "The length of the square is too large." <<endl ;
cout << "Tty a new file." << endl ; return 0 ; } cout << "What file do you want to process? ";
cin >> filename; ins.open(filename.c_str()); if (ins.fail()) { cout << endl ; cout << "Bad file name" << endl ; return 0; } readMagic(ins, magic, sides) ; printMagic(magic, sides, ins) ; } int readMagic(ifstream ins, int magic[], int sides) { for(int col = 0; col < sides; col++) { for(int row = 0; row < sides; row++) { ins >> magic[col][row]; } } return magic[] ; } void printMagic(int magic[], int sides, ifstream &ins) { for(int col = 0; col < sides; col++) { for(int row = 0; row < sides; row++) { ins >> magic[col][row]; } } } |
I'm trying to pass a 2D array into the functions so I can read numbers from a file into the array. The first ins >> sides means the first number determines the dimensions of the square. So if it is 5 then it's going to be a 5 by 5 triangle. Right now I'm getting errors saying
Quote: | 1>------ Rebuild All started: Project: Assign11, Configuration: Debug Win32 ------ 1>Deleting intermediate and output files for project 'Assign11', configuration 'Debug|Win32' 1>Compiling... 1>Assign11.cpp 1>c:\temp\assign11\assign11\assign11.cpp(14) : error C2234: 'magic' : arrays of references are illegal 1>c:\temp\assign11\assign11\assign11.cpp(16) : error C2234: 'magic' : arrays of references are illegal 1>c:\temp\assign11\assign11\assign11.cpp(21) : error C2146: syntax error : missing ';' before identifier 'ins' 1>c:\temp\assign11\assign11\assign11.cpp(21) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(24) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(36) : error C2065: 'filename' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(37) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(37) : error C2228: left of '.open' must have class/struct/union 1> type is ''unknown-type'' 1>c:\temp\assign11\assign11\assign11.cpp(37) : error C2065: 'filename' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(37) : error C2228: left of '.c_str' must have class/struct/union 1> type is ''unknown-type'' 1>c:\temp\assign11\assign11\assign11.cpp(39) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(39) : error C2228: left of '.fail' must have class/struct/union 1> type is ''unknown-type'' 1>c:\temp\assign11\assign11\assign11.cpp(46) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(47) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(56) : error C2109: subscript requires array or pointer type 1>c:\temp\assign11\assign11\assign11.cpp(60) : error C2059: syntax error : ']' 1>c:\temp\assign11\assign11\assign11.cpp(69) : error C2109: subscript requires array or pointer type 1>Build log was saved at "file://c:\temp\Assign11\Assign11\Debug\BuildLog.htm" 1>Assign11 - 17 error(s), 0 warning(s) ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ========== |
|