0% found this document useful (0 votes)
23 views3 pages

Q1

The document contains C++ code that defines functions to assign high, medium, and low temperature codes to values in a 2D temperature array, find the highest temperature for each hour across days, and display the temperature data and results. The code takes in dimensions for days and hours, inputs temperature data, and calls the various functions to process and output the results.

Uploaded by

TONG TIN YI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Q1

The document contains C++ code that defines functions to assign high, medium, and low temperature codes to values in a 2D temperature array, find the highest temperature for each hour across days, and display the temperature data and results. The code takes in dimensions for days and hours, inputs temperature data, and calls the various functions to process and output the results.

Uploaded by

TONG TIN YI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

using namespace std;

void HLMTemp(float temperature[][100], int d, int h)


//HLMTemp is to assign H,L,M to the temperature values
{
int i, j;
//i and j is to go through array

char * new_array = new char[d * h];


//loop through all rows
for(i = 0; i < d; i++)
{
//loop through all columns
for(j = 0; j < h; j++)
{

//if the temperature[i][j] is >= 27 then assign the value 'H' instead
of element//
if(*(*(temperature+i)+j)>=27)
{
new_array [j*d + i]='H';
}
//if the temperature is >=23 and <27 then assign 'M'
else if(*(*(temperature+i)+j)>=23 && *(*(temperature+i)+j)<27 )
{
new_array[j*d + i]='M';
}
//else assigning 'L'
else
{
new_array[j*d + i]='L';
}
}
}
//printing the new_array
for(i = 0; i < d; i++)
{
for(j = 0; j < h; j++)
{
cout<<"["<<i<<"]"<<"["<<j<<"]"<<" = "<<new_array[j*d + i]<<"\t\t";
}
cout<<"\n";
}
//deleting the new_array allotted space
delete[] new_array;
cout<<"\n";
}
//this is to print the highest temperature from each hour per day
void highesthourEach(float temperature[][100], int d, int h)
{
int i, j;

float highesthourEach;
for(i = 0; i < h; i++)
{
//assigning the min value to the highesthourEach
highesthourEach=-999999999999999999999999999999999999;
for(j = 0; j < d; j++)
{
//if the present value is greater than the highesthourEach , then
change the highesthourEach to present value
if(highesthourEach<(*(*(temperature+j)+i)))
{
//changing the highesthourEach
highesthourEach=*(*(temperature+j)+i);
}
}
//printing the highest temperature of the hour per day
cout<<"highest"<<" = "<<highesthourEach<<"\t\t";
}
cout<<"\n";
}
//to display the data in temperature array
void display(float temperature[][100], int d, int h)
{
//i and j is to traverse through array
int i, j;
//loop through rows
for(i = 0; i < d; i++)
{
//loop through columns
for(j = 0; j < h; j++)
{
cout<<"["<<i<<"]"<<"["<<j<<"]"<<" = "<<*(*(temperature+i)+j)<<"\t\t";
}
cout<<"\n";
}
}
int main()
{
//declaring the temperature array
float temperature[100][100];
//h and d is taking the no of hours and days respectively
double h, d;

//prompting the user for no of Days


cout<<"Enter no of Days: ";
std::cin >> d;
cout<<"\n";

//prompting the user for no of hours


cout<<"Enter no of hours: ";
std::cin >> h;
cout<<"\n";

//prompting the user for Enter the data of 2d array


cout<<"Enter your data"<<endl;
int i,j;

for(i=0;i<d;i++)
{
for(j=0;j<h;j++)
{
cin>>temperature[i][j];
}
}
cout<<"\n";
//displaying and calling the display Function
cout<<"The displayed temperature data is:"<<endl;
display(temperature,d,h);

//showing the display and highesthourEach


cout<<"\nThe highest temperature for each time is:"<<endl;
display(temperature,d,h);
highesthourEach(temperature,d,h);

//showing HLMTemp
cout<<"\nThe Found \"H\" \"M\" and \"L\" temperature is:"<<endl;
HLMTemp(temperature,d,h);
return 0;
}

You might also like