Jump to content


  • Please log in to reply
15 replies to this topic

#16 +Majesticmerc


    Resident Idealist

  • Tech Issues Solved: 9
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 23 September 2013 - 00:22

It's probably worth breaking down your problem into smaller chunks. Try these, all as smaller programs.

1: Print the header.

So the first thing that you're required to do is print the header. Now, the header only needs to be printed once, therefore we don't need to print the header inside a loop! Just print it once before you create a loop!

My Recommendation: write a program that prints ONLY the header, and only do it once.

2: Create a loop.

Next, have a go at creating the loop. So your for loop should start at a specific number, and increment in multiples of three (e.g. 6, 9, 12, 15, 18). You don't just have to use the "++" operator to increment your variable, you can do this too:
// Create a loop that increments by 10 every time...
int index;
for (index = 0; index <= 50; index += 10) {
    printf("%d", index);

// Output:
// "0"
// "10"
// "20"
// "30"
// "40"
// "50"
Do you see how the output was created? The variable "index" was passed into the printf function as a parameter, and the printf function replaced the "%d" with the value of "index".

See if you can just do this with a loop incrementing by three every time. Don't worry about any table data at the minute.

3: Print some output that accepts more than one number

Next, try to create a program that, like #1 above, only prints one line, but prints multiple numbers worked out using some variables. Remember how the loop program above used "%d" instead of a number, and then we passed the number in? The same concept applies for as many numbers as you like. So you can create a printf statement that looks like this:
int i = 1;
int j = 2;
int k = 4;
printf("%d %d %d", i, j, k);

// Output:
// 1 2 4
4: Put it all together

So now what do you have? You've got three little programs that do three things:
  • Prints a static line of text.
  • Prints a set of numbers in a loop.
  • Prints a line containing multiple numbers.
How you can put all this code together to solve your problem I'll leave as an exercise to you. Hopefully by the end of the third program you'll already know what you have to do.


If you're up for some constructive criticism, I've got a couple of pointers based on your initial code:

Tip #1: Indent your code correctly
I know it might seem a bit silly being strict on indenting your code, but it really helps you to read it later. Indenting code inside curly braces (i.e. { and }) allows you to see what block of code a function sits inside.

Check out how much easier it is to read your original code with better formatting:
#include "stdio.h"

void main()
    int i;

    for(i = i + 3; i < 18; i++)
        if (i % 3 == 0)
            printf("\t 'A'","%d", &i);

        if (i % 3 != 0)


    for(i = i + 3; i < 20; i++)
        if (i % 3 == 0)
            printf("\t 'A+2", "%d", i);

        if (i % 3 != 0)


Tip #2: Don't forget about 'else'
The else statement is your friend, don't forget to call him every now and then! You don't need to check if "i % 3 == 0" and then in the next line check if the opposite is true (i.e. "i % 3 != 0"), because you can just use an else...

// Do this if i is a multiple of three...
if (i % 3 == 0)
    printf("\t 'A+2", "%d", i);

// ... otherwise do this...
Tip #3: Remember to initialize your variables!
In C, your numbers won't have a defined value by default, so there's no guarantee that your number will start at zero. Remember to set your variable to zero (or another appropriate value) before you start using it, otherwise, as Asik said, you'll run into undefined behaviour, and nobody wants that!

int i; // <-- BAD! i could be "0", but it could also be "12654151", because you haven't told it what to be!
int j = 0; // <-- GOOD! j will definitely be zero the first time you use it.