|
-
April 18th, 2010, 10:46 PM
#1
Function Call Problem.
Like the title says, I need to reformat my code so it calls the functions. Here is the original code so you can have something to relate too. I've tried doing it myself, but the most common error I get is that I cannot change float[4][12] to float[][1] when I made an averageJuly function. So can anyone rearrange it for me, so I can see what it should look it. I've been relating it to past projects and it seems I'm still doing something wrong. A visual of code will help. Thanks.
Code:
#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
using namespace std;
const int row=4;
const int column=12;
void main()
{
ifstream infile;
float table[row][column];
int month, year;
float total=0, total2=0, totalJuly=0, total_6months=0;
infile.open("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;
}
for(year=0; year<4; year++)
totalJuly+=table[year][6];
cout<<"Average for July over 4 years is "<<setprecision(4)<<totalJuly/4<<endl;
for (month=0; month<6; month++)
total_6months+=table[0][month];
cout<<"Average for the first 6 months is "<<setprecision(4)<<total_6months/6<<endl;
for(month=0; month<column; month++)
total+=table[0][month];
for(month=0; month<column; month++)
total2+=table[3][month];
cout<<"Average price for first and last year is "<<setprecision(4)<<(total+total2)/24<<endl;
infile.close();
getch();
}
-
April 18th, 2010, 11:07 PM
#2
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;
}
Last edited by Thinias; April 18th, 2010 at 11:13 PM.
-
April 18th, 2010, 11:57 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|