Thread: printing a heart help

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
    int n, i, j;
    double dist, dist2;
    printf("How big is your heart?\n");
    scanf("%d", &n);
    
    for(i = 0; i < n; i++)
    {
    for(j = 0; j < 4*n+1; j++)
    {
    dist = sqrt(pow(i-n,2)+pow(j-n,2));
    dist2 = sqrt(pow(i-n,2)+pow(j-3*n,2));
    
    if(dist < n + 0.5 || dist2 < n + 0.5)
    printf("*");//logic for semicircles -- use distance formulas
    else
    printf(" ");//logic for sloping lines
    
    } 
    printf("\n"); //newline
    }
    
    int numspaces = 1;
    int numstars = 4*n-1;
    for(i = 1; i <= 2*n; i++)
    {
    for(numspaces=1;numspaces<=i;numspaces++)
    printf(" ");
    for(numstars = 4*n-1;numstars>=i;numstars--)
    printf("*");
    
    printf("\n");
    }
    system("pause");
    return 0;
    }

  2. #17
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Alright. With method you are approaching the sloping lines, you're going to need three for loops:
    One to print the left-side spaces.
    One to print the middle stars.
    One to print the right-side spaces (should be similar or the same as the left-side spaces for loop).
    Or you can of course change your approach.
    Also, I hope that's just a bug or mishap, in reference to your code not being indented.
    Last edited by Phenax; 03-03-2011 at 09:23 PM.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Yea the format was messed up when i pasted it

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

    I would have to do all that just to fix this one parameter. The print out just as the extra triangle as shown in orange.

  4. #19
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by dubbin240 View Post
    Yea the format was messed up when i pasted it

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

    I would have to do all that just to fix this one parameter. The print out just as the extra triangle as shown in orange.
    You don't have to do much more. You already have a for loop for printing the left-side spaces and the center stars. But, your center stars go out too far, so you need to reduce how far it goes (remember you can use a && in your for loop condition) and then add another for loop to add the right-side spaces.

    Or you can attempt to go about drawing it another way.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Because the width of the longest line in the heart isn't entered by the user, and because the number of rows isn't entered either, the drawing becomes more difficult.

    If I were you, I wouldn't use the distance formula, at all. Calculate the center point of the horizontal lines, and work from that.

    Nevertheless:

    This functions draws the bottom of the heart. I tried tweaking your code, but it was too frustrating.

    Code:
    /* the code that draws the top of the heart, is up here, and gives us the value of j.
    
      width = (j-3);
      for(rows = width; rows > 0; rows-=2) {
        spaces = (width - rows) + 2;
        stars = width - spaces;
        spaces /= 2;
        while(spaces-- > 0)
          putchar(' ');
        while(stars-- > 0 )
          putchar('*');
        putchar('\n');
      }
        putchar('\n');
      (void) getchar();
      return 0;
    }
    The only slight imperfection in the heart now, is the longest line, which is repeated. Also (and part of the above imperfection), is the lack of a top row on the heart, that is above (and would have two shorter lines of stars), the one being shown now.

  6. #21
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    if(dist < n + 0.5 || dist2 < n + 0.5)
    Kindly use explicit type casting...

    By the way, too many loops....
    It can be done in three loops as well....
    Or maximum of four....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  3. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  4. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM