Two Dice

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3528 Views - Last Post: 18 November 2008 - 02:07 PM Rate Topic: -----

#1 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Two Dice

Post icon  Posted 14 November 2008 - 07:34 AM

Write a program that simulates the rolling of two dice. The program should use
rand() to roll the first die and should use rand() again to roll the second die.
The sum of the two values should then be calculated. Your program should
roll the dice 120,000 times. Tally the numbers of
times each possible sum appears. Print the results in tabular format. Also,
determine if the totals are reasonable, i.e. there are six ways to roll a 7, so
approximately one-sixth of all the rolls should be 7.

the output must come out like this:

Sum Total Expected Actual
2 992 2.778% 2.756%
3 1936 5.556% 5.378%
4 3000 8.333% 8.333%
5 3981 11.111% 11.058%
6 4962 13.889% 13.783%
7 6036 16.667% 16.767%
8 5126 13.889% 14.239%
9 4076 11.111% 11.322%
10 2933 8.333% 8.147%
11 1953 5.556% 5.425%
12 1005 2.778% 2.792%

and my program output come out like this:

Sum Total Expected Actual
2 5 0% 0%
3 5 0% 0%
4 5 0% 0%
5 5 0% 0%
6 5 0% 0%
7 5 0% 0%
8 5 0% 0%
9 5 0% 0%
10 5 0% 0%
11 5 0% 0%
12 5 0% 0%

here's my code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>




int Sum, Expected, Actual;

using namespace std;



int main() {
int Dice1;
int Dice2;
int Total = 0;
	srand(time(0));
	cout << "Sum" << setw(10) << "Total" << setw(13) << "Expected" << setw(12) << "Actual";
	cout << endl;
  
  
for (int i = 0; i <= 120000; i++) { 
	  
		 Dice2= 1 + rand() % 9;
		 Dice1= 1 + rand() % 9;
		 Total = Dice1 + Dice2;
		 Total++;
		 
		  
		 
}


for (int r = 2; r <= 12; r++) {
	cout << r << setw(10) << Total << setw(13) << setprecision(3) << r / 36 << " %" << setw(12);
	cout << setprecision(3) << r /120000 << " %";
	cout << endl;
}
		
		
system("pause");	  
return 0; 
} 




Is This A Good Question/Topic? 0
  • +

Replies To: Two Dice

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Two Dice

Posted 14 November 2008 - 07:45 AM

Your total is calculated as the addition of dice1 and dice2. I imagine you want a running tally of this and should use:

Total += Dice1 + Dice2;


Was This Post Helpful? 0
  • +
  • -

#3 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Two Dice

Posted 14 November 2008 - 07:51 AM

View PostKYA, on 14 Nov, 2008 - 06:45 AM, said:

Your total is calculated as the addition of dice1 and dice2. I imagine you want a running tally of this and should use:

Total += Dice1 + Dice2;



now, for the total for all the number, i get 1230028 with
Total += Dice1 + Dice2;


and all the totals should be different, why are they coming out as the same?
Was This Post Helpful? 0
  • +
  • -

#4 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Two Dice

Posted 14 November 2008 - 08:08 AM

You could use an array to store the different totals. Otherwise you just sum up all the throws.

for (int i = 0; i <= 120000; i++) { 
	  
		 Dice2= 1 + rand() % 9;
		 Dice1= 1 + rand() % 9;
		 Total = Dice1 + Dice2;
		 Total++;
		 
		  
		 
}




First of all, the above is 120001 throws.
Instead of Total++, use an array Totals[13] <-- Skip the first 2 indexes
Totals[Total]++;
Was This Post Helpful? 0
  • +
  • -

#5 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Two Dice

Posted 14 November 2008 - 08:37 AM

View PostGloin, on 14 Nov, 2008 - 07:08 AM, said:

You could use an array to store the different totals. Otherwise you just sum up all the throws.

for (int i = 0; i <= 120000; i++) { 
	  
		 Dice2= 1 + rand() % 9;
		 Dice1= 1 + rand() % 9;
		 Total = Dice1 + Dice2;
		 Total++;
		 
		  
		 
}




First of all, the above is 120001 throws.
Instead of Total++, use an array Totals[13] <-- Skip the first 2 indexes
Totals[Total]++;


so i have to declare int Total as int Total[13]
and code this??
for (int i = 1; i <= 120000; i++) { 
	  
		 Dice2= 1 + rand() % 9;
		 Dice1= 1 + rand() % 9;
		 Total[13] = Dice1 + Dice2;
		 Total[13]++;
		 
		  
		 
}



Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Two Dice

