Can someone check my code?

Hello! I'm working on this assignment that wants us to write a program that assignment 40 random numbers between 50 and 100 to an array. We then have to print out the average of the maximum and minimum values in the array. I'm using Xcode and it says there are no errors but it won't compile. I can't find the error either :( My main problem is that I'm not sure how to populate the array... I'm sure my function for populateArray is incorrect but I don't know how to fix it. can someone help me out?

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

void populateArray(int array[], int);
int arrayMin(int array[], int);
int arrayMax(int array[], int);
float averageOf2Ints(int, int);

int main()
{
    const int size = 40;
    int array[size];
    srand(time_t(NULL));
    
    populateArray(array, size);
    int minimum = arrayMin(array, size);
    int maximum = arrayMax(array, size);
    
    cout << setprecision(1) << fixed << averageOf2Ints(minimum,maximum) << endl;
    return 0;
}

void populateArray(int array[], int size)
{
    for (int size = 0; size < 40; size++)
    {
        array[size] = 50 + rand() % 100;
        cout << array[size] << endl;
    }
}

int arrayMin(int array[], int size)
{
    int lowest = array[0];
    for (int pos = 0; pos < size; pos++)
        if (array[pos] < lowest)
            lowest = array[pos];
        
    return lowest;
}

int arrayMax(int array[], int size)
{
    int highest = array[0];
    
    for (int pos = 0; pos < size; pos++)
        if (array[pos] > highest)
            highest = array[pos];
    
    return highest;
}

float averageOf2Ints(int minimum, int maximum)
{
    
    return ((minimum + maximum) / 2);
}
I'm using code::block and it's compiling...

I think populateArray should be:

1
2
3
4
5
6
7
8
void populateArray(int array[], int size)
{
    for (int i = 0; i < size; i++)
    {
        array[i] = 50 + rand() % 100;
        cout << array[i] << endl;
    }
}


and the return of averageOf2Ints:

 
    return ((minimum + maximum) / 2.0);


but, I'm also a beginner...
Line 28: You're using the same variable name for your loop variable as you have for the argument. Not a good practice. Why have you hard-coded 40 as the limit for the array? Shouldn't you be using the the argument passed in?

Line 30: The assignment says to generate numbers between 50 and 100. Your statement is going to generate numbers between 50 and 150.
 
  array[i] = 50 + rand() % 50;


Line 59: You're doing integer division. If you want a floating point result, you should be doing floating point division as elaleph suggested.

there are no errors but it won't compile

That statement makes no sense. If you have no errors, it should execute. If you have errors, it won't execute. Program compiles and executes for me as written.

Your program may be executing so quickly that you don't see the output. See the following thread:
http://www.cplusplus.com/forum/beginner/1988/


Topic archived. No new replies allowed.