Thursday, December 13th, 2007 01:50 am
i have seen hell. it is arrays.
I have seen the face of hell. It is arrays combined with functions combined with infile combined with all the darkness of the soul.
Actually, it was hellish but since I did finish it, I guess it wasn't as bad as I thought.
#include iostream
#include string
#include iomanip
#include fstream
using namespace std;
using std::fixed;
using std::setprecision;
void readArray(string [], double []);
void highTemp(string[], double []);
void lowTemp(string[], double[]);
void calcAver(double[]);
int main()
{
//variables
string city[8] = "";
double temp[8] = {0.0};
//get temperatures
readArray(city, temp);
//get high temperature
highTemp(city, temp);
//get low temperature
lowTemp(city, temp);
//calculate average temperature
calcAver(temp);
cout << " " << endl;
cout << "You are done!" << endl;
cout << "And wasn't that a very special hell." << endl;
cout << "Please give me an A." << endl;
cout << " " << endl;
system("Pause");
return 0;
}
void readArray(string town[8], double temperature[8])
{
ifstream tempFile("temperatures.txt");
int x = 0;
tempFile >> town[x];
cout << "cityArray[" << x << "] = " << town[x] << endl;
tempFile >> temperature[x];
cout << "tempArray[" << x << "] = " << temperature[x] << endl;
for (int x = 1; x < 8; x = x + 1)
{
tempFile >> town[x];
cout << "cityArray[" << x << "] = " << town[x] << endl;
tempFile >> temperature[x];
cout << "tempArray[" << x << "] = " << temperature[x] << endl;
}
}
void highTemp(string place[8], double heat[8])
{
double highTemperature = 0.0;
string cityHigh = "";
int x = 0;
highTemperature = heat[0];
for (int x = 0; x < 8; x = x + 1)
{
if (highTemperature < heat[x])
{
highTemperature = heat[x];
cityHigh = place[x];
system("Pause");
}
}
cout << "High Temperature: " << highTemperature << endl;
cout << "High City: " << cityHigh << endl;
system("Pause");
}
void lowTemp(string place[8], double cold[8])
{
double lowTemperature = 0.0;
int x = 0;
lowTemperature = cold[0];
for (int x = 0; x < 8; x = x + 1)
{
if (lowTemperature > cold[x])
{
lowTemperature = cold[x];
system("Pause");
}
}
cout << "Low Temperature: " << lowTemperature << endl;
cout << "Low City: " << place[x] << endl;
system("Pause");
}
void calcAver(double averTemp[8])
{
double average = 0.0;
double total = 0.0;
int x = 0;
for (int x = 0; x < 8; x = x + 1)
{
total = total + averTemp[x];
}
average = total/8;
cout << "Average Temperature: " << average << endl;
system("Pause");
}
Known Problems
The readArray() function is completely wrong. I had to hard wire it in because I couldn't figure out how to initialize it otherwise. However, from what I understand, most of the class was having the same problem.
The lowTemp() function refuses to display the name of the low city under the variable string lowCity, so switched to place[x] in that function. However, highTemp() *will* allow the highCity variable to display correctly but refuses to do so for its place[x]. I--seriously can't figure that one out. They're clones of each other. Must try to fix.
readArray() is just--no idea. Gah.
However, it will run and give correct results, so at minimum partial credit. Must hope for a miracle. And hey, maybe study for my final sometime before tomorrow.
I need alcohol.
ETA: It's still funny; even when I'm close to snapping, this is still so incredibly, incredibly fun.
Actually, it was hellish but since I did finish it, I guess it wasn't as bad as I thought.
#include iostream
#include string
#include iomanip
#include fstream
using namespace std;
using std::fixed;
using std::setprecision;
void readArray(string [], double []);
void highTemp(string[], double []);
void lowTemp(string[], double[]);
void calcAver(double[]);
int main()
{
//variables
string city[8] = "";
double temp[8] = {0.0};
//get temperatures
readArray(city, temp);
//get high temperature
highTemp(city, temp);
//get low temperature
lowTemp(city, temp);
//calculate average temperature
calcAver(temp);
cout << " " << endl;
cout << "You are done!" << endl;
cout << "And wasn't that a very special hell." << endl;
cout << "Please give me an A." << endl;
cout << " " << endl;
system("Pause");
return 0;
}
void readArray(string town[8], double temperature[8])
{
ifstream tempFile("temperatures.txt");
int x = 0;
tempFile >> town[x];
cout << "cityArray[" << x << "] = " << town[x] << endl;
tempFile >> temperature[x];
cout << "tempArray[" << x << "] = " << temperature[x] << endl;
for (int x = 1; x < 8; x = x + 1)
{
tempFile >> town[x];
cout << "cityArray[" << x << "] = " << town[x] << endl;
tempFile >> temperature[x];
cout << "tempArray[" << x << "] = " << temperature[x] << endl;
}
}
void highTemp(string place[8], double heat[8])
{
double highTemperature = 0.0;
string cityHigh = "";
int x = 0;
highTemperature = heat[0];
for (int x = 0; x < 8; x = x + 1)
{
if (highTemperature < heat[x])
{
highTemperature = heat[x];
cityHigh = place[x];
system("Pause");
}
}
cout << "High Temperature: " << highTemperature << endl;
cout << "High City: " << cityHigh << endl;
system("Pause");
}
void lowTemp(string place[8], double cold[8])
{
double lowTemperature = 0.0;
int x = 0;
lowTemperature = cold[0];
for (int x = 0; x < 8; x = x + 1)
{
if (lowTemperature > cold[x])
{
lowTemperature = cold[x];
system("Pause");
}
}
cout << "Low Temperature: " << lowTemperature << endl;
cout << "Low City: " << place[x] << endl;
system("Pause");
}
void calcAver(double averTemp[8])
{
double average = 0.0;
double total = 0.0;
int x = 0;
for (int x = 0; x < 8; x = x + 1)
{
total = total + averTemp[x];
}
average = total/8;
cout << "Average Temperature: " << average << endl;
system("Pause");
}
Known Problems
The readArray() function is completely wrong. I had to hard wire it in because I couldn't figure out how to initialize it otherwise. However, from what I understand, most of the class was having the same problem.
The lowTemp() function refuses to display the name of the low city under the variable string lowCity, so switched to place[x] in that function. However, highTemp() *will* allow the highCity variable to display correctly but refuses to do so for its place[x]. I--seriously can't figure that one out. They're clones of each other. Must try to fix.
readArray() is just--no idea. Gah.
However, it will run and give correct results, so at minimum partial credit. Must hope for a miracle. And hey, maybe study for my final sometime before tomorrow.
I need alcohol.
ETA: It's still funny; even when I'm close to snapping, this is still so incredibly, incredibly fun.
no subject
From:-Bree, more of a Java girl, but could totally proof your logic if you ever need help
(- reply to this
- thread
- link
)
no subject
From:Finally figured out it's my for statements that are causing the problem, but way too tired to work out the fix tonight. Will probably do it tomorrow at work while hating everyone. *G*
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:if (lowTemperature > cold[x])
{
lowTemperature = cold[x];
system("Pause");
}
You'd have to put:
lowTemperature = cold[x];
cityLow = place[x];
or something.
Also, if you try to use place[x] as the calling variable, I think that will only work if the last location in your array is the right one. X will always be 7 (or the 8th array place) after you've gone through the for loop, because that's when it stops. Unless you reset X to something else, it will retain the last known value.
Find me on AIM sometime if that doesn't make sense. :D
-Bree
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:Okay, the second happeend, but the first one--I forgot (God knows how): Parallel Arrays! And while! And--have coffee. Happier.
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:Hell, I had those parsing dreams for like, weeks after I graduated.
It is the joy of coding. The rush we get when we make something work...or the 25 minutes of headdesking after we realize we just spent 2 hours looking for the mistake in our code when really we'd commented out our include file by mistake. (Not that me and my co-worker did that or anything. Of course not. We shall never speak of this again.)
Drink coffee and life will be gooooood!
-Bree
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:I think our absolute best "DOH" moment of the year was me freaking out for several hours over why my very simple database query wasn't deleting records from the database. My coworker and I dissected that thing for hours before she remembered that when she'd set up the db user, she hadn't given us delete rights.
Wow we felt dumb. :D
-Bree
(- reply to this
- parent
- top thread
- link
)
no subject
From:Universe is less dark.
(- reply to this
- parent
- top thread
- link
)
no subject
From:I couldn't sleep at all that night, and I hallucinated in Basic do loops for hours. That kind of trip I do not need.
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:Drugs and hours of coding Do Not Mix. I had one or two of those doped up dreams about Lisp when I was taking my Programming Languages course. The parenthesis were out to get me.
-Bree
(- reply to this
- parent
- top thread
- link
)
no subject
From:(- reply to this
- thread
- link
)
no subject
From:Okay, I can read part of it, but the parallel arrays thign is seriously *painful*.
(- reply to this
- parent
- top thread
- link
)
no subject
From:(- reply to this
- thread
- link
)
no subject
From:(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:(- reply to this
- parent
- top thread
- link
)
no subject
From:Alcohol makes it less messy for a while, though!
(- reply to this
- thread
- link
)
no subject
From:I seriosly wish we had stuck a little longer on arrays. The example we have for the parallel is *bad*.
Functions, however, have by comparison become a lot easier.
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:(- reply to this
- parent
- top thread
- link
)
no subject
From:Hell, I use 3D arrays in Java. I hate when people fail to teach arrays well because THEY ARE SO WONDERFUL! I can do anything with a couple arrays.
-Bree
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:saying that - if you're willing, any advice/tips you could give would be received with virtual mass hugging and my eternal gratitude!
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:It is my karmic duty for all of my online friends who sat up with me at 3 am and tried to help me figure out why my databases wouldn't connect correctly. :D
I am e-mailable at breecita@gmail.com if you need help with basic ideas. I WILL convince the rest of the world to love arrays like I love arrays. Some day...
-Bree
(- reply to this
- parent
- top thread
- link
)
no subject
From:assuming that you passed the array in correctly, and assuming that you're reading in the information correctly:
void readArray(string town[8], double temperature[8])
{
ifstream tempFile("temperatures.txt");
int x = 0;
if (tempFile.is_open()) // if your file opened properly
{
while(tempFile.eof()) // this should keep on looping until end of file (no need to hard code in the x)
{
tempFile >> town[x];
cout << "cityArray[" << x << "] = " << town[x] << endl;
tempFile >> temperature[x];
cout << "tempArray[" << x << "] = " << temperature[x] << endl;
x++;
} // end while
} // end if
} // end function
(- reply to this
- thread
- link
)
no subject
From:The only change was I had to getline to get rid of the null character after the double temperture[x] - I think that was what was failing it out unless I set the x < 8.
...at least, I htink that was. It works correctly now, so hopefully yes.
(- reply to this
- parent
- thread
- top thread
- link
)
no subject
From:hurrah for working programs! \o/
(- reply to this
- parent
- top thread
- link
)
no subject
From:(- reply to this
- parent
- top thread
- link
)