Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,104 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,693 people online right now. Registration is fast and FREE... Join Now!




2D to store avg temperature

 
Reply to this topicStart new topic

2D to store avg temperature, I have a hw to write a program- store the average monthly temp in 2D a

dumb4o
13 Oct, 2007 - 02:35 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
mellow.gif
This is my code- any suggestions?
Thanks!


This post has been edited by dumb4o: 13 Oct, 2007 - 09:10 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: 2D To Store Avg Temperature
13 Oct, 2007 - 02:50 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
depends what you want to do. are there errors in the code? is the output not what you expect/want? please be a little more specific. the code looks like it should run fine as is, so where so you want to go from here?

and please use the code tags for posting code - take a look at the gray text on the background of the post/reply text box. and just so you know, you can also edit your previous post to add the tags.

-jjh
User is offlineProfile CardPM
+Quote Post

dumb4o
RE: 2D To Store Avg Temperature
13 Oct, 2007 - 03:21 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
QUOTE(jjhaag @ 13 Oct, 2007 - 03:50 PM) *

depends what you want to do. are there errors in the code? is the output not what you expect/want? please be a little more specific. the code looks like it should run fine as is, so where so you want to go from here?

and please use the code tags for posting code - take a look at the gray text on the background of the post/reply text box. and just so you know, you can also edit your previous post to add the tags.

-jjh

The program runs O'K, but i have to store the result in 2D arrays and i'm really lost sad3.gif
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: 2D To Store Avg Temperature
13 Oct, 2007 - 03:44 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
for a statically allocated (compile-time) 2d array, the syntax isn't too tough. you just need to include another [] in your declaration and accessing.

for instance, if you want a 2D array with the high temps in column 0 and the lows in column 1, you would declare your variable index as int index[MONTH][2]. then you could input the values for the highs into index[i][0], and the lows into index[i][1].

and passing to a function would work in the same way. the function header would become:
CODE
void getData(int read, int read[MONTH][2], double ave, double aveOne, double maxIndex, double maxTwoIndex, double min, double minTwo);


and anywhere you are accessing the array, you would now need to specify both the row (the month) as well as the column (hi or lo) index.

hope that helps. try it out, play around with it, and see how it goes.

-jjh
User is offlineProfile CardPM
+Quote Post

dumb4o
RE: 2D To Store Avg Temperature
13 Oct, 2007 - 09:16 PM
Post #5

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
QUOTE(jjhaag @ 13 Oct, 2007 - 04:44 PM) *

for a statically allocated (compile-time) 2d array, the syntax isn't too tough. you just need to include another [] in your declaration and accessing.

for instance, if you want a 2D array with the high temps in column 0 and the lows in column 1, you would declare your variable index as int index[MONTH][2]. then you could input the values for the highs into index[i][0], and the lows into index[i][1].

and passing to a function would work in the same way. the function header would become:
CODE
void getData(int read, int read[MONTH][2], double ave, double aveOne, double maxIndex, double maxTwoIndex, double min, double minTwo);


and anywhere you are accessing the array, you would now need to specify both the row (the month) as well as the column (hi or lo) index.

hope that helps. try it out, play around with it, and see how it goes.

-jjh

Thank you very much that really helped me.
User is offlineProfile CardPM
+Quote Post

dumb4o
RE: 2D To Store Avg Temperature
14 Oct, 2007 - 01:05 PM
Post #6

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
QUOTE
Thank you very much that really helped me.



I thought that the program runs ok ,but.... there is something wrong with it -it shows "max" temp, but doesn't show "min"
this is the code:
CODE
int main()
{
    ofstream outFile;
    double ave=0.0, aveOne=0.0;
    int temp_read=12;
    double index[MONTH][2];

    double max=0;
    double min=0;
    double maxTwo=0;
    double minTwo=0;
    outFile.open("outData.out");

..........................................................................................................................................................................................................................................................................................................................................
        for (int i = 0; i< temp_read; i++)
    {
        cout << "Enter the highest temperature reading for month #" << i+1 << ": ";
        cin >> index[MONTH][0];
        
        ave = ave + index[MONTH][0];
        //if(index[MONTH][0]>max)
        //max=index[MONTH][0];
        
           if(index[MONTH][0]<max)    
             min=index[MONTH][0];
          
        
    }

    ave = ave / temp_read;
..........................................................................................................................................................................................................................................................................................................................................
        getch();
    return 0;
}


