Re: Function Call Problem.
C requires you to define the size of the lowest (n - 1) dimensions, if you think about it this makes sense. The array is contiguous, so how can C possibly know how big each dimension of the array is unless you tell it? In this case, you have 2 dimensions so you must specify the lowest dimension - like this.
Code:
float AverageJuly(float table[][12]);
Applied to your code with a little personal formatting and sprucing that I added while I was trying to understand the problem you were having, you get this. Note I strictly try to avoid using "magic numbers" anywhere possible.
Code:
#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
using namespace std;
typedef enum Month_t
{
JANUARY = 0,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
} Month;
const int ROW = 4;
const int COLUMN = (int)DECEMBER + 1;
float AverageMonth(float table[][COLUMN], Month iMonth = JULY);
float AverageNMonths(float table[][COLUMN], int iMonths = 6);
float AveragePriceForYear(float table[][COLUMN], int iYear = 0);
void main()
{
ifstream infile;
float table[ROW][COLUMN];
int month, year;
float total = 0.0f, total2 = 0.0f, totalJuly = 0.0f, total_6months = 0.0f;
infile.open("test.txt"); // FIXME: change this back to data.txt
for(year = 0; year < ROW; year++)
{
for(month = 0; month < COLUMN; month++)
{
infile >> table[year][month];
cout << setw(3) << year << setw(3) << month << setw(8) << table[year][month] << endl;
}
cout << endl;
}
infile.close();
cout << "Average for July over 4 years is " << setprecision(4) << AverageMonth(table, JULY) << endl;
cout << "Average for the first 6 months is " << setprecision(4) << AverageNMonths(table, 6) << endl;
float fYear1 = AveragePriceForYear(table, 0);
float fYear2 = AveragePriceForYear(table, 3);
float fAverage = (fYear1 + fYear2) / 2.0f;
cout << "Average price for first and last year is " << setprecision(4) << fAverage <<endl;
_getch();
}
float AverageMonth(float table[][COLUMN], Month iMonth)
{
float fTotalMonth = 0.0f;
for(int year = 0; year < ROW; year++)
fTotalMonth += table[year][(int)iMonth];
return fTotalMonth / ROW;
}
float AverageNMonths(float table[][COLUMN], int iMonths)
{
float fTotalNMonths = 0.0f;
for (int month = 0; month < iMonths; month++)
fTotalNMonths += table[0][month];
return fTotalNMonths / iMonths;
}
float AveragePriceForYear(float table[][COLUMN], int iYear)
{
float fTotalPrice = 0.0f;
for(int month = 0; month < COLUMN; month++)
fTotalPrice += table[iYear][month];
return fTotalPrice / COLUMN;
}
Re: Function Call Problem.
Thanks alot. I appreciate the detailed help. And do you know a good site where I can learn C++/Java on my own? I want to review C++ during the summer and touch a bit on Java before fall semester starts. From judging on how many posts/help requests I needed, I definitely require huge amounts of practice if I'm doing this as a career haha. Sad eh? haha. So any site links to help me with my studies will be appreciated. Thanks to all.