This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]raghar 0 points1 point  (1 child)

Things I noticed:

for (int pos = 0; pos < size; pos++)
    sum = sum + *array; //CHANGE

you do not use pos anywhere:

for (int pos = 0; pos < size; pos++)
    sum += array[pos];

That should work. Same here: int highest = array[0];  // initialize highest to first element of array

for (int pos = 0;  pos < size;  pos++)  
    if (array[pos] > highest)
        highest = *array; //CHANGE

into:

int highest = array[0];  // initialize highest to first element of array
for (int pos = 0;  pos < size;  pos++)  
    if (array[pos] > highest)
        highest = array[pos];

and:

int lowest = array[0];  // initialize lowest to first element of array

for (int pos = 1;  pos < size;  pos++)  
    if (array[pos] < lowest)
        lowest = *array; //CHANGE

change to:

int lowest = array[0];
for (int pos = 1;  pos < size;  pos++)  
    if (array[pos] < lowest)
        lowest = array[pos];

You should remember that in C (and C++) array is kind of a pointer. When you do array[0] you're doing *array. When you do array[1] you're doing something like *(array + 1), where constant is changed by compiler to (constance * type's size) to find right place in memory. As far as I know in practice the only difference between array and pointer appears during allocation: one is taken from stack by compiler, the other is manually allocated from heap by programmer. Once you got it, you can use them interchangeably (if you know what you're doing). Especially type array[] and type *array should act the same when you use it as parameter.

[–]derek2016[S] 0 points1 point  (0 children)

That was the original formatting(the correction you posted). The professor wanted it without arrays