Random group of numbers: highest, smallest, and average value

Hi guys, is there anyone here who could help me with this one?
I m basically creating a program which prints out 50 random numbers from 0 to 100. then it has to show the smallest value, the highest value and the average number of that group. Subsequently if the user press any buttom, the program will show a number for every line. The only issue is that when I run the program the highest, smallest, and average number are equal to 0. Can you guys tell me what s wrong with my code please ?:)

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
int input;
int array [50];
 int sort ()
 {
    srand(time(NULL));
    for (int i=0; i<50; i++)
    {

       array [i] =rand()% 100;
    }

 }

 int display()
 {
     cout << "Original list"<<endl;
     cout <<"{ ";
     for (int i=0; i<50; i++)
     {
         if (i !=0)
         {
            cout<<", ";
         }
         cout<<array[i];
     }
     cout << " }"<<endl;
 }

 int highest ()
 {
     int HighestValue=array[0];
     for (int i=0;i<50;i++)
     {
         if (array[i]> HighestValue)
         {
             array [i]=HighestValue;
         }
     }
     return HighestValue;
 }

 int lowest ()
 {

     int LowestValue=0;
     for (int i=0;i<50;i++)
     {
         if (array[i]< LowestValue)
         {
             array [i]=LowestValue;
         }
     }
     return LowestValue;
 }

 int average ()
 { int sum = 0;
     for (int i=0; i<50; i++)
     {
        sum+= array [i];
     }
     return sum;
     double result = sum / 50;

 }

 int list ()
 {
     for (int i =0; i<50; i++)
     {
        cout<<array[i];
        cout<<endl;
     }
 }
 int LowestValue;
 int HighestValue;
 double result;
 int main ()
 {
     sort();
     display ();
     highest ();
     cout << "The highest number within this group is: "<<HighestValue<<endl;
     lowest ();
     cout << "The lowest number within this group is: "<<LowestValue<<endl;
     average ();
     cout <<" The average value of this list is: "<<result<<endl;
     cout << "If you want to list all the values press any button"<<endl;
     cin >> input;
     list ();
 }
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
 int highest ()
 {
     int HighestValue=array[0];
     for (int i=0;i<50;i++)
     {
         if (array[i]> HighestValue)
         {
             array [i]=HighestValue; // kbw: is this right?
         }
     }
     return HighestValue;
 }

 int lowest ()
 {

     int LowestValue=0;
     for (int i=0;i<50;i++)
     {
         if (array[i]< LowestValue)
         {
             array [i]=LowestValue; // kbw: is this right?
         }
     }
     return LowestValue;
 }
closed account (E0p9LyTq)
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <limits>

int array[50];

void makeList()
{
   for (int i = 0; i < 50; i++)
   {

      array[i] = rand() % 100;
   }
}

void displayList()
{
   std::cout << "Original list:\n";
   std::cout << "{ ";

   for (int i = 0; i < 50; i++)
   {
      if (i != 0)
      {
         std::cout << ", ";
      }
      std::cout << array[i];
   }
   std::cout << " }\n";
}

int getHighest()
{
   int HighestValue = std::numeric_limits<int>::min();

   for (int i = 0; i < 50; i++)
   {
      if (array[i] > HighestValue)
      {
         HighestValue = array[i];
      }
   }

   return HighestValue;
}

int getLowest()
{
   int LowestValue = std::numeric_limits<int>::max();

   for (int i = 0; i < 50; i++)
   {
      if (array[i] < LowestValue)
      {
         LowestValue = array[i];
      }
   }
   return LowestValue;
}

double getAverage()
{
   int sum = 0;

   for (int i = 0; i < 50; i++)
   {
      sum += array[i];
   }

   // return sum; // 
   double result = sum / 50;
   return result;
}

void getList()
{
   for (int i = 0; i < 50; i++)
   {
      std::cout << array[i] << '\t';
   }
   std::cout << '\n';
}

int main()
{
   srand(time(NULL));

   makeList();

   displayList();

   int HighestValue = getHighest();
   std::cout << "The highest number within this group is: " << HighestValue << '\n';

   int LowestValue = getLowest();
   std::cout << "The lowest number within this group is: " << LowestValue << '\n';

   double result = getAverage();

   std::cout << "The average value of this list is: " << result << '\n';
   std::cout << "Enter any key to get the list of numbers: ";
   int input;
   std::cin >> input;

   getList();
}

Original list:
{ 77, 96, 20, 73, 56, 35, 7, 33, 4, 85, 82, 61, 52, 84, 5, 26, 23, 31, 77, 60, 43, 98, 6, 81, 24, 57, 8, 1, 51, 70, 39, 11, 20, 59, 33, 79, 58, 75, 58, 44, 83, 43, 27, 32, 55, 16, 12, 57, 89, 31 }
The highest number within this group is: 98
The lowest number within this group is: 1
The average value of this list is: 46
Enter any key to get the list of numbers: a
77      96      20      73      56      35      7       33      4       85
82      61      52      84      5       26      23      31      77      60
43      98      6       81      24      57      8       1       51      70
39      11      20      59      33      79      58      75      58      44
83      43      27      32      55      16      12      57      89      31

Hello gdz98,

In addition to what FurryGuy has shown you. I would ask why all the global variables? You should avoid using global variables unless they start with "constexpr" or const. As a global variable the whole file has access to them and anything in the file can change the value.

You should compile the program before posting because when I tried it did not compile.

Line 5 caused a problem.

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy. And not all at once to try to keep a job.

What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.

Every line that contains "array[i]" because line 5 wants to put "std::" in front of "array" and since there is a "std::array" the compiler does not know which one to use.

"srand" only needs done once in main and works better this way:
srand(static_cast<std::size_t>(time(NULL)));. "srand" requires an unsigned int for its number, but "time" returns a "time_t" which is not an unsigned int.

All of your functions except main return an "int", but there is nothing in the functions that needs to be returned and there is no return at the end of these functions. In the end I changed all the "int"s to "void"s.

Hope that helps,

Andy

Edit:

My mistake. The functions "highest", "lowest" and "average do return a value, byt is is never used in main.

In the "average" function the last two lines are reversed. You should get your answer into result first then return result.

Since "highest" and "lowest" return a value the "cout" statements in main can just call these functions and it will, in the end, will print the returned value.

I am not sure yet, but you have defined three global variables after the function definitions. These variables may not be available to what is above them, but only available to what comes after. I have to check this out. If you insist on using global variables put them at the top of the program.
Last edited on
Hi FurryGuy thanks a lot for your time, but I forgot to tell you that I m a really beginner and I haven studied the limits yet.... so I don t know what s their function... is there any other way to avoid using them?
Topic archived. No new replies allowed.