I have been given an assignment to construct a program that reads in temperatures every hour for 24 hours, find lowest, highest, and average, then output the temps in a tabulated form.

I'm semi-sure I can manage the computational and display part, but I am at a loss as to how to deal with the array. I don't know if it should be 1d, 2d, or should I use two arrays, one for temps and one for the hour feeding them to the functions separately? Suggestions please.

The other thing I'd like to ask is if anyone has a suggestion for reading in the temps. My only thought right now is to prompt the user for the 24 hourly temps, but that seems lengthy and cumbersome.

I know I'm probably making this harder than it really is, but getting started seems to be a recurring problem for me even with developing the pseudocode. If there are any suggestions for that besides the known "practice, practice, practice", let me know...lol

Thanks in advance, I appreciate the advice/constructive criticism

Why not make a double temp[24] array initialize the elements(to some unlikely temp say -1000) and then fill then in one at a time(for each hour)

Okay, I get the double temp 24, is it okay to initialize it to the unlikely temp when I have to provide validation for temp input between -50 and 130?

When you say fill them in one at a time for each hour, how do you suggest that's done? Right now what is stuck in my head (right or wrong) is a cout that asks for 24 hourly temps. Is that what you were thinking or did I misunderstand?

Well the simplest way is to associate the array elements with the hours of the day is temp[0] = initail temp, temp[1] = hour 1 temp, temp[2] = hour 2 temp, ..., temp[23] = final temp

is it okay to initialize it to the unlikely temp when I have to provide validation for temp input between -50 and 130?

Sure -1000 tells the program that the temp(array element) is available. Think of it as a marker or sential to inform the program that this element has not been used yet so it should fail your valiidation test.

Well the simplest way is to associate the array elements with the hours of the day is temp[0] = initail temp, temp[1] = hour 1 temp, temp[2] = hour 2 temp, ..., temp[23] = final temp

I'm not sure how that's done, but I'll see if I can figure it out, thank you

ask the user for which hour is it
cin>>x;

ask the temp.
cin>>y

store in a array
arr[x-1]=y;

or use a function that takes time controlled in put
if there is any.
try to get more info. on <timer.h>.

sorry wrong thread.

int count = 0
double temp = 0


