Now that we've gotten that i thing out of the way, let's focus on that darn sorting algorithm.
Now that we've gotten that i thing out of the way, let's focus on that darn sorting algorithm.
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-compliantCode:#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.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; }
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);should beCode:int finish = 1;
Did you guys miss that, or were you trying to torture me by forcing me to figure it out on my own?Code:finish = 1;
Also, I guess declaring i globally is the best solution to that whole i issue.
No, I didn't miss that. That is why I posted earlier about checking your variable declarations.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?
For heaven's sake, no!Originally posted by volk
Also, I guess declaring i globally is the best solution to that whole i issue.
Declare it at the start of the function if you must, but NOT globally!
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Yes, it would definately be worth your while to look into vectors and other STL containers like lists and maps and queues.Sheesh, what the hell is that?! I guess it's time I learn vectors.
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 atThat kind of looks like you're giving up there, PorkyChop. Resorting to the sorting algorithm in <algorithm>?
www.gamedev.net
global variables != good practice
What's wrong with global variables (just when I thought this thread was dead)?
Also, I still didn't get an answer to...
...from HaLCy0n, swoopy, Magos, CornedBee, and PorkChop. <---Try typing all of those names with a straight face.Code:Did you guys miss that, or were you trying to torture me by forcing me to figure it out on my own?
That they are accessible from everywhere makes them vulnerable to sudden changes. Imagine a simple scenario using a global i as you said:Originally posted by volk
What's wrong with global variables (just when I thought this thread was dead)?
Both functions modify the same variable. Try to figure out what happens.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]); } }
Wouldn't not answering that make us seem more like gurus?
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?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Um...no...Originally posted by CornedBee
Wouldn't not answering that make us seem more like gurus?
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
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
Can you explain what that does in great detail? Also, I always thought #define declarations were considered bad.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
(When will this thread ever die...)
How would that help? It doesn't introduce a new block, it doesn't do anything.
Damn.Um...no...
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
That code does nothing? So, was XSquared trying to be funny?Code:#define for if( 0 ); else for