# Thread: printing a heart help

1. ## printing a heart help

Hey I am new to programming and I am trying to write a code to print a heart of size n that is input by the user. The top half of the heart is made up of semicircles and the bottom is made up of an upside down triangle. Thanks 2. Honestly asking this type of general question is bound to lead you nowhere.

There are numerous ways to print a heart in C of "n size." When you asked this question, people could give you a ton of answers. And you didn't really specify what "n size" was. The size of the width? height? width of a semicircle?

If you're going to request help (should ask the TAs, etc, obvious homework question), you should at least post the specifications so people have a general idea of what you're actually posting about.

Anyhow:
The main function of the program is determining whether to draw a space or a star. Generally, you'd use a for loop in row-major order. Look at your program specifications and determine if in the current spot you should be drawing a semicircle or the "upside down triangle" portion of the code.

If you are drawing the semicircle, use the distance formula along with the parameters defined by the program specifications. If your row and column are within this distance, draw a star, otherwise, draw a space.

For the "sloping lines", you'll probably want to maintain two variables. Have one start at 0 and the other start at the specifications for the end of the row. With each new row, increment the first value and decrement the second value. And then when you draw the line, draw a * only if it's between these values, otherwise, draw a space. 3. I tried the TAs and everything but they were not in the office or responding to emails.
The exact specifications are as follows:

The user will enter a size for the heart, which is a positive integer. Let this integer be n. (The best sizes are typically in between 4 and 7.) The grid upon which the design will be printed will have 3n rows and 4n+1 columns. The top left corner of the grid is assigned coordinates (0,0), where the first number represents the row and the second number represents the column. The bottom right corner has coordinates (3n-1, 4n).

The outline of the heart is comprised of two main parts: semicircles and straight lines.

The two semicircles are centered at coordinates (n, n) and (n, 3n) and are drawn in rows 0 through n-1, inclusive. (Notice that we dont continue the semicircle on row n, so technically, its ever so slightly less than a semicircle.)

The two straight lines are diagonals from (n+1, 1) to (3n, 2n) and from (n+1, 4n  1) to (3n, 2n). (Notice that the slope of each of these lines is -1 and 1, respectively. 4. So, the first step is to make two loops that go through all of the rows and columns.

Do you understand how loops work, and do you understand that by the specifications how your loop would be set up? You could use for, while, do, etc. Or do you need help with the basics of setting up the initial loops? 5. I understand the basics of the different types of loops. However I am having trouble setting up the initial parameters for the loop for this problem. 6. Code:
```#include <stdio.h>

int main(void)
{
int n, row, col;
scanf("%d", &n);
for(row = 0; row < 3*n; row++)
{
for(col = 0; col < 4*n-1; col++)
{
if(row <= n-1)
//logic for semicircles -- use distance formulas
else
//logic for sloping lines
}
printf("\n"); //newline
}
return 0;
}```
Quickly written, but it should resemble something like this. The specifications state that there are 3*n rows and 4*n-1 columns. It also states that the semicircles are from row 0 to n-1 (Inclusive!). The rest is the sloping lines.

When you do the distance formulas (You'll need to make two distance formulas, one for (n, n) and one for (3n, n)), Measure the distance from your current row and column to your other points (n, n) and (3n, n). Make sure you take into account the 0.5 when you make your comparison. 7. ok thank you very much 8. Take into account that when calculating the distance formula you're likely going to need to include math.h to use the pow/sqrt functions. 9. I was thinking that. But im using DEVC++ and I think someone from class said its built in. 10. Originally Posted by dubbin240 I was thinking that. But im using DEVC++ and I think someone from class said its built in.
By default your compiler most likely links to it (technical jargon). But at the top, you should still include it. i.e., it will probably work if you don't include it, but it's more correct and more portable and better standards to include it. If you don't include it, it will likely throw you a warning. So you should probably add #include <math.h> at the top. 11. ok thanks. I was wondering if you could explain how to incorporate or exactly how the distance formulas work. Say for instance i use dist = sqrt(pow(i-n,2)+pow(j-n,2); as one distance. Do i use this in a loop under the if statement? 12. Originally Posted by dubbin240 ok thanks. I was wondering if you could explain how to incorporate or exactly how the distance formulas work. Say for instance i use dist = sqrt(pow(i-n,2)+pow(j-n,2); as one distance. Do i use this in a loop under the if statement?
Yep. Make sure dist is a double or a float. Under the if statement is correct? Why?

Because under both of the for loops, you are going through each row and each column -- basically each point. And you are calculating the distance of each point from your pointers. However, the if statement keeps it within the specifications. So it stops where the sloping lines begin. 13. #include <stdio.h>
#include <math.h>
int main()
{
int n, i, j;
double dist, dist2;
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;
}

I cant figure out why but I have an extra triangle on my heart. 14. Please highlight your code, and click on the # icon in the advanced editing window. That will surround your code with code "tags", and make your code much more readable and easy to study.

Always use code tags for posting code on a programming forum. 15. You need to edit the condition in your second for loop in your triangle portion. I'm just posting real quick so I could be wrong (G2g in a sec).
Make two conditions in your second for loop in the triangle portion (just like if you were in an if statement), use && to join them -- figure out what you need to put for the second condition. Your stars draw all the way to the right end, where they should only draw to a portion of the right end. Popular pages Recent additions 