Array Help

need help fixing my errors and help with assigning a sort function to sort the numbers in ascending order.. here is what i've so far .


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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>

 

int max(int, int []);

int min(int, int []);

int average(int, int []);

 

main(){

      int i=0;

      int j[i+1];

      int k;

      while(true){

      cout << "Please input the number of temperatures to be read : ";

      cin>>i;

    

      for(int m=1;m<=i;m++){

                        cout<<"\nEnter temperature  "<<m<<":";

                        cin>>j[m];   

                        }     

                      

      cout<<"\nThe average temperature is: "<<average(i, j);

      cout<<"\nThe highest temperature is: "<<max(i, j);

      cout<<"\nThe lowest temperature is: "<<min(i, j);

 

cout<<"\n\nPress any key for run again and press '0' for quit\n";

cin>>k;

if(k==0){

         cout<<"\n\t\t**************************************\n";

         cout<<"\t\tCompiled by Nicholas Flores";

         cout<<"\n\t\t**************************************\n\n";

         system("pause");

         break;}

}

}

 

int max(int i, int j[]){   

                            int l=j[1];

                            for(int m=1; m<=i; m++)

                                    if(j[m]>l)

                                    l=j[m];                               

    return l;

}

  

int min(int i, int j[]){   

                           int l=j[1];

                           for(int m=1; m<=i; m++)

                                   if(j[m]<l)

                                   l=j[m];

    return l;

}

  

int average(int i, int j[]){

                           int l=0;

                           for(int m=1; m<=i; m++)

                                   l=l+j[m];                                 

                           l=l/i;

    return l;

}
Last edited on
Please edit your post and write [code] before your code and [/code] after your code so that it uses syntax highlighting.
Sorry "LB" i am new to this site. my professor recommended it to me to use if i ever need help.

need help fixing my errors and help with assigning a sort function to sort the numbers in ascending order.. here is what i've so far .

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>



int max(int, int []);

int min(int, int []);

int average(int, int []);



main(){

int i=0;

int j[i+1];

int k;

while(true){

cout << "Please input the number of temperatures to be read : ";

cin>>i;



for(int m=1;m<=i;m++){

cout<<"\nEnter temperature "<<m<<":";

cin>>j[m]; 

} 



cout<<"\nThe average temperature is: "<<average(i, j);

cout<<"\nThe highest temperature is: "<<max(i, j);

cout<<"\nThe lowest temperature is: "<<min(i, j);



cout<<"\n\nPress any key for run again and press '0' for quit\n";

cin>>k;

if(k==0){

cout<<"\n\t\t**************************************\n";

cout<<"\t\tCompiled by Nicholas Flores";

cout<<"\n\t\t**************************************\n\n";

system("pause");

break;}

}

}



int max(int i, int j[]){ 

int l=j[1];

for(int m=1; m<=i; m++)

if(j[m]>l)

l=j[m]; 

return l;

}



int min(int i, int j[]){ 

int l=j[1];

for(int m=1; m<=i; m++)

if(j[m]<l)

l=j[m];

return l;

}



int average(int i, int j[]){

int l=0;

for(int m=1; m<=i; m++)

l=l+j[m]; 

l=l/i;

return l;

}
Last edited on
These are the errors I get when I compile your program:
prog.cpp:13:6: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
 main(){
      ^
prog.cpp: In function ‘int main()’:
prog.cpp:23:1: error: ‘cout’ was not declared in this scope
 cout << "Please input the number of temperatures to be read : ";
 ^
prog.cpp:23:1: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.8/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
                  ^
prog.cpp:25:1: error: ‘cin’ was not declared in this scope
 cin>>i;
 ^
prog.cpp:25:1: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.8/iostream:60:18: note:   ‘std::cin’
   extern istream cin;  /// Linked to standard input
                  ^
Have you tried actually reading the errors? They explain exactly what's wrong and even give hints on how to fix it.

Specifically, main must return int, and you need to use std::cout and std::endl.
Last edited on
This would be a lot easier if you used std::vector<> to store the items. Then you could use std::min() and std::max(). Have you learned about these things? Will your prof let you use them?

If you have to do it as shown then, here are some comments.

Line 17 (int j[i+1]) doesn't do what you think it does. At that point, i==0 so it allocates an array j with 1 item in it. When you input a value for i at line 25, the size of j does NOT change. In other words, the size of an array is fixed when you define it.

Throughout the program you store the i items in j[1] through j[i]. This may seem more natural, but C++ starts arrays at index value 0. So it's better to store the values in j[0] through j[i-1]. This way you aren't wasting an extra value of j.

Your code for the average is returning an int. Should it return a floating point type instead? If so then line 101 should be double average(int i, int j[]) and line 109 should be return (double)l/i; Then you can get rid of line 111.

I suggest that you indent blocks of code and use curly braces, even when they aren't needed. There's nothing worse than having your code execute one statement when you think it's executing a block. For example, in min, change this:
1
2
3
4
5
for(int m=1; m<=i; m++)

if(j[m]<l)

l=j[m];

to this:
1
2
3
4
5
for(int m=1; m<=i; m++) {
    if(j[m]<l) {
        l=j[m];
    }
}
if i were to write it with the std :: <vector> how would i re write my program so that it sorts in ascending order the inputed numbers from the user and displays the average correctly ? we are learning this currently in class but she doesn't really explain it very well… and has a heavy accent that is hard to understand. but i am trying to write it using vectors
Last edited on
Only one way that's using namespace "...".

For example:

if you're using the #include<iostream> you can use :

#include<iostream>

using namespace std;

---> It will help you don't need to use the "std" prefix.

Last edited on
At the top you will need
1
2
#include<vector>
#include <algorithm> 


You can define the vector as std::vector<int> numbers;
Add a value with
1
2
cin >> k;
numbers.push_back(k);


Sort the numbers with
std::sort(numbers.begin(), numbers.end())

Get the min and max with
1
2
x = std::min_element(numbers.begin(), numbers.end());
x = std::max_element(numbers.begin(), numbers.end());

You'll still have to compute the average yourself.
Topic archived. No new replies allowed.