0
#include <stdio.h>
#include <stdlib.h>

int power(int base, int power){
    int result, i;
    result = 1;
    for (i=0; i < power; i++){
        result *= base;
    }/*for*/
    return result;
}/*power*/

int main (){
    int n = 0;

    int exponent = 0;
    while(n < 10){
        int answer = power(2, n);
        float neganswer = 1.0 / (power(2,n));

        printf("%d %d %g\n", exponent, answer, neganswer);

        exponent++;
        n++;
    }/*while*/
    return EXIT_SUCCESS;

}/*main*/

When this program runs, the 2nd function goes from 1 to 512, which pushes the rest of the columns are moved 2 to the right. How would I go about lining up these columns? Thanks.

5 Answers 5

3

You can change your printf format to:

printf("%d %3d %10g\n", exponent, answer, neganswer);

This will format the argument to the specific width:

0   1          1
1   2        0.5
2   4       0.25
3   8      0.125
4  16     0.0625
5  32    0.03125
6  64   0.015625
7 128  0.0078125
8 256 0.00390625
9 512 0.00195312
Sign up to request clarification or add additional context in comments.

Comments

2

Not to take away from the all ready provided fine 2 answers, but many options are available with printf().

// Nicely aligned with decimal point in the same place
// #  : Alternate form always prints `.`
// -  : Left justify the output.
// .* : Determine width from the next parameter which is `n`.
printf("%d %4d %#-.*f\n", exponent, answer, n, neganswer);
0    1 1.
1    2 0.5
2    4 0.25
3    8 0.125
4   16 0.0625
5   32 0.03125
6   64 0.015625
7  128 0.0078125
8  256 0.00390625
9  512 0.001953125

Comments

1

C string that contains the text to be written to stdout. It can optionally contain embedded format specifiers that are replaced by the values specified in subsequent additional arguments and formatted as requested.

A format specifier follows this prototype: [see compatibility note below]

%[flags][width][.precision][length]specifier 

int main (){
    int n = 0;

    int exponent = 0;
    while(n < 10){
        int answer = power(2, n);
        float neganswer = 1.0 / (power(2,n));

        //modify printf("%d %d %g\n", exponent, answer, neganswer);
        printf("%d %4d %12g\n", exponent, answer, neganswer);

        exponent++;
        n++;
    }/*while*/
    return EXIT_SUCCESS;

}/*main*/

more about printf function, please refer to the following link

http://en.cppreference.com/w/c/io/fprintf

Comments

0

Take a look at my simple library: libtprint , the code there is quite easy to understand and you should get some basic ideas how to format columns with printf ().

Hope it helps !

Comments

0

Try this

    float neganswer = 1.0f / (power(2,n));

    printf("%d %3d %f\n", exponent, answer, neganswer);  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.