Posted 14 November 2008 - 09:07 AM

Total[i] = //stuff



Otherwise it is all going in that same slot.
Was This Post Helpful? 0
  • +
  • -

#7 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Two Dice

Posted 14 November 2008 - 02:24 PM

View PostKYA, on 14 Nov, 2008 - 08:07 AM, said:

Total[i] = //stuff



Otherwise it is all going in that same slot.



yeah...i'm still having a problem with the arrays and still giving me the same total.... :(
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>




int Sum, Expected, Actual;

using namespace std;



int main() {
int Dice1;
int Dice2;
int Total[13] = {0};
	srand(time(0));
	cout << "Sum" << setw(10) << "Total" << setw(13) << "Expected" << setw(12) << "Actual";
	cout << endl;
  
  
for (int i = 1; i <= 120000; i++) { 
	  
		 Dice2= 1 + rand() % 9;
		 Dice1= 1 + rand() % 9;
		 Total[13] += Dice1 + Dice2;
		 Total[13]++;

		 
		  
		 
}


for (int r = 2; r <= 12; r++) {
	cout << r << setw(10) << Total[13] << setw(13) << setprecision(3) << r / 36 << " %" << setw(12);
	cout << setprecision(3) << r /120000 << " %";
	cout << endl;
}
		
		
system("pause");	  
return 0; 
} 



Was This Post Helpful? 0
  • +
  • -

#8 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Two Dice

Posted 14 November 2008 - 02:45 PM

See KYAs post about the index i instead of 13.

BTW, why % 9? A dice is 1-6, right?

Better show you like this..

tot = Dice1 + Dice2;
Total[tot]++;

First calculate the sum of the two dice. The sum will represent an index of the array.
Then increment the post with the index tot.

Also, you're still looping 1 too many (120001)

This post has been edited by Gloin: 14 November 2008 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 barnwillyb   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: Two Dice

Posted 14 November 2008 - 06:35 PM

View Postqwert12345, on 14 Nov, 2008 - 06:34 AM, said:

Write a program that simulates the rolling of two dice. The program should use
rand() to roll the first die and should use rand() again to roll the second die.
The sum of the two values should then be calculated. Your program should
roll the dice 120,000 times. Tally the numbers of
times each possible sum appears. Print the results in tabular format. Also,
determine if the totals are reasonable, i.e. there are six ways to roll a 7, so
approximately one-sixth of all the rolls should be 7.

the output must come out like this:

Sum Total Expected Actual
2 992 2.778% 2.756%
3 1936 5.556% 5.378%
4 3000 8.333% 8.333%
5 3981 11.111% 11.058%
6 4962 13.889% 13.783%
7 6036 16.667% 16.767%
8 5126 13.889% 14.239%
9 4076 11.111% 11.322%
10 2933 8.333% 8.147%
11 1953 5.556% 5.425%
12 1005 2.778% 2.792%

and my program output come out like this:

Sum Total Expected Actual
2 5 0% 0%
3 5 0% 0%
4 5 0% 0%
5 5 0% 0%
6 5 0% 0%
7 5 0% 0%
8 5 0% 0%
9 5 0% 0%
10 5 0% 0%
11 5 0% 0%
12 5 0% 0%

here's my code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>




int Sum, Expected, Actual;

using namespace std;



int main() {
int Dice1;
int Dice2;
int Total = 0;
	srand(time(0));
	cout << "Sum" << setw(10) << "Total" << setw(13) << "Expected" << setw(12) << "Actual";
	cout << endl;
  
  
for (int i = 0; i <= 120000; i++) { 
	  
		 Dice2= 1 + rand() % 9;
		 Dice1= 1 + rand() % 9;
		 Total = Dice1 + Dice2;
		 Total++;
		 
		  
		 
}


for (int r = 2; r <= 12; r++) {
	cout << r << setw(10) << Total << setw(13) << setprecision(3) << r / 36 << " %" << setw(12);
	cout << setprecision(3) << r /120000 << " %";
	cout << endl;
}
		
		
system("pause");	  
return 0; 
} 




/* Problem: roll two six-sided die 120,000 times. */

//——————————————————————————————————————————————————————————
//                          includes
//——————————————————————————————————————————————————————————

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

/*__________________________________________________________*/
/*																													*/
/*                         constants												*/
/*__________________________________________________________*/

const int ARRAY_SIZE = 13;
const int NUMBER_OF_ROLLS = 120000;

//——————————————————————————————————————————————————————————
//                        prototypes
//——————————————————————————————————————————————————————————

int random_generator( int, int );
int throw_dice();
char * dice_face( int );
char * insert_comma( unsigned int );