While (count != numTemp) //numTemp will be defined in the header as a constant
{
	cout << “Enter temperature for :” << count << “ :00 “ << endl:
	cin >> temp

	if (temp >-50 && temp < 130)
		{
			array[count] =temp
			Count ++; 
		}

how does this look for an input function? Note: I have not yet named the array so I just called it "array" for now. This is intended to read in temperatures one for each hour for 24 hours.

Looks good

Alright, I'm getting the c2447 error for the following function and header

// Header file for array to hold 24 hour weather values

#ifndef WEATHERSTATION_H  
#define WEATHERSTATION_H  

const int numtemp = 24;

int hourlytemps[numtemp]; 

void gettemps( int temp[], int numtemp);

#endif


//function
#include "weatherstation.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;


// Input

void gettemps(int temp[], int numtemp);
{
int count = 0;

	while (count != numtemp)
	{
		cout << " Enter " << count << " :00 " << " temperature "<< endl;
		cin >> temp;

		if (temp >= -51 && temp <= 131)
			{
				hourlytemps[count] = temp;
				count ++; 
			}
	}
}

Now the first thing I thought of was pull the semi colon off the end of the function declaration, but if I do that, I get a whole host of istream errors and a right hand operand error at the cin>>temp line

what am I doing wrong?

oh...the purpose of this is to read in a temperature for each hour of the day for 24 hours and then output it in tabular format.

void gettemps(int temp[], int numtemp)
{
              int count = 0;

	while (count != numtemp)
	{
		cout << " Enter " << count << " :00 " << " temperature "<< endl;
		cin >> temp[count] ;

		if (temp[count] >= -51 && temp[count] <= 131)
			{
				hourlytemps[count] = temp[count];
				count ++; 
			}
	}
}

in void gettemps(int temp[], int numtemp) temp is a pointer. (presumably points to the first element of an array of size numtemp)
since the array hourlytemps is what you seem to be attempting to fill up, is there a need for a separate temp array?

well....I don't know if I need the second array or not...the idea behind coding it the way I did was to try to use the count to increment the input to the next position in the array using the loop to continue to ask for input until I filled all 24 positions (0 - 23). My intent is also to get the output display to be tabular with the hour and temp
00:00 42
01:00 44
etc.

should I just use a different variable than temp to increment the counter? If I did that, would it still fill the array properly?

well, you could simply write

void gettemps(  int numtemp )
{
  int count = 0;
  int temp ;
  while (count != numtemp)
  {
    cout << " Enter " << count << " :00 " << " temperature "<< endl;
    cin >> temp ;

    if (temp >= -51 && temp <= 131)
    {
      hourlytemps[count] = temp ;
      count ++; 
    }
  }
}

or better still, pass the array to be filled up (hourlytemps) as an argument

void gettemps(  int hourlytemps[ /* numtemp */ ], int numtemp )
{
  int count = 0;
  int temp ;
  while (count != numtemp)
  {
    cout << " Enter " << count << " :00 " << " temperature "<< endl;
    cin >> temp ;

    if (temp >= -51 && temp <= 131)
    {
      hourlytemps[count] = temp ;
      count ++; 
    }
  }
}

Okay, I see how you did it and I understand it, but my instructor is big on using prototypes, and thats why I used the prototype from the header as the function declaration. Now, he does say to pass the array to a function, is that not what I did? I'd also like to know if you think the code below will still fill my array properly, I used a different variable for temp called inptemp

void gettemps(int temp[], int numtemp)
{
int count = 0;
int inptemp = 0;

	while (count != numtemp)
	{
		cout << " Enter " << count << " :00 " << " temperature "<< endl;
		cin >> inptemp;

		if (inptemp > -50 && inptemp < 130)
			{
				hourlytemps[count] = inptemp;
				count ++; 
			}
	}
}

I also have to average all 24 temps in a separate function, thats what I'm trying to figure out now, how to sum them all without having to write them all out

> he does say to pass the array to a function ...
i'm almost certain that he means 'pass the hourlytemps array to the function'. ignoring the array that is passed does not make sense. perhaps write it as the second example in post #15.

> I also have to average all 24 temps in a separate function, thats what I'm trying to figure out now
write another function to which you pass the array:

double average( const int hourlytemps[], int numtemp )
{
   double total = 0.0 ;
   // loop thru the array; in the loop add the array element to total
   return total / numtemp ;
}

Would this do my averaging?

double averagetemp( int temp[], int numtemp)
{
sum = 0.0;
for (i = 0; i < Count; i++)
   sum = sum + hourlytemps[i];

if (count > 0)
   return sum / count;
else
   return 0.00;
}

> ignoring the array that is passed?

can you explain that a little? and if you can refer to where I screwed it up in my code I'll understand better :)

double averagetemp( const int hourlytemps[], int numtemp)
{
  // the first parameter is the array containing the temperatures
  // the second parameter is number of elements of this array
  // to do: return the average of all temperatures in the array.

  double sum = 0.0;
  for (i = 0; i < numtemp; i++)
    sum = sum + hourlytemps[i];

  if (numtemp > 0)
    return sum / numtemp;
  else
    return 0.00;
}

again, use the information that is passed to the function

hehe, I think we were posting at the same time...lol

So does this mean my prototypes are wrong in the header?

in your code,

double averagetemp( int temp[], int numtemp)
{
sum = 0.0;
for (i = 0; i < Count; i++)
   sum = sum + hourlytemps[i];

if (count > 0)
   return sum / count;
else
   return 0.00;
}

you are using some other array than the one passed to the function. the array (for which average is to be determined is temp not hourlytemps. and the number of elements are numtemp, not count

> So does this mean my prototypes are wrong in the header?
no. the prototypes are correct. the array called temp in the prototype is the same array that you want to call hourlytemps. and perhaps you are under the impression that these are two different arrays. giving rise to the confusion.

Okay, is that what's wrong with the other one also ( the fill function)? using a different array than the one I'm supposed to be passing?

yes. the problem is the same. fill the array that is passed to the function, not something else.
your main would look like:

// ...
const int N = 24 ;
int what_ever_you_want_to_call_it[N] ; // array of temperatures
gettemps( what_ever_you_want_to_call_it, N ) ;
double avg = averagetemp( what_ever_you_want_to_call_it, N ) ;
// ...

Okay, last question (I think) Can I use a for loop (much like the averaging) to find the low temp and high temp as well...essentially using the first array position, test > with second position, increment to third position and test >again, replacing if the condition is true?

yes. something like

int max_so_far = array[0] ;
for( int i=1 ; i<num_elements ; ++i )
{
  // if the element in question is greater than max_so_far, make 
  // max_so_far equal to that element
}
// max_so_far now contains the value of the largest element in the array
commented: V was very patient and very helpful +1

Thank you very much for your patience and assistance. I'm only beginning to understand this and it still takes me 10 tries to write even the simplest code...yet I still press on...lol

I have a function written to display my output, but I've never called a display function before and I'm not sure how to do it. Here is the function

void displaytemp( const int hourlytemps[], int numtemp, double averagetemp)
{
	cout << " Average Temperature: " << averagetemp << endl;
}

how do I call this in main?... I know it's not really necessary to create an output function, it's just practice writing functions and calling stuff from arrays and other functions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.