rizalriz
rizalriz

Reputation: 5

How not to print - (minus symbol) after the last output?

I'm trying to solve a problem, The objective is to print the last step can be achieved, the inputs are N as testcases, and A as the integers of the testcases. For example : N = 10 A = 1 2 1 1 2 3 4 1 2 3 <- 10 integers

So, this is what I have done.

#include <stdio.h>
    
int main()
{
    int N,i,j;
    scanf("%d",&N);
     
    int A[N+1];
    
    for(int i=0;i<N;i++)
    {
        scanf("%d",&A[i]);
    }
    A[N] = 1;
    
    for(int i=0;i<N;i++)
    {
        if(A[i] > A[i+1])
        {
            printf("%d-",A[i]);
        }
        else if(A[i] == A[i+1])
        {
            printf("%d-",A[i]);
        }
    }
    printf("\n");

    return 0;
}

The expected output is : 2-1-4-3

but the output I get is : 2-1-4-3-

How do I exclude the minus symbol after the last output? Thanks in advance.

Upvotes: 0

Views: 86

Answers (3)

chux
chux

Reputation: 153417

As "-" is a separator only used after the first printed value, consider changing the separator.

const char *separator = "";
for(int i=0;i<N;i++) {
    if(A[i] >= A[i+1]) {
      printf("%s%d", separator, A[i]);
      separator = "-";
    }
}
printf("\n");

Upvotes: 0

Sourav Das
Sourav Das

Reputation: 107

#include<stdio.h>
int main()
{
    int N,i,j;
    scanf("%d",&N); 
    int A[N+1];
    for(int i=0;i<N;i++)
    {
        scanf("%d",&A[i]);
    }
    A[N] = 1;
    int flag = 0; //added a new variable here
    for(int i=0;i<N;i++)
    {
        if(A[i] >= A[i+1])
        {   
            if(flag == 0){ //check if its printing for the first time 
                printf("%d",A[i]); // then print without dash
                flag = 1;          //change the flag so that this block never executes again
            }else{
                printf("-%d",A[i] ); //else print a dash and then the number
            }
        }
    }
    printf("\n");
    return 0;
}

Upvotes: 0

paddy
paddy

Reputation: 63471

Personally, my preference is to print the separator character before. The simple reason is that generally this happens in loops that begin at zero, so I can do a zero-test as follows:

for(int i = 0; i < N; i++)
{
    if (i > 0)
        putc('-', stdout);
    printf("%d", A[i]);
}

BUT, in your case, your loop won't always print a value. So you actually need to be smarter. There are many ways to achieve this, but for simplicity, why not just use a flag:

int has_output = 0;
for(int i = 0; i < N; i++)
{
    if(A[i] >= A[i+1])
    {
        if (has_output)
            putc('-', stdout);
        else
            has_output = 1;
 
        printf("%d", A[i]);
    }
}

Notice that the preference is still to print the separator just in time. In other words, only when you determine that you need to print something.

Going a bit more crazy:

const char* fmt[2] = { "%d", "-%d" };
int has_output = 0;
for(int i = 0; i < N; i++)
{
    if(A[i] >= A[i+1])
    {
        printf(fmt[has_output], A[i]);
        has_output = 1;
    }
}

Upvotes: 1

Related Questions