im learning c and need help learning this concept
and what it does

#include <stdio.h>
#include <c.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
int rollone(void);
void printrolls(int rolls[]);
void printx(int howMany);
int main (int argc, const char * argv[])
{ int rolls[ 13 ], twoDice, i;

srand (clock() );
for ( i=0; i<=12; i++ ) 
rolls[ i ] = 0;
for (i=1; i <= 1000; i++ )
{ twoDice = RollOne() + RollOne() ;
++ rolls [twoDice]; }	
PrintRolls( rolls );
return 0; }
int RollOne( void )
{ return (rand() % 6) + 1; }
void PrintRolls (int rolls [])
{int i;
for (i=2; i<=12; i++)
{printf ("%2d (%3d) : ",i, rolls [i]);
PrintX( rolls[ i ] / 10);
printf(" \n" );}
} void PrintX (int howMany )
{int i;
for (i=1; i<=howMany; i++)
printf( "x" ) ; }

please edit your post and enclose the code in code tags. I hope the original program contains proper indentations so you can read it.

[ code ]
// code here
[ /code ]

remove spaces from above.

thanks didn't know about code ) boxes ill rember next time

That is a pretty sloppy program that was probably written by a 1st semester student that didn't know what (s)he was doing. It contains several problems that will not allow any compiler to successfully compile it. If you are using this as an example to learn -- DON'T.


the program sets all elements of the rolls array to 0 (there is a much easier way to do that than is shown in the program.). Next it generates 1,000 random rolls of the dice and increments the array to count the number of times a roll was generated. Finally, the program attempts (incorrectly by the way) to display the results.

That is a pretty sloppy program that was probably written by a 1st semester student that didn't know what (s)he was doing. It contains several problems that will not allow any compiler to successfully compile it. If you are using this as an example to learn -- DON'T.


the program sets all elements of the rolls array to 0 (there is a much easier way to do that than is shown in the program.). Next it generates 1,000 random rolls of the dice and increments the array to count the number of times a roll was generated. Finally, the program attempts (incorrectly by the way) to display the results.

it's teaching me in a book
so any help would be appreacited

(p.s) it does compile and work (Xcode)
if that's not a good example could some one explaine the
random system works and how to make a random number
be generated between certian numbers

The random generator parts are ok, there are other problems such as undefined variable names.
[edit]nevermind -- due to its very sloppy programming style I missed a function[/edit]

Why so complicated?
You want to create the rolling of a dice?

#include <iostream>
#include <cstdlib>
#include <conio.h>

int RandomInteger;
int Choice;
int ProgramRun = 0;

int main()
{
	do
	{
	
		std::cout << "What would you like to do : \n";
		std::cout << "\n";
		std::cout << "#1 Roll the dice!\n";
		std::cout << "\n";
		std::cout << "#2 Quit this stupid program\n";
		std::cout << "\n";
		std::cout << "Choice : ";
		std::cin >> Choice;
		std::cout << "\n";
	
		switch(Choice)
		{
		case 1:
				RandomInteger = rand() % 7; // number between 0-6

				switch(RandomInteger)
				{
					case 0:	

						break;

					case 1:
			
						std::cout << "One\n";
						break;
	
					case 2:
	
						std::cout << "Two\n";
						break;
	
					case 3:
	
						std::cout << "Three\n";
						break;

					case 4:
	
						std::cout << "Four\n";
						break;

					case 5:
			
						std::cout << "Five\n";
						break;

					case 6:
	
						std::cout << "Six\n";
						break;
				} // end of second switch	
				break;

		case 2:
			ProgramRun = 1;
			break;
		
		} // end of first switch

	} // end of loop
	while (ProgramRun == 0); // loop statement
	
	std::cout << "Press any key to quit\n";

	getch();

	return 0;
} // end of main

Ancient Dragon...
I'm also new to C++. Could you expand on a couple of items in your response..

...the program sets all elements of the rolls array to 0 (there is a much easier way to do that than is shown in the program.).
How?

Finally, the program attempts (incorrectly by the way) to display the results.
What's the correct way.

Why so complicated?
You want to create the rolling of a dice?

#include <iostream>
#include <cstdlib>
#include <conio.h>

int RandomInteger;
int Choice;
int ProgramRun = 0;

int main()
{
	do
	{
	
		std::cout << "What would you like to do : \n";
		std::cout << "\n";
		std::cout << "#1 Roll the dice!\n";
		std::cout << "\n";
		std::cout << "#2 Quit this stupid program\n";
		std::cout << "\n";
		std::cout << "Choice : ";
		std::cin >> Choice;
		std::cout << "\n";
	
		switch(Choice)
		{
		case 1:
				RandomInteger = rand() % 7; // number between 0-6

				switch(RandomInteger)
				{
					case 0:	

						break;

					case 1:
			
						std::cout << "One\n";
						break;
	
					case 2:
	
						std::cout << "Two\n";
						break;
	
					case 3:
	
						std::cout << "Three\n";
						break;

					case 4:
	
						std::cout << "Four\n";
						break;

					case 5:
			
						std::cout << "Five\n";
						break;

					case 6:
	
						std::cout << "Six\n";
						break;
				} // end of second switch	
				break;

		case 2:
			ProgramRun = 1;
			break;
		
		} // end of first switch

	} // end of loop
	while (ProgramRun == 0); // loop statement
	
	std::cout << "Press any key to quit\n";

	getch();

	return 0;
} // end of main