just like a brain storm, but i'm confused crazy.gif

This post has been edited by dumb4o: 14 Oct, 2007 - 04:57 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: 2D To Store Avg Temperature
14 Oct, 2007 - 01:33 PM
Post #7

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
you've got min, max, MinOne, MinTwo, MaxOne, and MaxTwo, and you're outputting stuff to both the screen and a file. exactly what is it not showing, and where is it not doing so?

maybe you should post a sample input and both the expected and actual output. then it would be a little easier to see where the problem is.

you probably aren't going to get the result you're looking for using outFile<<(max, min, ave)<<" . you need to use a separate insertion << for each variable:
outFile<< max << " " << min << " " << ave<< " "

and usually printf and cout statements aren't mixed. there doesn't appear to be any reason why you can't just use cout << in this case.

hope that helps.

-jjh
User is offlineProfile CardPM
+Quote Post

dumb4o
RE: 2D To Store Avg Temperature
14 Oct, 2007 - 01:49 PM
Post #8

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
QUOTE(jjhaag @ 14 Oct, 2007 - 02:33 PM) *

you've got min, max, MinOne, MinTwo, MaxOne, and MaxTwo, and you're outputting stuff to both the screen and a file. exactly what is it not showing, and where is it not doing so?

maybe you should post a sample input and both the expected and actual output. then it would be a little easier to see where the problem is.

you probably aren't going to get the result you're looking for using outFile<<(max, min, ave)<<" . you need to use a separate insertion << for each variable:
outFile<< max << " " << min << " " << ave<< " "

and usually printf and cout statements aren't mixed. there doesn't appear to be any reason why you can't just use cout << in this case.

hope that helps.

-jjh



I've got max, min, maxTwo and minTwo -thease are for high temp and for low temp .The algorithm for max and maxTwo is ok,but it displays a random temp. for min and minTwo and i can't realize whre my mistake is sad3.gif

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: 2D To Store Avg Temperature
14 Oct, 2007 - 02:38 PM
Post #9

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE(dumb4o @ 14 Oct, 2007 - 03:49 PM) *

I've got max, min, maxTwo and minTwo -thease are for high temp and for low temp .The algorithm for max and maxTwo is ok,but it displays a random temp. for min and minTwo and i can't realize whre my mistake is sad3.gif


oops. i saw the doubled up output (cout and file) for MaxTwo, and my brain did something weird there.

you probably don't want this:
CODE
if(index[MONTH][0]<max)    
min=index[MONTH][0];

since that will assign the current value that you're looking at to min any time that the current value is less than the max. which is not what a minimum is. you probably wanted
CODE
if(index[MONTH][0]<min)    
min=index[MONTH][0];

of course, this is probably going to cause you some problems, because you have intialized min and max to zero prior to your input loop. for doing an on-the-fly min/max search, you want to intialize them both to the first value entered. Do one input step outside of your loop, assign the value to the array, and also to your min and max values. then start the loop beginning with i=1.

hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

dumb4o
RE: 2D To Store Avg Temperature
14 Oct, 2007 - 02:40 PM
Post #10

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 9


My Contributions
QUOTE
I've got max, min, maxTwo and minTwo -thease are for high temp and for low temp .The algorithm for max and maxTwo is ok,but it displays a random temp. for min and minTwo and i can't realize whre my mistake is sad3.gif

I think there is something wrong with my "if"
CODE
for (int i = 0;i < temp_read;i++)
    {
        cout << "Enter the lowest temperature reading for month #" << i+1 << ": ";
        cin >> index[i][1];
        
        aveOne = aveOne + index[i][1];
        if(index[i][1]>maxTwo)
        maxTwo=index[i][1];
        
        if(index[i][1]<maxTwo)
        minTwo=index[i][1];
    }

Oopsss, we typed at the same time.
I tried that example and it gave me a wrong negative answer...
It gave me a real max temp. but the min was negative.

This post has been edited by dumb4o: 14 Oct, 2007 - 02:49 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:07PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month