Finding weather calculations using arrays

I need help finding the coldest temperature using an array. I already found how to get the warmest temperature.

[code]
#include <iostream>
#include <fstream>
using namespace std;

int Warmest(const int A[], int n)
{
int warm=-1;
for(int i=0;i<n;i++)
if(A[i]>warm) warm = A[i];
return warm;
}

int ReadData(int A[], int n)
{
ifstream file;
file.open("temp.dat");
int k=0;
if(file)
{
while(file)//while last read was successful
{
if(k>=n) break;
file>>A[k];
cout<<A[k]<<endl;
k++;
}
}
file.close();
return k;
}

int main() {
int A[31];
ReadData(A,31);
int n = ReadData(A,31);
int hotest=Warmest(A,n);
cout<<"Warmest days's temp:"<<hotest<<endl;
return 0;
}
[code]
Last edited on
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
50
51
52
53
#include <iostream>
#include <fstream>

const char file_name[] = "temp.dat" ;

// return count of items read
int read_data( int array[], int array_size ) {

    std::ifstream file(file_name) ;

    int cnt = 0 ;
    while( cnt < array_size && file >> array[cnt] ) ++cnt ;

    return cnt ;
}

int highest( const int array[], int array_size ) { // invariant: array_size > 0

    int highest_val = array[0] ;

    for( int i = 1 ; i < array_size ; ++i ) // for every item after position 0
        if( array[i] > highest_val ) highest_val = array[i] ;

    return highest_val ;
}

int lowest( const int array[], int array_size ) { // invariant: array_size > 0

    int lowest_val = array[0] ;

    for( int i = 1 ; i < array_size ; ++i ) // for every item after position 0
        if( array[i] < lowest_val ) lowest_val = array[i] ;

    return lowest_val ;
}

int main() {

    const int MAX_DAYS = 31 ;
    int temperatures[MAX_DAYS] = {0} ;

    const int ndays = read_data( temperatures, MAX_DAYS );
    std::cout << ndays << " temperatures were read\n" ;

    if( ndays > 0 ) {
            
        const int warmest = highest( temperatures, ndays );
        const int coldest = lowest( temperatures, ndays );

        std::cout << "warmest: " << warmest << '\n'
                  << "coldest: " << coldest << '\n' ;
    }
}
******
Last edited on
its the same as warm.

int Coldest(const int A[], int n)
{
int cold = A[0]; //consider doing this on warm too. what if all the data were from antartica?
for(int i=1; i<n;i++)
if(A[i] < cold) cold = A[i];
return cold;
}
Last edited on
Topic archived. No new replies allowed.