//——————————————————————————————————————————————————————————
//                       main listing
//——————————————————————————————————————————————————————————

int main () {
	int rolls_of_dice[ARRAY_SIZE] = { 0 };
	int expected_size[ARRAY_SIZE] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };

	// set precision to 3 decimals
	cout << setprecision( 3 ) << setiosflags( ios::fixed | ios::showpoint );

	cout << "\n\tDice Face" << setw( 20 ) << "Rolls of Dice" << setw( 17 ) << "Expected" << setw( 18 ) << "Actual" << endl;
	cout << " ———————————" << setw( 50 ) << "———————————————" << setw( 37 ) << "——————————" << setw( 39 ) << "——————————" << endl;

	/* initialize random seed */
	srand( static_cast<unsigned int>(time( NULL)) );

	for (int roll = 1; roll <= NUMBER_OF_ROLLS; roll++)												
		++rolls_of_dice[throw_dice()];

	// dice face starts at 2 and ends at 12
	for (int face = 2; face <= 12; face++)
		cout << setw( 10 ) << dice_face( face )
				 // fill across to frequency
				 << ' ' <<  setfill( '.' ) << setw( 11 ) 
				 << ' ' << setfill( ' ' ) << setw( 6 )
				 // place comma in frequency number and print it
				 << insert_comma( rolls_of_dice[face] ) << ' '
				 // fill across to expected
				 <<  setfill( '.' ) << setw( 12 ) << ' '
				 // stop filling with period( '.' )
				 << setfill( ' ' ) << setw( 6 )
				 // print expected percentage
				 << 100.0 * expected_size[face]/36 << "%" << ' '
				 // fill across to actual
				 << setfill( '.' ) << setw( 11 ) << ' '
				 // stop filling with period( '.' )
				 << setfill( ' ' ) << setw( 6 )
				 // print actual percentage
				 << 100.0 * rolls_of_dice[face]/NUMBER_OF_ROLLS << "%" << setfill( ' ' ) << endl;

	cout << " ———————————" << setw( 50 ) << "———————————————" << setw( 37 ) << "——————————" << setw( 39 ) << "——————————" << endl;

	return 0; 
}

// GENERATE RANDOM NUMBERS
int random_generator( int low, int high ) {
	return (rand() % (high - low + 1)) + low;
}

// SIMILATE ROLLING A PAIR OF DICE
int throw_dice() {
	int die_one, die_two, dice;

	// roll first die
	die_one = random_generator( 1, 6 );
	// roll second die
	die_two = random_generator( 1, 6 );
	// get dice total
	dice = die_one + die_two;

	return dice;
}

// DICE FACE STRINGS
char * dice_face( int face ) {
  switch (face) {
    case  2: return "Two's"; break;
    case  3: return "Three's"; break;
    case  4: return "Four's"; break;
    case  5: return "Five's"; break;
    case  6: return "Six'es"; break;
    case  7: return "Seven's"; break;
    case  8: return "Eight's"; break;
    case  9: return "Nine's"; break;
    case 10: return "Ten's"; break;
    case 11: return "Eleven's"; break;
    case 12: return "Twelve's"; break;
    default: return "Illegal Face";
  }
}

// INSERT COMMAS INTO A NUMBER
char * insert_comma( unsigned int num ) {
	static int comma = '\0';
	static char temp[30];
	char * numPtr = &temp[sizeof(temp)-1];
	int i = 0;

	memset( temp, 'x', sizeof temp );
	
	if (comma == '\0') {
		struct lconv * lcp = localeconv();
		if (lcp != NULL) {
			if (lcp->thousands_sep != NULL && * lcp->thousands_sep != '\0')
				comma = * lcp->thousands_sep;
			else
				comma = ',';
		}
	}
	*numPtr = '\0';

	do {
    if (i % 3 == 0 && i != 0)
      *--numPtr = comma;
    *--numPtr = '0' + num % 10;
    num /= 10;
		i++;
	} while (num != 0);

	return numPtr;
}


Enjoy!

This post has been edited by barnwillyb: 15 November 2008 - 01:55 AM

Was This Post Helpful? 0
  • +
  • -

#10 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Two Dice

Posted 17 November 2008 - 03:10 PM

I think the array problem is solved, but now I'm don't know if the expected and actual percentages are connected.

expected % (100.0 * r) / 36
actual % (100.0 * sum[ r ] ) /120000

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>






using namespace std;



