Help With a Counter

This is a discussion on Help With a Counter within the C++ Programming forums, part of the General Programming Boards category; Greeting again. I have a homework assigment that I worked on a long time ago and now I need to ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    52

    Help With a Counter

    Greeting again. I have a homework assigment that I worked on a long time ago and now I need to add something. I need to add in a cycle counter... More or less a counter that counts the number of cycles this radix sort takes. Can anyone give me a habd I really don't know where to add the counters. Thanks

    Code:
    #include "stdafx.h"
    
    // Based off orginal LabShell.cpp
    // Which defined the entry point for the console application.
    
    #define _TCHAR char
    
    #include <fstream>
    #include <iostream>
    #include <ctime>
    #include <vector>
    //#include <cassert>
    using namespace std;
    void genData(int *, int);
    int radixSort(int *, int);
    long count = 0;
    
    void genData(long *dta, int n) 
    {// generates social security numbers that do not contain zeroes
    #define limit 1000000 // make this number whatever you want - we are just looking at a sample of the data to make sure it is correct 
    
    	ofstream f;// open a file to write a sample of the output
    	
    	//srand(time(0));
    	if(n < limit)
    	{
    		f.open("data.txt", ios::out);
    		if(!f.is_open())
    		{
    			cout << "SSN sample file did not open" << endl;
    			system("pause");
    			exit(0);
    		}
    	}
    	
    	for(int i=0; i < n; i++)// generate the social security numbers at random
    	{
    		dta[i] =  rand()%889 + 111 + 1000*(rand()%889 + 111) + 1000000*(rand()%889 + 111);
    		if(dta[i] > 999999999) dta[i] = dta[i] % 1000000000;
    		if(n < limit)f << i<< " " << dta[i] << endl;// write them out if in the sample you selected
    	}
    	if(f.is_open())f.close();// if the file is open, close it
    }
    
    void sort(long*, long*, long);
    long* radixSort(long *dta, int n)
    {
    	long* dest = new long[n];
    	sort(dta, dest, n);
    
    	return dest; // this is a dummy routine - you need to write your own that sorts the data and counts the operations	
    }
    
    void print_data(const char* title, long* data, int count)
    {
    	cout << "--------" << title << "--------" << endl;
    	for(int i=0; i<count; ++i)
    	{
    		cout << data[i] << endl;
    	}
    }
    
    //      main program to run the radix sort graphing 
    int _tmain(int argc, _TCHAR* argv[])
    {
    	long *data;// pointer to where SSNs will be stored
    	int N=2;// initial size of the SSN data set.
    
    	ofstream f;
    	f.open("output.csv", ios::out);// open the output excel file
    	if(f.is_open())// if it opened, proceed, else issue error and quit
    	{
    		for(int i = 0; i < 15; i++)
    		{// generate 15 sets of data the first of size 2, the next of size 4, until you get to 32768 (2 ^ 15)
    			data = new long [N];// allocate memory to hold the data generated by genData
    			if(data != NULL) // did we get the memory we requested?
    			{
    				genData(data, N);// generate the SSNs and out them in the array
    				print_data("unsorted", data, N);
    				long* dest = radixSort(data, N); // sort the data and count the operations	
    				print_data("sorted", dest, N);
    				cout << "------\n";
    				f << N << "," << N << endl;// write the data pairs to the excel file for later graphing
    				cout << N << "," << N << endl;// write the data pairs to the screen also 
    				N = 2 * N;// double the data size for the next pass
    				delete [] data;// free the data array because it will be reallocated next time
    			}
    			else 
    			{// did not get the memory we asked for, issue error message and exit
    				cout  << "Out of memory" << endl;
    				system("pause");// wait for user to see the message
    				break;
    			}
    		}
    		f.close();// close the file
    	}
    	else
    	{// file did not open, so alert user and exit
    		cout << "csv output file did not open" << endl;
    		system("pause");
    	}
    	system("pause");
    	return 0;
    }
    
    // the first parameter is the radix which is used to collect the numbers
    // the second one is the number of data elements
    // in is the array of data
    // out is the result
    void radix(int byte, long N, long *in, long* out)
    {
    	//create buckets
    	vector<long> bucket[1000];
    
    	/*for(int x =0; x < 999; x++)
    	{
    		bucket[x][0] = 0;
    		for(int y = 0; y < 100; y++)
    		{
    			bucket[x][y]=0;
    		}
    		
    		//memset (bucket, 0, sizeof (bucket[x]));
    	}
    	*/
    	
    	//loop through the whole input set
    	for (int i = 0; i < N; i++)
    	{
    		if(in[i] < 0)
    		{
    			in[i] = in[i] * -1;
    		}
    		long temp = 0;
    		switch(byte)
    		{
    		case 0:
    			temp = in[i] % 1000;
    			break;
    		case 1:
    			temp = in[i] / 1000;
    			temp = temp % 1000;
    			break;
    		case 2:
    			temp = in[i] / 1000000;
    		}
    		
    		
    
    		//temp is now the index number for the bucket
    
    		//we need to find an empty place in the bucket 
    		//int t = 0;
    		//while(bucket[temp]. != 0)
    		/*{
    			t++;
    			//cout << "temp= " << temp << "in= "<< in[i] << "I= " << i << "Bucket = " << bucket[temp][t] << endl;
    		}*/
    		//place number in bucket
    		bucket[temp].push_back(in[i]);
    
    	}
    	//get numbers from buckets, put in array
    	
    	//currentI is the position on the out array
    	int currentI = 0;
    	for(i = 0; i< 1000; i++)
    	{
    		//t is the position in the bucket
    		int t = 0;
    		if(bucket[i].size() > 0)
    		{
    			for(int x = 0; bucket[i].size() > x ; x++)
    			{
    				out[currentI] = bucket[i].at(x);
    				currentI++;
    				//assert(currentI > (N + 1));
    				//cout << currentI;
    			}
    		}
    	}
    	if(currentI != N) cout << " N = " << N << "  CurrentI = " << currentI << endl; 
    }
    
    	
    
    
    void sort(long *in, long *out, long count)
    {
    	long *tmp = out;
    	out = new long [count];
    	
    	// collect radices for each byte
    	radix(0, count, in, out);
    
    	in = out;
    	// create a new output list
    	out = new long [count];
    	
    	//pass 2
    	radix(1, count, in, out);
    
    	in = out;
    	// use output list from calling function
    	out = tmp;
    
    	//pass 3
    	radix(2, count, in, out);
    
    }
    
    /*int main()
    {
    	return _tmain(0, NULL);
    }
    */

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    else
    	{// file did not open, so alert user and exit
    		cout << "csv output file did not open" << endl;
    		system("pause");
    	}
    You don't exit there.

    You don't free any of your newed memory.

    And just so you know, srand() is in <cstdlib>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    one more thing: avoid system() calls, especially for something as trivial as pausing... and secondly, how did you manage to create a radix sort if you don't know how to create something as simple as a counter?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    ^ Bit suspicious...Anyways, I'd just chuck it at the beginning of the radix sort function, incrementing each time the function is entered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 02:17 AM
  3. Flowchart Question
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 04-08-2007, 11:33 AM
  4. Counter Heap Sort
    By Achillles in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 12:17 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 12:58 PM

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