문제

I'm trying to prevent printing a trailing tab at the end of each row of output. How can I do this?

The rest of the code works as I need it to

#include <stdio.h>
int main()
{  
    int i, j, k, y, z, x, c, b, a, C;
    scanf("%d", &x);

    for(i=0; i<x; i++){
        for(j=0; j<x; j++){
            int c = 0;
            for(k=0; k<x; k++){

                y = (i+1)*(k+1);
                z = (j+k); 
                c = (z*y)+c;
            } 
            printf("%d\t", c);
        }
        printf("\n");
    }
}
도움이 되었습니까?

해결책

The \t and \n are the tab and new-line escape sequences respectively, so change

printf("%d\t", c);

to

printf("%d", c);

to get rid of the tab, and remove

printf("\n");

all together, to loose the new lines... easy
As an asside: why are you declaring a second int c? Your code starts with declaring bunch of ints, some of which you don't use:

int i, j, k, y, z, x, c, b, a, C;
                        //last 3 aren't used
                      //c declared here, though
//I'd write:
int i, j, k, y, z, x, c;

And further down:

//inside second loop:
int c = 0;
//would be better if wou wrote:
c = 0;

Last of the notes: you're missing a return statement, but your main function's signature indicates (rightly) the main function should return an int, not a void.
Add a return 0; at the end

If the only thing you want to avoid printing is the last \n (and \t), you could change:

printf("\n");

with

if (i < x-1) printf("\n");

This will print \n every time, except for the last time your loop runs. Simply because the condition for the loop to run is i<x, and the condition for the newline to be printed is i<x-1.
As far as your tabs are concerned, replacing:

 printf("%d\t", c);

with:

if (j < x - 1) printf("%d\t", c);
else printf("%d", c);

does just what you need.
That said, since x is a constant value, it would probably be better to assign x-1 to one of those unused, yet declared ints:

scanf("%d", &x);
a = x -1;

Then, since you're checking when you're printing the last number of a row with this code:

if (j < a) printf("%d\t", c);//replaced x - 1 with a here
else printf("%d", c);

You can safely assume that the else clause is only applicable to the last number of each row, so why not add the newline there?

if (j < a) printf("%d\t", c);//replaced x - 1 with a here
else printf("%d\n", c);

Overall, this leaves you with the following code:

#include <stdio.h>
int main()
{  
    int i, j, k, y, z, x, c, a;
    scanf("%d", &x);
    a = x - 1;
    i = 0;
    for(i=0; i<x; i++){
        for(j=0; j<x; j++){
            c = 0;
            for(k=0; k<x; k++){
                y = (i+1)*(k+1);
                z = (j+k); 
                c = (z*y)+c;
            } 
            if (j < a) printf("%d\t", c);
            else printf("%d\n", c);
        }
    }
    return 0;//ADD A RETURN STATEMENT!!
}

This still adds a new line after the last row of output. To remove that, too, just write:

if (j < a) printf("%d\t", c);
else if (i < a) printf("%d\n", c);//check if we're in the last i-loop
else printf("%d", c);//if so, don't print new line

Job done... I've tried this code and you can see the output on this codepad

다른 팁

\t means tab, \n means new line. These are called escape sequences.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top