int main() {
int Dice1;
int Dice2;
int sum [ 13 ] = { 0 };

srand(time(0));
   
	cout << "Sum" << setw(10) << "Total" << setw(13) << "Expected" << setw(12) << "Actual";
	cout << endl;
	
for (int i = 1; i < 120000; i++) { 
 
   Dice2 = 1 + rand() % 6;
	 Dice1 = 1 + rand() % 6;
	   sum [ Dice1 + Dice2 ] ++;  

	  
}		

		 
   for (int r = 2; r <= 12; r++) {
	cout << r << setw(10) << sum[ r ] << setw(13) << setprecision(3) << (100.0 * r) / 36 << " %" << setw(12);
	cout << setprecision(3) << (100.0 * sum[ r ] ) /120000  << " %";
	cout << endl;



	   
}
  


		
		
system("pause");	  
return 0; 
} 



Was This Post Helpful? 0
  • +
  • -

#11 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Two Dice

Posted 17 November 2008 - 03:15 PM

Hehe.. After all my nagging, you changed the loop to iterate 119,999 times instead of 120,001 times.. :P

Of course the actual and expected percentages are connected (dependant), although not equal.

This equation for the expected percentages is however incorrect..
expected % = (100.0 * r) / 36

This post has been edited by Gloin: 17 November 2008 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#12 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Two Dice

Posted 17 November 2008 - 03:19 PM

View PostGloin, on 17 Nov, 2008 - 02:15 PM, said:

Hehe.. After all my nagging, you changed the loop to iterate 119,999 times instead.. :P

Of course the actual and expected percentages are connected (dependant), although not equal.


lol...are you serious?!....should it be for (int i = 2; i <= 120000; i++)
does that mean my percentages are right??.....hopefully
Was This Post Helpful? 0
  • +
  • -

#13 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Two Dice

Posted 17 November 2008 - 03:35 PM

is the actual % right?? (fingers crossed)

i know that the expected % is based on the number of ways the two dice can roll a number, right?

This post has been edited by qwert12345: 17 November 2008 - 03:36 PM

Was This Post Helpful? 0
  • +
  • -

#14 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Two Dice

Posted 17 November 2008 - 03:39 PM

This is what you were using..

