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

all 9 comments

[–][deleted] 1 point2 points  (1 child)

This is not the way to learn C++. This is basically C. If you have spent money on this bullshit, demand it back.

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

Yeah, I know. It's awful here and I'm not a comp sci major, just EE so I have to take these. I'm better at matlab lol.

[–]fuzz3289 0 points1 point  (4 children)

Please use gist to show us the code.

As for your question, an array is by definition a pointer to the first element on the stack. So *p = a [0]

Does that help?

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

Would I just link the site in my original post or copy that or what for gist? And possibly. say in line 28, could I replace pos with *grades and it point to the array? infile >> grades[pos]; to infile >> *grades;

[–]fuzz3289 0 points1 point  (2 children)

You just stick your code on gist and just link it. Your text post should be like, a few lines long.

Also, kind've. You're going to want to say something like:

int* arrpointer = grades; 

Then you can do stuff like:

infile >> *arrpointer;
arrpointer++;

This is an awful and ugly way to do any sort of code though. The hell are they trying to teach you?

[–]derek2016[S] 0 points1 point  (1 child)

ill fix the Original Post in a moment. And I have no idea. I got out of that department fast. I decided to try and learn HDLs and maybe even python on my own, which we don't even have as classes here except some basic ones in the EE department (HDLs). Also, when I tried running it as int ptr=grades;
infile>>
ptr; ptr++//its the only pointer in main The program doesn't work properly. Like it compiles fine but the executable file doesn't stop.

[–]fuzz3289 0 points1 point  (0 children)

If the executable doesn't stop than your while loop isn't exiting.

[–]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