Help with Diaming program

This is a discussion on Help with Diaming program within the C Programming forums, part of the General Programming Boards category; The object is to make a program that does what this attatched program does. change the file to .exe and ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    10

    Help with Diaming program

    The object is to make a program that does what this attatched program does.

    change the file to .exe and it will run the program.

    I need help with this.

    Its supposed to make a diamond shape with the inputted number.

    If you put in 3 it should do this

    *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *

    but in the shape of a diamond
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    I suggest that you post your code and state how does it not work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Ill post the code later.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    No problem, we'll help you out later.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    this is what I have so far.

    The j is my friends, what can I do to make his program more efficient?
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you have some code, then post it up, highlight it, and click on the pound symbol # to wrap it in code tags.

    Don't post links to docs, etc. That's a waste of time.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Code:
    #include<stdio.h> 
    #include<conio.h> 
    intmain( void ) 
    {
    int n = 0; //number of lines 
    int countUp = 0; //counts number of lines 
    int totalCount = 0; //counter that stores other counts 
    int tempCount = 0; //Counter used randomley 
    int spaceCount = 0; //space counter 
    int tempSpace = 0; 
    int a; //counts asterisks 
    while
    (1 > 0){ 
    printf("Enter a number or 0 to exit:"); 
    scanf("&#37;d", &n); 
    if(n !=0) //decides placement of first asterisk {
    for(countUp = 1; countUp < n; countUp++) 
    printf(" "); 
    printf("*"); 
    for(a = n; a > 1; a--) 
    printf(" "); 
    printf("\n"); 
    while(n > 1) //determines upper half of diamond 
    {
    n--;
    totalCount = totalCount + 2;
    for(countUp = 1; countUp < n; countUp++) 
    {
    printf(" "); 
    }
    tempCount = totalCount + 1;
    while(tempCount != 0) 
    {
    printf("*"); 
    tempCount--;
    }
    for(a = n; a > 1; a--) 
    {
    printf(
    " "); 
    }
    printf(
    "\n"); 
    }
    while
    (totalCount > 2) //determines lower half of diamond 
    {
    spaceCount++;
    tempSpace = spaceCount;
    while(tempSpace > 0) 
    {
    tempSpace--;
    printf(
    " "); 
    }
    totalCount = totalCount - 2;
    tempSpace = totalCount;
    while(tempSpace >= 0) 
    {
    printf(
    "*"); 
    tempSpace --;
    }
    tempSpace = spaceCount;
    while(tempSpace > 0) 
    {
    printf(
    " "); 
    tempSpace--;
    }
    printf(
    "\n"); 
    } 
    if
    (totalCount != 0) 
    {
    tempSpace = spaceCount;
    while(tempSpace >= 0) 
    {
    printf(
    " "); 
    tempSpace--;
    } 
    printf(
    "*"); 
    tempSpace = spaceCount;
    while(tempSpace >= 0) 
    {
    printf(
    " "); 
    tempSpace--;
    }
    printf(
    "\n"); 
    }
     
    }
    //sets all variables back to defaults 
    n = 0; 
    countUp = 0; 
    totalCount = 0; 
    tempCount = 0; 
    spaceCount = 0; 
    tempSpace = 0;
    a = 0; 
    }
    getche();
    return
    0; 
    }
    what can I do to make this more efficient?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    You might want to post your actual code by copying and pasting it.

    Good to see that you used code tags, but remember to indent your code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Umm . . . if you used better indentation, it would be more efficient for us to read it.
    If you did, you might spot a problem:
    Code:
    #include<stdio.h>
    #include<conio.h>
    intmain(void)
    {
        int n = 0;			//number of lines 
        int countUp = 0;		//counts number of lines 
        int totalCount = 0;		//counter that stores other counts 
        int tempCount = 0;		//Counter used randomley 
        int spaceCount = 0;		//space counter 
        int tempSpace = 0;
        int a;			//counts asterisks 
        while (1 > 0) {
    	printf("Enter a number or 0 to exit:");
    	scanf("&#37;d", &n);
    	if (n != 0)		//decides placement of first asterisk {
    	    for (countUp = 1; countUp < n; countUp++)
    		printf(" ");
    	printf("*");
    	for (a = n; a > 1; a--)
    	    printf(" ");
    	printf("\n");
    	while (n > 1)		//determines upper half of diamond 
    	{
    	    n--;
    	    totalCount = totalCount + 2;
    	    for (countUp = 1; countUp < n; countUp++) {
    		printf(" ");
    	    }
    	    tempCount = totalCount + 1;
    	    while (tempCount != 0) {
    		printf("*");
    		tempCount--;
    	    }
    	    for (a = n; a > 1; a--) {
    		printf(" ");
    	    }
    	    printf("\n");
    	}
    	while (totalCount > 2)	//determines lower half of diamond 
    	{
    	    spaceCount++;
    	    tempSpace = spaceCount;
    	    while (tempSpace > 0) {
    		tempSpace--;
    		printf(" ");
    	    }
    	    totalCount = totalCount - 2;
    	    tempSpace = totalCount;
    	    while (tempSpace >= 0) {
    		printf("*");
    		tempSpace--;
    	    }
    	    tempSpace = spaceCount;
    	    while (tempSpace > 0) {
    		printf(" ");
    		tempSpace--;
    	    }
    	    printf("\n");
    	}
    	if (totalCount != 0) {
    	    tempSpace = spaceCount;
    	    while (tempSpace >= 0) {
    		printf(" ");
    		tempSpace--;
    	    }
    	    printf("*");
    	    tempSpace = spaceCount;
    	    while (tempSpace >= 0) {
    		printf(" ");
    		tempSpace--;
    	    }
    	    printf("\n");
    	}
    
        }
    //sets all variables back to defaults 
        n = 0;
        countUp = 0;
        totalCount = 0;
        tempCount = 0;
        spaceCount = 0;
        tempSpace = 0;
        a = 0;
    }
    
    getche();
    return 0;
    indent: Standard input:110: Error:Stmt nesting error.
    }
    You've gone and commented out one of you opening curly braces! Oops.

    There are other problems there as well -- e.g., no space in "intmain".
    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.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    intmain does not looks good
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
        int n = 0;			
        int countUp = 0;		
        int totalCount = 0;		
        int tempCount = 0;		
        int spaceCount = 0;		
        int tempSpace = 0;
        int a;	
        while (1 > 0) {
    	printf("Enter a number or 0 to exit:");
    	scanf("%d", &n);
    	if (n != 0)		 {
    	    for (countUp = 1; countUp < n; countUp++)
    		printf(" ");
    	printf("*");
    	for (a = n; a > 1; a--)
    	    printf(" ");
    	printf("\n");
    	while (n > 1)		{
    	    n--;
    	    totalCount = totalCount + 2;
    	    for (countUp = 1; countUp < n; countUp++) {
    		printf(" ");
    	    }
    	    tempCount = totalCount + 1;
    	    while (tempCount != 0) {
    		printf("*");
    		tempCount--;
    	    }
    	    for (a = n; a > 1; a--) {
    		printf(" ");
    	    }
    	    printf("\n");
    	}
    	while (totalCount > 2)	
    	{
    	    spaceCount++;
    	    tempSpace = spaceCount;
    	    while (tempSpace > 0) {
    		tempSpace--;
    		printf(" ");
    	    }
    	    totalCount = totalCount - 2;
    	    tempSpace = totalCount;
    	    while (tempSpace >= 0) {
    		printf("*");
    		tempSpace--;
    	    }
    	    tempSpace = spaceCount;
    	    while (tempSpace > 0) {
    		printf(" ");
    		tempSpace--;
    	    }
    	    printf("\n");
    	}
    	if (totalCount != 0) {
    	    tempSpace = spaceCount;
    	    while (tempSpace >= 0) {
    		printf(" ");
    		tempSpace--;
    	    }
    	    printf("*");
    	    tempSpace = spaceCount;
    	    while (tempSpace >= 0) {
    		printf(" ");
    		tempSpace--;
    	    }
    	    printf("\n");
    	}
    
    	{
    
        n = 0;
        countUp = 0;
        totalCount = 0;
        tempCount = 0;
        spaceCount = 0;
        tempSpace = 0;
    	a = 0;{
    
    _getche();
    return 0;
    }
    Alright so this is what I have...it works but only works the first time.

    Like when I put in a number it does what its supposed to but when it loops back and I enter another number its messed up...it spaces the bottom out farther and farthe with each number.

    Also, it doesnt end when I put in 0.....

    what should I do so it doesnt get messed up after each one and how do I make it end when i put in 0?

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    After you read in the number, explicitly tell the program to exit if the command is zero

    Code:
     if ( input == 0 )
    {
       abort();
    }
    I'm just trying to be a better person - My Name Is Earl

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Quote Originally Posted by swgh View Post
    After you read in the number, explicitly tell the program to exit if the command is zero

    Code:
     if ( input == 0 )
    {
       abort();
    }

    Im using c

    It says theres no such thing as abort

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
        int n = 0;			
        int u = 0;		
        int t = 0;		
        int c = 0;		
        int s = 0;		
        int p = 0;
        int a;	
        while (1 > 0) {
    	printf("Enter a number or 0 to exit:");
    	scanf("%d", &n);
    	if (n != 0);		 {
    	    for (u = 1; u < n; u++)
    		printf(" ");
    	printf("*");
    	for (a = n; a > 1; a--)
    	    printf(" ");
    	printf("\n");
    	while (n > 1)		{
    	    n--;
    	    t = t + 2;
    	    for (u = 1; u < n; u++) {
    		printf(" ");
    	    }
    	    c = t + 1;
    	    while (c != 0) {
    		printf("*");
    		c--;
    	    }
    	    for (a = n; a > 1; a--) {
    		printf(" ");
    	    }
    	    printf("\n");
    	}
    	while (t > 2)	
    	{
    	    s++;
    	    p = s;
    	    while (p > 0) {
    		p--;
    		printf(" ");
    	    }
    	    t = t - 2;
    	    p= t;
    	    while (p >= 0) {
    		printf("*");
    		p--;
    	    }
    	    p = s;
    	    while (p > 0) {
    		printf(" ");
    		p--;
    	    }
    	    printf("\n");
    	}
    	if (t != 0) {
    	    p = s;
    	    while (p >= 0) {
    		printf(" ");
    		p--;
    	    }
    	    printf("*");
    	    p = s;
    	    while (p >= 0) {
    		printf(" ");
    		p--;
    	    }
    	    printf("\n");
    	
    	}
    	}
    	
        n = 0;
        u = 0;
        t = 0;
        c = 0;
        s = 0;
        p = 0;
    	a = 0;
    	}
    	_getche();
    return 0;
    }

    Alright, thats what I have...it works perfectly except it doesnt exit when I put in 0...it just does nothing then asks the same thing again...enter the number or press 0 to exit

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, your loop only stops when 1 <= 0. If that's not when you want your loop to stop, then maybe you should change that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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