Assignment - read an input file that has month, highest temp for month and lowest temp for month.
I have to use a function called GetData to fill the array.
I define the array as 12 rows and 3 columns. Please could someone look at the code I have and let me know if I'm creating the prototype correctly and the function call. I'm getting that ol' undeclared identifier error on GetData(m_t[]) I have only coded the GetData function so far. To my understanding the array m_t was identified in the function prototype. Do I have to do a priming read first?

#include <iostream> 
#include <fstream>
#include <iomanip>
   
using namespace std;

void GetData(ifstream& in, double m_t[]);                
void AverHigh(double m_t[],double& sum, double& aver_h)	
void AverLow (double m_t[],double& sum, double& aver_l);       
void HighestTemp(double m_t[], double& high);                    
void LowestTemp(double m_t[], double& low);                    
void PrintData (ofstream& out, double m_t[],double& aver_h, double& aver_l, double& high, double& low); 

int main ()
{
	int r = 12;                                   
	int c = 3;						

	ifstream in;
	ofstream out;

	in.open("temp.txt");
	if (!in)
	{
		cout << "Cannot open input file." << endl;
		return 1;
	}
	out.open("outdata");
    
	while (in)
	{
	     GetData(in, m_t[]);
	     AverHigh(m_t[], sum, aver_h);
             AverLow (m_t[], sum, aver_l);
	     HighestTemp(m_t[], high);
	     LowestTemp(m_t[], low);
	     PrintData (out, m_t[], aver_h, aver_l, high, low);
	}
	return 0;
}
void GetData(ifstream& in, m_t[])
{	
	double rcount = 12;
	double ccount = 3;
	int i;
	int j;
	
	in >> r >> c;
	for (i=0, i < rcount, i++)
		for (j=0, j < ccount, j++)
		{	in >> m_t(i,j);

You are missing the datatype for m_t[] in your function definition. You are passing m[t] into your functions in main() improperly, you do not need the [].
Take a look at line 51 versus other times you have used a 2D array. If you want to keep m_t as 1D you'll have to compute the index based on i,j. If not you'll have to rework your program.

I fixed the function definition but I'm still getting the error at the same place. I have change the datatype on the array to int instead of double. None of the examples in the book defines the prototype array with the row and column. Do I need to put in variables for that? Do I have to do a priming read before the function call?

Don't change the datatype of the definition you need to add the double before m_t[] on line 41. Changing it to int will conflict with your prototype and exacerbate the problem. If the arrays are 2D you must specify the second dimension in the prototype and definition, and you can't address the array with m_t(i,j) in C++. I'm not completely confident I know what you mean by priming read but I don't think so.

Edited 6 Years Ago by jonsca: n/a

I changed all occurences of reference to m_t to a data type int. so the
function prototype now is GetData (ifstream& in, int m_t[]) the function call is GetData (in, m_t[]) this is where I'm getting the "undeclared identifier" error. The error to me means the m_t[] array isn't identified anywhere. I don't know how else to declare the array in the function.

#include <iostream> 
#include <fstream>
#include <iomanip>
   
using namespace std;

void GetData(ifstream& in, int m_t[]);                 
void AverHigh(int m_t[],double& sum, double& aver_h);		  
void AverLow (int m_t[],double& sum, double& aver_l);         
void HighestTemp(int m_t[], double& high);                    
void LowestTemp(int m_t[], double& low);                       
void PrintData (ofstream& out, int m_t[],double& aver_h, double& aver_l, double& high, double& low); 

int main ()
{
	int r = 12;                         
	int c = 3;							
								                        	   
	ifstream in;
	ofstream out;

	in.open("temp.txt");
	if (!in)
	{
		cout << "Cannot open input file." << endl;
		return 1;
	}
	out.open("outdata");
    
	while (in)
	{
	     GetData(in, m_t[]);
// system ("pause")
		 AverHigh(m_t[], sum, aver_h);
		 AverLow (m_t[], sum, aver_l);
		 HighestTemp(m_t[], high);
		 LowestTemp(m_t[], low);
		 PrintData (out, m_t[], aver_h, aver_l, high, low);
	}
	return 0;
}
void GetData(ifstream& in, int m_t[])
{	
	double rcount = 12;
	double ccount = 3;
	int i;
	int j;
		
	for (i=0, i < rcount, i++)
		for (j=0, j < ccount, j++)
		{	in >> m_t(i,j);
}

In the main function when you call getData, the array m_t is not declared anywhere .Either make this array global or pass a pointer (reference to this array) to the getData function

The arrays are not passed into the functions with the []. I thought I went over that (EDIT: I did put the brackets around the t by mistake in post #2 but nontheless). Abhimanipal is right, I did not notice that before. You need to declare the array in main() and pass it in to all your functions. I wouldn't make it global.

Edited 6 Years Ago by jonsca: mea culpa

I'm not sure how to either of the suggestions. Would global array be defined const int m_t[12][3]?

void GetData(ifstream& in, int m_t[]) ?

how do I pass by reference?
The book has me totally confused.

The book has a function coded with the input file and the array with the brackets. example from book

void readCode(ifstream& infile, int list[], int& length ....)

so I don't understand arrays not being passed with the []

If you make the array global you do not need to pass it to any function. All functions have complete access to all global variables. So if you declare the array as global your approach will be something of this sort

int arr[10][10];

void func()
{
// Set/ get data into the array arr
}
int main()
{
  func();
}

But making a variable global is frowned upon in the industry as the variable can be modified from any where. So the other option is to pass the array by pointer (reference)

void func(int* arr)
{
   // The variable arr contains the address of the first element of the array
}

int main()
{
  int arr[10][10];
  func(&arr[0][0]);
}

Edited 6 Years Ago by abhimanipal: n/a

func(&arr[0][0]); which is just func(arr); If your book is confusing you look for some examples on the net. You need the brackets in prototypes and function definitions but NOT when calling the function.

Arrays are passed by pointer (so actually the value of the address) so when you type int a[] into the arguments of your prototype or definition, it really degrades to int * a . It's not actually passed by reference.

When you define your function as void myfunc(int a[][10]) it ends up being void myfunc(int (*a)[10]) (I believe, I may be off on that slightly) so passing in the address of an array int a[3][10] a is equvalent to (*a)[10], so the call would be myfunc(a);

Edited 6 Years Ago by jonsca: n/a

func(&arr[0][0]); which is just func(arr);

I also assumed the same but to my surprise when I wrote this piece of code

void func(int* arr)
{
     printf("%d\n",*arr);
}
      
int main()
{   
    int arr[2][2]={1,2,3,4};
//    func(arr);      This does not compile. What mistake am I making here?
    func(&arr[0][0]);
  
    printf("\n");              
	return 0;
}

Edited 6 Years Ago by abhimanipal: n/a

Okay,so the deal seems to be:

void func(int arr[][2])
{
   //Yada
}

int main()
{
    int arr[2][2] = {{1,2},{3,4}}; //I don't think this makes any difference
    func(arr);
    return 0;
}

Edited 6 Years Ago by jonsca: n/a

We haven't gotton to pointers yet in class. This chapter deals with arrays and 2d or multi arrays. from the book "when declaring a two-dimensional array as a formal parm you can omit the size of the first dimension, but not the second, that is, you must specify the number of columns." Even following the book examples nothing works. I have tried every combination I can think of to get this to compile w/o the m_t undeclared identifier error. Examples
void GetData(ifstream& in, int m_t[][3]);
void GetData(ifstream& in, int m_t[], int col);
void GetData(ifstream& in, int m_t[][3], in col);
nothing is working.
my call function example
GetData(in, m_t, col) error m_t undeclared identifier
GetData(in, m_t) error m_t undeclared identifier
In the prototype I have declared m_t as an array of int. I don't understand what I'm not declareing correctly.

I beat on this and beat, error by torturing error I now have the orginal problem working. Now I have one compiler error "error C2064: term does not evaluate to a function taking 2 arguments" on the line 72 where I am trying to fill the array in the function. If it is looking at the function as having 3 arguments I don't know what I'm doing. I just thought of something I am considering this a 2d array 12 rows indicating 12 months and 3 columsn for high and low in each column. Am I trying to define a 3d array?
M H L
1 29 5

void GetData(ifstream& in, int m_t[][3])
{	
	
	int row;
	int col;
	int rcount = 12;
	int ccount = 3;
	
//	int i; //first column for month number
//	int j; //second column for high/low temp
		
	for (row=0; row < rcount; row++)
		for (col=0; col < ccount; col++)
			in >> m_t(row,col);

// this line

No, it's a 2D array. Think of a 3D as holding values of a quantity that vary based on both the row and the column they are in. What you've done is essentially constructed a 2D array out of what could be 2 distinct 1D arrays(one holding the highs and one holding the lows)for ease of access.

I'm going to presume you're talking about line 14 above. I told you that 2D arrays are not accessed that way back in post #4. I'm not sure where you're getting that idea from but it's definitely wrong. m_t[row][col] is correct way(unless you want to address it by pointers but there's no reason to do that there).

Edited 6 Years Ago by jonsca: n/a

With the help of a fellow student I managed to get part of this program to compile.
I am breaking down the program into pieces. The function I am on now is supposed to find the average of high temperatures. I am getting an error that has me stumped. Any help would be appreciated.

: error LNK2019: unresolved external symbol "double __cdecl AverHigh(double (* const)[3],int,double)" (?AverHigh@@YANQAY02NHN@Z) referenced in function _main
: fatal error LNK1120: 1 unresolved externals
here is the code

#include <iostream> 
#include <fstream>
#include <iomanip>
   
using namespace std;
const int NUMBER_OF_ROWS = 12;
const int NUMBER_OF_COLUMNS = 3;
void GetData(ifstream& in, double m_t[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS);
double AverHigh(double m_t[][NUMBER_OF_COLUMNS],int NUMBER_OF_ROWS, double aver_h);
int main()
{
	double m_t[12][3];
	double aver_h = 0;
//	double aver_l;
//	int high;
//	int low;
	ifstream in;
//	ofstream out;

	in.open("testdata.txt");
	if (!in)
	{
		cout << "Cannot open input file." << endl;
		return 1;
	}
//	out.open("outdata");
    
	while (in)
	{
		int row;
		int col;
		in >> row >> col;    // I'm trying to do a priming read

	    GetData(in, m_t, NUMBER_OF_ROWS);
		AverHigh(m_t, NUMBER_OF_ROWS, aver_h);
//		AverLow(m_t, NUMBER_OF_ROWS);
//		HighestTemp(m_t, NUMBER_OF_ROWS);
//		LowestTemp(m_t, NUMBER_OF_ROWS);
		
	}
//	   PrintData (m_t, aver_h, aver_l, high, low);
//   PrintData (out, m_t, aver_h, aver_l, high, low);
	return 0;
}
void GetData(ifstream& in, double m_t[][NUMBER_OF_COLUMNS], int numberOfRows)
 { 
	int row;
	int col;

	for (row=0; row < numberOfRows; row++)
		for (col=0; col < NUMBER_OF_COLUMNS; col++)
		in >> m_t[row][col];

system("pause");	
 } 
double averHigh(double m_t[][NUMBER_OF_COLUMNS],int NUMBER_OF_ROWS, double aver_h)
{
   int c;
   int r;
//   double aver_h;
   for (c = 1; c < NUMBER_OF_COLUMNS; c++)
   {
     double aver_h = 0;	 
     double sum = 0;
	  for (r = 0; r < NUMBER_OF_ROWS; r++)
		  sum = sum + m_t[r][c];
		  aver_h = sum / NUMBER_OF_ROWS;
    }
  return aver_h;
  
}

You have a lowercase a (averHigh) in your definition. Change it to capital A.
Note something about scoping in your AverHigh function: it wouldn't let you redeclare aver_h in that position where it is commented out because your parameter aver_h already exists in that scope.
The compiler allows it within the braces of the for loop because that is considered its own scope, but the consequence is when you hit the closing brace all of those variables are out of scope and destroyed.
What you have essentially done is pass aver_h in one end of the function and return the same value out the other.

Thank you for your help. I went and reread about return value functions and it said "its a good idea to return the value as soon as it is computed" I think you are saying the same thing. I have another function that according to the book and you (if I'm understanding you correctly) is giving the same type of error and telling me the return stmt doesn't have a value. I have looked thru this function for the last 3 hours and I can't figure out what is wrong. Maybe I don't understand what your saying. Thank you for being so patient, I have serious doubts I will pass this class, one more chpt and then the final. As you can tell I have no desire to pursue prgmming as a carerr.

: warning C4715: 'HighestTemp' : not all control paths return a value
1>Linking...
: error LNK2019: unresolved external symbol "int __cdecl HighestTemp(int (* const)[2],int)" (?HighestTemp@@YAHQAY01HH@Z) referenced in function _main
: fatal error LNK1120: 1 unresolved externals

int HighestTemp(int m_t[][NUMBER_OF_COLUMNS],int NUMBER_OF_ROWS);
HighestTemp(m_t, NUMBER_OF_ROWS);int HighestTemp(int m_t[][NUMBER_OF_COLUMNS],int NUMBER_OF_ROWS);
HighestTemp(m_t, NUMBER_OF_ROWS);
int HighestTemp(int m_t[][NUMBER_OF_COLUMNS],int NUMBER_OF_ROWS, int high)
{
	int row, col;	 
    for (row = 0; row < NUMBER_OF_ROWS; row ++)
	{
	 high = m_t[row][0];
	 for (col=0; col < NUMBER_OF_COLUMNS; col++)
		if (high < m_t[row][col])
		    high = m_t[row][col];
	 return high; 
	}
}

for the other part from before it's an issue of

{
   int t = 10;

}

cout<<t; //won't work t is not defined within this scope

So anything on the inside braces, like stuff in for loops (with braces), functions (including main), pairs of braces in the code like the above example. Once the flow of execution leaves the braces, all those variables are destroyed. In your case you had that one variable coming in as a parameter, so it's in scope for the function. So either a) don't pass it in as a parameter, b) don't redeclare it inside the scope of that for loop or c) redesign your function so that both of the above are true.

I'm not sure what you are showing me in that bit of code you posted. Basically your call matches the prototype on line 1 but your implementation of the function has 3 parameters. The compiler isn't sure what to do.

If you're having serious doubts, talk to your professor. Sure he/she could be a cold-hearted you-know-what but there's some chance that they'll be able to help you. It sounds like you don't do well with your book, which is pretty common, but read some examples on the internet (hunt around here in the threads for suggestions of good tutorials too).

This question has already been answered. Start a new discussion instead.