Help with pointing arrays (simple...)
Sep 4, 2013 at 4:24pm UTC
In the temp array the first box represents the days and the second is the hour of the day.
I need to average the
total temperature for the day in the function "GetAvTime"
-How can I use my user defined "day1" in my array in my function?
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
#include <iostream>
#include <array>
#include <stdlib.h>
#include <time.h>
using namespace std;
void GetAvTime (int temp, int day1)
{
int k = day1, int average;
for (int y = 0; y < 25; y++)
{
temp [k][y];
average += y;
return ;
}
int main ()
{
int day1;
int random;
int temp[7][24];
srand (time(NULL));
random = rand() %50 + 50;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 24; j++)
{
random = rand() %50 + 50;
temp [i][j] = random;
}
}
cout << "Enter Day." << endl;
cin >> day1;
if (day1 < 0 || day1 > 7)
{
cout << "Invalid Parameters. Press any key to exit." << endl;
exit(1);
}
cout << endl;
GetAvTime (temp[7][24], day1);
return 0;
}
Last edited on Sep 4, 2013 at 4:49pm UTC
Sep 4, 2013 at 4:52pm UTC
1 2 3 4 5 6 7 8
int GetAvTime (int temp[][24], int day)
{
int average = 0;
for ( int k = 0; k < 24, k++ ) average += temp[day][k];
return ( average /= 24 );
}
Last edited on Sep 4, 2013 at 4:53pm UTC
Sep 4, 2013 at 4:58pm UTC
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
# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;
void GetAvTime (int *ptr)
{
int average = 0;
for (int i=0;i<24;i++)
{
average += *ptr++;
}
average /= 24;
cout << "The average temp is : " << average << endl;
}
int main ()
{
int day1;
int random;
int temp[7][24];
srand (time(NULL));
random = rand() %50 + 50;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 24; j++)
{
random = rand() %50 + 50;
temp [i][j] = random;
}
}
cout << "Enter Day." << endl;
cin >> day1;
cout << endl;
GetAvTime(temp[day1]);
return 0;
}
Sep 4, 2013 at 5:06pm UTC
Thanks, but one last thing.
it says:
"Arguement type of int is incompatible with parameter of type int [*][24];
on line 45
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
#include <iostream>
#include <array>
#include <stdlib.h>
#include <time.h>
using namespace std;
int GetAvTime (int temp[][24], int day)
{
int average = 0;
for ( int k = 0; k < 24; k++ ) average += temp[day][k];
return ( average /= 24 );
}
int main ()
{
int day1;
int random;
int temp[7][24];
srand (time(NULL));
random = rand() %50 + 50;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 24; j++)
{
random = rand() %50 + 50;
temp [i][j] = random;
}
}
cout << "Enter Day." << endl;
cin >> day1;
if (day1 < 0 || day1 > 7)
{
cout << "Invalid Parameters. Press any key to exit." << endl;
exit(1);
}
cout << endl;
GetAvTime (temp[7][24], day1);
return 0;
}
Sep 4, 2013 at 5:18pm UTC
1 2
std::cout << "The average temperature for day " << day1
<< " is " << GetAvTime (temp, day1) << std::endl;
Last edited on Sep 4, 2013 at 5:19pm UTC
Sep 4, 2013 at 5:47pm UTC
Int k = day1 is not an int
fix:
// k= day 1
int k = 1
Topic archived. No new replies allowed.