for (int i = 1; i < 120000; i++) { <-- 119,999 iterations

You also used

for (int i = 0; i <= 120000; i++) { <-- 120,001 iteration

You could use either

for (int i = 0; i < 120000; i++) {
or
for (int i = 1; i <= 120000; i++) {

both iterating 120,000 times

I haven't seen your percentages but the formula for actual is correct, the one for expected wasn't. In your first post you had the correct expected values..

2 : 1/36 [1, 1]
3 : 2/36 [1, 2][2, 1]
4 : 3/36 [1, 3][2, 2][3, 1]
5 : 4/36 [1, 4][2, 3][3, 2][4, 1]
6 : 5/36 [1, 5][2, 4][3, 3][4, 2][5, 1]
7 : 6/36 [1, 6][2, 5][3, 4][4, 3][5, 2][6, 1]
8 : 5/36 [2, 6][3, 5][4, 4][5, 3][6, 2]
9 : 4/36 [3, 6][4, 5][5, 4][6, 3]
10: 3/36 [4, 6][5, 5][6, 4]
11: 2/36 [5, 6][6, 5]
12: 1/36 [6, 6]
Was This Post Helpful? 0
  • +
  • -

#15 barnwillyb   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: Two Dice

Posted 18 November 2008 - 02:00 PM

View Postqwert12345, on 17 Nov, 2008 - 02:19 PM, said:

View PostGloin, on 17 Nov, 2008 - 02:15 PM, said:

Hehe.. After all my nagging, you changed the loop to iterate 119,999 times instead.. :P

Of course the actual and expected percentages are connected (dependant), although not equal.


lol...are you serious?!....should it be for (int i = 2; i <= 120000; i++)
does that mean my percentages are right??.....hopefully


Try this! It mighto be more suited.


//——————————————————————————————————————————————————————————
//                          includes
//——————————————————————————————————————————————————————————

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

/*__________________________________________________________*/
/*																													*/
/*                         constants												*/
/*__________________________________________________________*/

const int ARRAY_SIZE = 13;
const int NUMBER_OF_ROLLS = 120000;

//——————————————————————————————————————————————————————————
//                        prototypes
//——————————————————————————————————————————————————————————

int random_generator( int, int );
int throw_dice();
char * dice_face( int );
char * insert_comma( unsigned int );

//——————————————————————————————————————————————————————————
//                       main listing
//——————————————————————————————————————————————————————————

int main (int argc, char * const argv[]) {
	int rolls_of_dice[ARRAY_SIZE] = { 0 };
	double expected_size[ARRAY_SIZE] = { 0, 0, 2.8, 5.6, 8.3, 11.1, 13.9, 16.7, 13.9, 11.1, 8.3, 5.6, 2.8 };

	cout << "\n\tDice Face" << setw( 20 ) << "Rolls of Dice" << setw( 14 ) << "Actual" << setw( 16 ) << "Expected" << endl;
	cout << " ———————————" << setw( 50 ) << "———————————————" << setw( 35 ) << "——————————" << setw( 35 ) << "——————————" << endl;

	/* initialize random seed */
	srand( static_cast<unsigned int>(time( NULL)) );

	for (int roll = 1; roll <= NUMBER_OF_ROLLS; roll++)
		++rolls_of_dice[throw_dice()];

	// dice face starts at 2 and ends at 12
	for (int face = 2; face <= 12; face++)
		// print spacing over to Dice Face
		cout << setw( 10 )
				 // print Dice Face
				 << dice_face( face )
				 // print 1 space before fill
				 << ' ' 
				 // fill blank space with period( '.' )
				 <<  setfill( '.' )
				 // fill across to frequency
				 << setw( 10 )
				 // print 1 space after fill
				 << ' '
				 // stop filling with period( '.' )
				 << setfill( ' ' )
				 // format frequency number to 6 digits
				 << setw( 6 )
				 // place comma in frequency number and print it
				 << insert_comma( rolls_of_dice[face] )
				 // print one space after frequency
				 << ' '
				 // fill across to actual with period( '.' )
				 <<  setfill( '.' )
				 // print spacing over to expected size
				 << setw( 10 )
				 // print 1 space before fill
				 << ""
				 // stop filling with period( '.' )
				 << setfill( ' ' )
				 // format actual number to 6 digits
				 // set precision to 3 decimals
				 << setprecision( 3 ) << setiosflags( ios::fixed | ios::showpoint )
				 << setw( 7 )
				 // print actual and percentage symbol
				 << 100.0 * rolls_of_dice[face]/NUMBER_OF_ROLLS << "%"
				 // print 1 space after fill
				 << ' '
				 // fill across to expected with period( '.' )
				 << setfill( '.' )
				 // print spacing over to actual size
				 << setw( 7 ) << ""
				 // stop filling with period( '.' )
				 << setfill( ' ' )
				 // format expected number to 6 digits
				 << setw( 5 )
				 // set precision to 1 decimals
				 << setprecision( 1 ) << setiosflags( ios::fixed | ios::showpoint )
				 // print expected and percentage symbol
				 << expected_size[face] << "%"
				 // return 1 line
				 << endl;

	cout << " ———————————" << setw( 50 ) << "———————————————" << setw( 35 ) << "——————————" << setw( 35 ) << "——————————" << endl;

	// indicates successful completion
	return 0; 
}

// GENERATE RANDOM NUMBERS
int random_generator( int low, int high ) {
	return (rand() % (high - low + 1)) + low;
}

// SIMILATE ROLLING A PAIR OF DICE
int throw_dice() {
	int die_one, die_two, dice;

	// roll first die
	die_one = random_generator( 1, 6 );
	// roll second die
	die_two = random_generator( 1, 6 );
	// get dice total
	dice = die_one + die_two;

	return dice;
}

// DICE FACE STRINGS
char * dice_face( int face ) {
  switch (face) {
    case  2: return "Two's"; break;
    case  3: return "Three's"; break;
    case  4: return "Four's"; break;
    case  5: return "Five's"; break;
    case  6: return "Six'es"; break;
    case  7: return "Seven's"; break;
    case  8: return "Eight's"; break;
    case  9: return "Nine's"; break;
    case 10: return "Ten's"; break;
    case 11: return "Eleven's"; break;
    case 12: return "Twelve's"; break;
    default: return "Illegal Face";
  }
}

// INSERT COMMAS INTO A NUMBER
char * insert_comma( unsigned int num ) {
	static int comma = '\0';
	static char temp[30];
	char * numPtr = &temp[sizeof(temp)-1];
	int i = 0;

	memset( temp, 'x', sizeof temp );
	
	if (comma == '\0') {
		struct lconv * lcp = localeconv();
		if (lcp != NULL) {
			if (lcp->thousands_sep != NULL && * lcp->thousands_sep != '\0')
				comma = * lcp->thousands_sep;
			else
				comma = ',';
		}
	}
	*numPtr = '\0';

	do {
    if (i % 3 == 0 && i != 0)
      *--numPtr = comma;
    *--numPtr = '0' + num % 10;
    num /= 10;
		i++;
	} while (num != 0);

	return numPtr;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2