Skip to main content

Line Drawing

Suppose we wanted to write a program that will draw a line of stars ( the astericks (*) character)

What are the steps?

Think about what you will need to do... of those things, what do you already know how to do? Expand for solution:

Click to see steps
  • prompt user for a number
  • read in the number
  • draw as many stars as the number entered

Looking at the above steps, we already know how to do the first 2 steps...we just need to add the third step!

Starting with what we know:

We have seen the following bit of code a lot with slight variations... for explanation, please look in previous chapters

#include <stdio.h>
//declare what functions we will have:
int getNumStars();
void drawLine(int numStars);
int main(void)
{
//get userinput
int numStars = getNumStars();
//call function that will draw stars
drawLine(numStars);
return 0;
}
int getNumStars()
{
int numStars;
printf("Please enter number of stars on the line: ");
scanf("%d", &numStars);
return numStars;
}
void drawLine(int numStars)
{
//need to write this function
}

Printing the stars

Let us now consider how to write the drawLine() function. For this function, we are passed a number representing how many stars we need to draw. We want to print that number of stars.

What we already know how to do is to create a single star. This is just:

printf("*");

What we want to do is repeat this numStars times.

Here is the flow chart illustrating how we want to solve this problem.

In C, the way we repeat statements is to use a loop. In this case, we are counting the number of stars we are printing. When we want to repeat something that we can count, we want to use a for loop.

In a for loop, we typically have a loop counter. This loop counter is initialized, checked and incremented in the for statement. A for loop looks like this:

Loop counters

Traditionally we use the single characters i, j and k for variable names. Alternatively you can use a descriptive name. However, if you do decide to use a single character, then stick to i, j and k (in that order. If you can't use i, use j. If you can't use j, use k)

for (<initialization>; <check statement>; <update>){
//do this code every time
}

In our situation, we have a counter that will track how many stars has been printed. Initially, 0 stars are printed so we initialize the counter to 0. We will print 1 star and update the counter. We print as long as we have not yet printed numStar stars.

Thus our we can break down our statements as follows:

Initialization

i = 0

i is our counter for how many stars are printed. We initialize it to 0 to indicate we have printed no stars yet

Check Statement

i < numStars

we want to only print more stars if we have not printed numStars yet. Thus we check i against numStars

Update

i++

We want to update our our counter to increment the number of stars printed by 1

Update Options

For a loop where the loop counter is i, i++ is the typical way to increment by 1.

While the following all work:

i+=1
i = i + 1
++i

the standard is i++ if you want to increment by 1 so the others always look awkward. For that reason the expression should be i++. If you want to increment by more than 1, then you can use other operators to do this.

drawLine() function:

This function puts together what we are doing above

void drawLine(int numStars)
{
int i; //declare our loop counter
//putting together our loop with its 3 parts
// * initialization
// * check
// * increment
for (i = 0; i < numStars; i++){
//each time we do the for loop, we print one star
printf("*");
}
//after we print all the stars put in a newline
printf("\n");
}
A standard counting loop recipe:

Suppose you want to write a loop that will run exactly n times. The best way to write a loop to do this is as follows:

int i;
for(i = 0;i < n ;i++){
//...
}

Notice how the initialization starts at 0. The reason we count from 0 has to do with the way arrays work. Arrays is a concept we will cover in the next chapter. It is a good idea to practice writing your counting loops in this manner. Count starting from 0 by default.

Note that this is also how we wrote our loop to solve the numStars problem.

Putting it all together.

Putting everything together, here is our completed program:

#include <stdio.h>
//declare what functions we will have:
int getNumStars();
void drawLine(int numStars);
int main(void)
{
int numStars = getNumStars();
drawLine(numStars);
return 0;
}
int getNumStars()
{
int numStars;
printf("Please enter number of stars on the line: ");
scanf("%d", &numStars);
return numStars;
}
void drawLine(int numStars)
{
int i;
for (i = 0; i < numStars; i++){
printf("*");
}
printf("\n");
}
Loop Variable Updating

C for loops allow you to update the loop counter (i in our example) anywhere inside the body loop. The following is syntactically correct:

int i;
for (i = 0; i < n; i++){
i += 2;
//do stuff
}

HOWEVER, this sort of code leads to errors. It is extremely bad practice to update the counter of a for loop inside the body of the for loop (between the ). It makes the code harder to read AND makes the code more error prone. In general any changes to the loop counter should be done in the <update> component of the for() statement.

A while loop version

A for loop is preferred in the situation described here as we are clearly counting. However, it is possible to write this program using a while loop instead. You would simply need to separating out the 3 parts of the for statement. In fact, if you have trouble reading/writing a for() loop, a while loop may be easier to read.

#include <stdio.h>
//declare what functions we will have:
int getNumStars();
void drawLine(int numStars);
int main(void)
{
int numStars = getNumStars();
drawLine(numStars);
return 0;
}
int getNumStars()
{
int numStars;
printf("Please enter number of stars on the line: ");
scanf("%d", &numStars);
return numStars;
}
void drawLine(int numStars)
{
int i=0; //initialize the counter before the loop

//while loop has condition that is same as the middle part of
//the for loop
while(i < numStars){
printf("*");
// just before the end of the while loop
// update the counter
i++;
}
printf("\n");
}

While it is possible to write this with a while loop, the for loop version is more succinct. It allows you to see immediately the number of loop iterations for the loop. The while loop version works but it is not the preferred way to do this task.