# Thread: pointer and memory allocation trouble

1. Now that we've gotten that i thing out of the way, let's focus on that darn sorting algorithm.

2. Code:
```#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int    average(vector<int> vec) {
int ret = 0;

for(int i = 0;i < vec.size();i++) ret += vec[i];
ret /= vec.size();

return ret;
}

int main() {

int scorenum;

//Get number of scores
cout <<"How many scores would you like to average? ";
cin >>scorenum;
if(scorenum < 1) return 0;

//Make a vector to hold individual scores
vector<int> scores(scorenum);

//Get each score
for(int i = 0;i < scorenum;i++) {
cout <<"Test score #" <<i + 1 <<": ";
cin >>scores[i];
}

//Sort scores
sort(scores.begin(), scores.end());

//Sorted scores print
cout << "\nHere are the scores you entered in sorted form:" << endl;

for (int i = 0; i < scorenum; i++)
cout <<scores[i] << endl;

//Average time!!!
cout << "\nThe average of the scores is " << average(scores) << endl;

return 0;
}```
All behold the power of the C++ STL!! Use the STL!! Vectors are your friends. Yes, i should be declared again in the second for-loop. MSVC doesn't like that because it is not standard-compliant

3. Originally posted by PorkyChop
Code:
```#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int    average(vector<int> vec) {
int ret = 0;

for(int i = 0;i < vec.size();i++) ret += vec[i];
ret /= vec.size();

return ret;
}

int main() {

int scorenum;

//Get number of scores
cout <<"How many scores would you like to average? ";
cin >>scorenum;
if(scorenum < 1) return 0;

//Make a vector to hold individual scores
vector<int> scores(scorenum);

//Get each score
for(int i = 0;i < scorenum;i++) {
cout <<"Test score #" <<i + 1 <<": ";
cin >>scores[i];
}

//Sort scores
sort(scores.begin(), scores.end());

//Sorted scores print
cout << "\nHere are the scores you entered in sorted form:" << endl;

for (int i = 0; i < scorenum; i++)
cout <<scores[i] << endl;

//Average time!!!
cout << "\nThe average of the scores is " << average(scores) << endl;

return 0;
}```
Sheesh, what the hell is that?! I guess it's time I learn vectors.

That kind of looks like you're giving up there, PorkyChop. Resorting to the sorting algorithm in <algorithm>?

Also, I figured out the problem with my sorting algorithm:

Code:
```	int hold;
int finish;

do
{
finish = 0;

for (i = 0; i < number - 1; i++)
{
if (*(array + i) > *(array + i + 1))
{
hold             = *(array + i);
*(array + i)     = *(array + i + 1);
*(array + i + 1) = hold;

int finish = 1;
}
}
}while (finish != 0);```
Code:
`int finish = 1;`
should be
Code:
`finish = 1;`
Did you guys miss that, or were you trying to torture me by forcing me to figure it out on my own?

Also, I guess declaring i globally is the best solution to that whole i issue.

4. Originally posted by volk
Did you guys miss that, or were you trying to torture me by forcing me to figure it out on my own?
No, I didn't miss that. That is why I posted earlier about checking your variable declarations.

5. Originally posted by volk
Also, I guess declaring i globally is the best solution to that whole i issue.
For heaven's sake, no!
Declare it at the start of the function if you must, but NOT globally!

6. Sheesh, what the hell is that?! I guess it's time I learn vectors.
Yes, it would definately be worth your while to look into vectors and other STL containers like lists and maps and queues.

That kind of looks like you're giving up there, PorkyChop. Resorting to the sorting algorithm in <algorithm>?
Heh, I don't think "resorting" is the verb I would use there. That sort function is a good one(faster than the old C qsort function), and I prefer to use the stuff in the C++ standard library rather than recreating it myself when I really want to get something done. But, if you want to make a sort function yourself, there's a lot of good stuff on sorting at
www.gamedev.net

global variables != good practice

7. What's wrong with global variables (just when I thought this thread was dead)?

Also, I still didn't get an answer to...
Code:
`Did you guys miss that, or were you trying to torture me by forcing me to figure it out on my own?`
...from HaLCy0n, swoopy, Magos, CornedBee, and PorkChop. <---Try typing all of those names with a straight face.

8. Originally posted by volk
What's wrong with global variables (just when I thought this thread was dead)?
That they are accessible from everywhere makes them vulnerable to sudden changes. Imagine a simple scenario using a global i as you said:
Code:
```int i;

void func2(int *, int);

void func1() {
int array[20];
for(i = 19; i >= 0; --i) {
func2(array, i);
}
}

void func2(int *ar, int max) {
for(i = max; i >= 0; --i) {
++(ar[i]);
}
}```
Both functions modify the same variable. Try to figure out what happens.

Also, I still didn't get an answer to...
Code:
`Did you guys miss that, or were you trying to torture me by forcing me to figure it out on my own?`
Wouldn't not answering that make us seem more like gurus?

9. Originally posted by CornedBee

Wouldn't not answering that make us seem more like gurus?
Um...no...

10. There is another solution to the for loop issue. Just put this at the start of your code.
Code:
`#define for if( 0 ); else for`

11. Originally posted by XSquared
There is another solution to the for loop issue. Just put this at the start of your code.
Code:
`#define for if( 0 ); else for`
Can you explain what that does in great detail? Also, I always thought #define declarations were considered bad.

(When will this thread ever die...)

12. How would that help? It doesn't introduce a new block, it doesn't do anything.

Um...no...
Damn.

13. Code:
`#define for if( 0 ); else for`
That code does nothing? So, was XSquared trying to be funny?

Popular pages Recent additions