?

Log in

No account? Create an account

The Toybox

people for the conservation of limited amounts of indignation


Previous Entry Share Next Entry
i have seen hell. it is arrays.
clex reverence
seperis
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.


  • 1
I find that putting comments and output begging for good grades is VERY HELPFUL!

-Bree, more of a Java girl, but could totally proof your logic if you ever need help

Aww, thank you!

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*

You aren't assigning place[x] to cityLow in your loop there, if that helps.

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


Right after I read this, considered ritual maiming in the bathroom (as one does before finals), I was absolutely convinced that a.) You had actually written this in sanskrit and b. I was giong to dream about arrayed functions chasing me through my dreams.

Okay, the second happeend, but the first one--I forgot (God knows how): Parallel Arrays! And while! And--have coffee. Happier.

I can not TELL you how many times I dreamt about code during finals time. My last semester I had to actually built a code interpreter from scratch using Java and I had dreams about parsing for weeks.

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

Mine was only 30 minutes of looking - and that was only because someone else caught it as they walked past the desk. Err. I mean, if I had done that before. Which I also haven't.

Of course not. We would never do such things!!

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

It was the devil's '\n' thing that is made of evil. Also, weirdly, an extra return after the last string and double in the text.

Universe is less dark.

Once I had the Brilliant! (not) idea of taking a super-strength Sudafed during finals to fend off an incipient sinus headache.

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.

OH MY GOD I have had that trip! That is a bad, bad trip.

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

I'm just totally gobsmacked that that means anything at all to you! Bows!


....I totally fooled you!

Okay, I can read part of it, but the parallel arrays thign is seriously *painful*.

I'm definitely a Java person, but word has forced me to C++ hell (and for various reasons, I'm very bad/rusty and am still in the very, very basic level). So right now, you do realise that I want to open up Visual Studio and play with your code now, right?

Tell me if you figure out the parallel arrasy thing for high adn low temp. Gah.

I'm just about to hit send on an email to seperis [at] livejournal. Let me know if you don't get it.

I feel your pain. I'm doing -or trying very hard to do- something very similar for my Programming coursework, only in Java. This? a gazillion times less messy than mine. (and despite never having done C++, there is something a little bit wrong with the fact that I can *read* this...)

Alcohol makes it less messy for a while, though!

*giggles*

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.

Oh yes. I asked our tutor today to teach something totally complicated and useless, just to make this seem less mind-numbingly scary. He thought I was joking.

Hey! I could at least help you! :D I PWN Java arrays after my Multimedia Database course where I had to use java to break down imagines into two-dimensional arrays of pixel values. :D

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

You know what? I would LOVE your help so much! but If I can't figure this one out on my own now, I'm never gonna get this stuff, and as frustrating as it is I do actually enjoy it. (I also have a feeling it would be frustrating for you too - There are a ton of better ways to do what were trying to do, but we've only been taught so much - it's very early days - and we're only allowed to do certain things in certain styles - i.e in this case, we can only user one dimensional arrays.)

saying that - if you're willing, any advice/tips you could give would be received with virtual mass hugging and my eternal gratitude!

Sure! I actually tutored CS201 at my school (which was the Intro to Object Oriented Program with Java course) so I'm fairly used to simplifying solutions to use the base level of options. I'd be happy to point you in the right direction or help with odd bugs...sometimes that's all you need. Someone to tell you why it's not working!

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


crap. i can't remember how c and c++ deal with passing in variables and pointers differently. *squints*

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

Okay, yes. That's where it went dark and wrong.

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.

yay! :D my c++ is really dusty (as in, sr year of high school), so i had to go find things online. XD and i wasn't sure how your input file was formatted. (also, i hate file i/o. bane of my existence.)

hurrah for working programs! \o/

Ah hah! I should have seen that. Good catch!

  • 1