pointer and memory allocation trouble

This is a discussion on pointer and memory allocation trouble within the C++ Programming forums, part of the General Programming Boards category; Now that we've gotten that i thing out of the way, let's focus on that darn sorting algorithm....

  1. #16
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Now that we've gotten that i thing out of the way, let's focus on that darn sorting algorithm.

  2. #17
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    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. #18
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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. #19
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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!
    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

  6. #21
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    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. #22
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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?
    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

  9. #24
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by CornedBee

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

  10. #25
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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

  11. #26
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How would that help? It doesn't introduce a new block, it doesn't do anything.


    Um...no...
    Damn.
    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

  13. #28
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Code:
    #define for if( 0 ); else for
    That code does nothing? So, was XSquared trying to be funny?

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21