Hi, I'm New and I have a Question redaring Memory Maps
Apr 12, 2011 at 5:21am UTC
Hi everyone,
Here is the code I wrote myself.
How would I make a map of memory?
I need to draw a map of memory at the point just before the closing brace for the function inputFmFile().
Also, how would I draw a map of memory at the point when the program first enters the function temp().
I really need help and would definitely appreciate it.
Love,
Ingrid
tempFns.h
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MaxDays = 31;
const int MaxTimes = 5;
void tempAvg (double [] [MaxTimes], int , int );
void inputFmFile (double [][MaxTimes], int &, int &);
void outputData (double [][MaxTimes], int , int );
tempDriver.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include "tempFns.h"
int main () {
double temperatures [MaxDays][MaxTimes];
int numDays = 0;
int numTimes = 0;
inputFmFile (temperatures, numDays, numTimes);
cout << "Temperature data before calculating averages." << endl;
outputData (temperatures, numDays, numTimes);
cout << endl;
tempAvg (temperatures, numDays, numTimes);
cout << "Temperature data after calculating averages." << endl;
outputData (temperatures, numDays, numTimes+1);
cout << endl;
cout << "Bye!" << endl;
return 0;
}
Definitions
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "tempFns.h"
void tempAvg (double tempArray [MaxDays] [MaxTimes], int days, int times) {
if (days > MaxDays || days < 28 || times >= MaxTimes) exit (1);
double sum = 0;
for (short i = 0; i < days; i++) {
for (short j = 0; j < times; j++) {
sum += tempArray [i] [j];
}
tempArray [i] [MaxTimes] = sum / times;
sum = 0;
}
}
void inputFmFile (double tempArray [][MaxTimes], int &days, int ×)
{
bool worked = true ;
string inFileName;
char * name = "" ;
ifstream inStr;
do {
cout << "Enter name of file to read from: " ;
cin >> name;
inFileName = name;
inStr.open (inFileName.c_str());
if (inStr.fail())
{
cerr << "Error opening file. Try again." << endl;
inStr.clear();
worked = false ;
}
else
worked = true ;
} while (!worked);
inStr >> days >> times;
for (int i=0; i < days; i++)
for (int j=0; j < times; j++)
inStr >> tempArray [i][j];
for (int i=0; i < days; i++)
tempArray [i][times] = 0.0;
}
void outputData (double tempArray [][MaxTimes], int days, int times)
{
cout << endl << "The temperatures for the month are : " << endl;
cout.setf(ios::fixed);
cout.precision (2);
cout << setw(9) << "Day" ;
for (int i=0; i < times-1; i++)
cout << setw(8) << "Temp" << i+1;
cout << setw(8) << "Average" << endl;
for (int i=0; i < days; i++) {
cout << setw(9) << i+1;
for (int j=0; j < times; j++)
cout << setw(9) << tempArray [i][j];
cout << endl;
}
}
Apr 12, 2011 at 8:47am UTC
Your function parameters are somewhat unconventional. If my view, the function signature does not match that of the prototype, but I haven't compiled it to confirm this.
In function InputFmFile, you have construct:
1 2 3 4 5
string inFileName;
char * name = "" ;
//...
cin >> name;
inFileName = name;
You've declared
name
to be a pointer to a string, and that string is an empty string. What you probably meant to do was to allocate a buffer that you could read a value into. You'd do that with:
1 2 3 4 5
string inFileName;
char name[64]; // input limited to 63 chars
//...
cin >> name;
inFileName = name;
This is a bit of a pain and not very good. A better way is to use a string class instead. It deals with variable lengths.
1 2 3 4 5
string inFileName;
string name;
//...
cin >> name;
inFileName = name;
I realise this hasn't answered you question, ...
EDIT: If you're program is crashing, this would be the place to start fixing it.
Last edited on Apr 12, 2011 at 9:28am UTC
Topic archived. No new replies allowed.