//if thats in C++ then it won't help me

im trying to learn C and how to make a random number
is the current item on the list when i attempted to run your program i got a pacific massage 3 times and the rest of the garbage

main.c:35: iostream: No such file or directory
main.c:36: cstdlib: No such file or directory
main.c:37: conio.h: No such file or directory
main.c:48: error: parse error before ':' token
main.c:49: error: parse error before ':' token
main.c:49: error: duplicate label `std'
main.c:50: error: parse error before ':' token
main.c:50: error: duplicate label `std'
main.c:51: error: parse error before ':' token
main.c:51: error: duplicate label `std'
main.c:52: error: parse error before ':' token
main.c:52: error: duplicate label `std'
main.c:53: error: parse error before ':' token
main.c:53: error: duplicate label `std'
main.c:54: error: parse error before ':' token
main.c:54: error: duplicate label `std'
main.c:55: error: parse error before ':' token
main.c:55: error: duplicate label `std'
main.c:56: error: parse error before ':' token
main.c:56: error: duplicate label `std'
main.c:71: error: parse error before ':' token
main.c:71: error: duplicate label `std'
main.c:76: error: parse error before ':' token
main.c:76: error: duplicate label `std'
main.c:81: error: parse error before ':' token
main.c:81: error: duplicate label `std'
main.c:86: error: parse error before ':' token
main.c:86: error: duplicate label `std'
main.c:91: error: parse error before ':' token
main.c:91: error: duplicate label `std'
main.c:96: error: parse error before ':' token
main.c:96: error: duplicate label `std'
main.c:110: error: parse error before ':' token
main.c:110: error: duplicate label `std'

so i figure your codeing is in C++ not C
because there are no files or directories
everyone makes mistakes usually me

Here's your original code beautified (indenting), good habit-ized (full bracing always), mostly usual-idiomed (for loop initialization and condition), and fairly de-loused (unnecessary stuff removed, etc.).

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int RollOne ( void )
{
   return (rand() % 6) + 1;
}

void PrintX ( int howMany )
{
   int i;
   for ( i = 0; i < howMany; ++i )
   {
      printf ( "x" ) ;
   }
}

void PrintRolls ( int *rolls )
{
   int i;
   for ( i = 2; i <= 12; ++i )
   {
      printf ( "%2d (%3d) : ",i, rolls[i]);
      PrintX ( rolls[i] / 10);
      printf ( " \n" );
   }
}

int main ( void )
{
   int twoDice, i, rolls[13] = {0}; /* initialize rolls */

   srand ( time(0) );
   for ( i = 0; i < 1000; ++i )
   {
      twoDice = RollOne() + RollOne() ;
      ++rolls[twoDice];
   }
   PrintRolls ( rolls );
   return 0;
}

/* my output
 2 ( 38) : xxx 
 3 ( 49) : xxxx 
 4 ( 69) : xxxxxx 
 5 (104) : xxxxxxxxxx 
 6 (155) : xxxxxxxxxxxxxxx 
 7 (173) : xxxxxxxxxxxxxxxxx 
 8 (143) : xxxxxxxxxxxxxx 
 9 (104) : xxxxxxxxxx 
10 ( 76) : xxxxxxx 
11 ( 60) : xxxxxx 
12 ( 29) : xx 
*/

The language is case sensitive, so printrolls is not the same as PrintRolls .

...the program sets all elements of the rolls array to 0 (there is a much easier way to do that than is shown in the program.).
How?

Below sets all elements of the array to 0 because if one or more elements are explicitly initialized as shown below, all remaining elements not in the initialization list are set to 0.

int roll[12] = {0};

Finally, the program attempts (incorrectly by the way) to display the results.
What's the corret way.

for (i=2; i<=12; i++)
{printf ("%2d (%3d) : ",i, rolls [i]);

All arrays are numbered from 0 to 1 less than the number of elements. rolls array has 12 elements, which are numbered 0, 1, 2, 3, ... 11. The loop above will attempt to access element number 12, which does not exist.

[edit]Note: Dave fixed the problem by declarin the array to have 13 elements, not 12. [/edit]

This is the correct loop -- it uses < not <=. The i counter initializer may also be wrong, depending on the programmer's intent for the first element. The second array element is numbered 1, not 2.

for (i=2; i<12; i++)
{printf ("%2d (%3d) : ",i, rolls [i]);

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.