Dice Program

I need formatting assistance

Page 1 of 1

2 Replies - 1685 Views - Last Post: 03 March 2008 - 09:22 AM Rate Topic: -----

#1 sdhanaver   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-February 08

Dice Program

Post icon  Posted 01 March 2008 - 06:09 PM

Someone helped me greatly this far and my code is almost complete. However, I need the printout formatted a specific way. It's a "dice program" that requires user input to calculate the total of two dice rolled (they can have 4 - 12 sides). Missing from the printout now is a first row that has the number rolled for the first di (sp?) and a first column that has the number rolled for the second di. For example (with two 4-sided di):

1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

That's all I have to add to finish the program. Help, please? Here's what I've got so far:

#include <stdlib.h>  
#include <stdio.h>  
  
int main () 
{  
  
	int sides;  
	int d1, d2;  
  
	printf("How many sides are on your dice?\n");  
	scanf("%d", &sides); 
   
	if ((sides < 4) || (sides > 12))
	{  
		printf("Invalid Entry.\n");  
	}  

	else 
	{  
		for (d1 = 1; d1 <= sides; d1++) 
	  {   
			for (d2 = 1; d2 <= sides; d2++) 
			{  
				printf ("%4d", d2+d1); 
			}  
			printf("\n"); //a new line   
		}  
	}  
  
	getchar();  
	getchar();  
			   
	return 0;  
  
}


*Mod Edit: added code tags: :code:

This post has been edited by NickDMax: 01 March 2008 - 06:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Dice Program

#2 Tennison   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-February 08

Re: Dice Program

Posted 03 March 2008 - 08:36 AM

I'm confused as to exactly what u want
Was This Post Helpful? 0
  • +
  • -

#3 letthecolorsrumble   User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Dice Program

Posted 03 March 2008 - 09:22 AM

I think I get you and here is the solution, just don't copy and paste put try to understand the changes or addition been made.

#include <stdlib.h>  
#include <stdio.h>  
  
int main ()
{  
  
    int sides;  
    int d1, d2;  
  
    printf("How many sides are on your dice?\n");  
    scanf("%d", &sides);
  
    if ((sides < 4) || (sides > 12)) {  
        printf("Invalid Entry.\n");  
    }  

    else {
		printf("    "); //4 spaces for proper output
		for(d1=1; d1 <=sides;d1++){
			printf("%4d",d1);
		}
		printf("\n"); //a new line 
		
        for (d1 = 1; d1 <= sides; d1++) {
			//printing column for die 2 as it has same number of sides as die 1
			printf("%4d",d1); 
            for (d2 = 1; d2 <= sides; d2++) {  
                printf ("%4d",d2+d1);
            }  
            printf("\n"); //a new line  
        }  
    }  
  
    getchar();  
    getchar();  
              
    return 0;  
  
}



More help? Keep Posting! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1