Trouble in code

Actually I have to write a program that displays average, highest and lowest temperatures which displays something like:
"Enter the number of consecutive days to read their temperature : 5
Enter temperature for day 1: 50
Enter temperature for day 2: 80
Enter temperature for day 3: 30
Enter temperature for day 4: 92
Enter temperature for day 5: 47

The average temperature is 59.80
The highest temperature is 92.00
The lowest temperature is 30.00"

I have wrote something : ) but how I put "Enter days"?? and I have some trouble in end of the code. Please see and help me Please. The code is follows:

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
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream.h>
#include <stdlib.h>

void highTemp(int[]);
void lowTemp(int []);
void averageTemp(int[]);
using namespace std;

int main(int argc, char *argv[])
{
    int n;
    cout << "\n\t     TEMPERATURE CALCULATION     " << endl;
    cout << "Enter the number of consecutive days to read their tempetature : ";
    
    int x[n], tmp;
    
    for(int i=0; i < n; i++)
    {
            for(int j=0; j < n-1; j++)
            {
                    if (x[j] > x[j+1])
                    {
                             tmp = x[j];
                             x[j] = x[j+1];
                             x[j+1] = tmp;
                    }
            }
    }
    
    highTemp(x, n);
    lowTemp(x, n);
    averageTemp(x, n);
    
    system("pause");
}

void highTemp(int as[], int arraySize)
{
     cout << "The highest Temperature is : " << as[arraySize-1] << endl;
}

void lowTemp(int as[])
{
     cout << "The lowest temerature is : " << as[] << endl;
}

void averageTemp(int as[], int arraySize)
{
     int i; double c = 0;
     for(i=0; i < arraySize; i++)
     {
              c = c+ as[i];
     }
     c = c/i;
     cout << "The average temperature is : " << c << endl;
}
void swap(int *x, int *y)
{
     int tmp;
     if(*x > *y)
     {
           tmp = *x;
           *x = *y;
           *y = tmp;
     }
}


1
2
3
4
5
6
7
cin >> n;
for (int i = 0; i < n; ++i)
{
    cout << endl << "Temparature: ";
    cin >> x[i];
}
cout << endl;



In line 15 you cannot declare an array with a variable length. The array size must be a constant value.

Note: In line 49 you don't need to declare i. You are declaring it within your for loop in line 50.


What trouble are you having at the end of your code?
Last edited on
Depending on where you are in the learning process, I would either learn a bit about vectors (easier) or dynamic arrays (more advanced) to solve your need of a variably sized array. I suggest taking a look at both and seeing which you understand more however.
Thanks friends.
Here is remaining:

In line 4 : too many arguments to function 'void hignTemp(int**)'
In line 36: at this pint

also in line (5,37) and (6,38)

(also in 'int main(int, char**)')


Today is last date :'(


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream.h>
#include <stdlib.h>

void highTemp(int[]);
void lowTemp(int []);
void averageTemp(int[]);
using namespace std;

int main(int argc, char *argv[])
{
    int n;
    cout << "\n\t     TEMPERATURE CALCULATION     " << endl;
    cout << "Enter the number of consecutive days to read their tempetature : ";
    cin >> n;
    
    int i, x[n], tmp;
    for(i=0; i<n; i++)
    {
             cout << "\nEnter temperature of day " << i+1 <<" : ";
             cin >> x[i];
    }
    
    for(i=0; i < n; i++)
    {
            for(int j=0; j < n-1; j++)
            {
                    if (x[j] > x[j+1])
                    {
                             tmp = x[j];
                             x[j] = x[j+1];
                             x[j+1] = tmp;
                    }
            }
    }
    
    highTemp(x, n);
    lowTemp(x, n);
    averageTemp(x, n);
    
    system("pause");
}

void highTemp(int as[], int arraySize)
{
     cout << "The highest Temperature is : " << as[arraySize-1] << endl;
}

void lowTemp(int as[])
{
     int b = 0;
     cout << "The lowest temerature is : " << as[b] << endl;
}

void averageTemp(int as[], int arraySize)
{
     int i; double c = 0;
     for(i=0; i < arraySize; i++)
     {
              c = c+ as[i];
     }
     c = c/i;
     cout << "The average temperature is : " << c << endl;
}
void swap(int *x, int *y)
{
     int tmp;
     if(*x > *y)
     {
           tmp = *x;
           *x = *y;
           *y = tmp;
     }
}
Some of your declarations and prototypes don't match up.

Similarly, some of the calls and declarations don't match up.
Topic archived. No new replies allowed.