Minor C problem

This is a discussion on Minor C problem within the C Programming forums, part of the General Programming Boards category; Hi, I am kind of new to C and I have run into some basic problems. I am trying to ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Minor C problem

    Hi, I am kind of new to C and I have run into some basic problems. I am trying to create a diamond in asterixes and I’ve managed to create a filled diamond with asterixes. But the problem I have is that I need it to look something like this:
    Code:
               *
             *  *
           *      *
         *          *
           *      *
             *  *
               *
    The code that I managed to make presents the diamond like this:
    Code:
              *
            ***
          *****
        *******
       ********
         ******
           ****
             **
              *
    And this is the code that makes it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void pyramid(){
         int bredd,center,a,b,c;
    	
    	 printf("Lines: ");
    	 scanf("%d",&bredd);
    	 if(bredd%2!=0){ //Check if enterd number is odd or not
    
    	 center = (bredd/2 + bredd%2);
    	
    	 for(a=1;a<=bredd;a+=2)
    	 {	
    		for(c=0;c<=center;c++)
    			printf(" ");
    		for(b=0;b<a;b++)
    			printf("*");
    		printf("\n");
    		center--;
    	}
    	center+=2;
    	for(a-=4;a>=1;a-=2)
    	{
    		for(c=center;c>=0;c--)
    			printf(" ");
    		for(b=0;b<a;b++)
    			printf("*");
    		printf("\n");
    		center++;
    	}
     }
     else{
          printf("Fel!\n");
          }
    }
    int main(void)
    {
      pyramid();
      system("PAUSE");	
      return 0;
    }
    Any feedback would be appreciated!

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How about slowly going through an example on paper and seeing what you would do to draw the diamond, if you could only print one line at a time.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Happy_Reaper View Post
    How about slowly going through an example on paper and seeing what you would do to draw the diamond, if you could only print one line at a time.
    If you've got grid paper that would also help, if not just draw a grid. And where you see a blank cell could be a space...?

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    Thank you! I've solved it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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