Print sequence of numbers repeating in arithmetic progression

Asked

Viewed 837 times

1

Make a program to print:

1
2   2
3   3   3
.....
n   n   n   n   n   n  ... n

for a user-informed n. Use a function that takes an integer n value and prints up to nth line.

Why doesn’t it work?

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


void tarefa(int r){
    int e;

    for (e=0; e<=r;e++){

printf(" %d  ", e);


    }


}


int main(int argc, char *argv[]) {

    int x, j;


    scanf("%d", &x);

    for(j=0; j<=x; j++){

    tarefa(j);  

    }
    return 0;
}
  • Error? Which one? Or if there is no error, which is the output of the program? Have you tried the table test?

  • What is the error? You are not printing what you should or are dropping any fault during execution?

  • Is it the lack of a line end? Note also that you do not need (nor should) pass argument 0

  • the problem was in the compiler...

  • is already working

  • Then post what you did to resolve the question as an answer, and help the next people who come across this same problem

  • 1

    The problem was not in the compiler, the problem is never in the compiler. And can do whatever is not going to solve, the algorithm was wrong.

  • 1

    bigown has already solved... I also did in the ideone: https://ideone.com/vOCB9O

  • thanks ai...nn was working out msm

  • 1

    @Rovannlinhalis, in matters of competitive programming, usually they specify for the line break to be only the \n. In such cases, perhaps your code could generate a Presentation Error in the BOCA or in the URI Judge.

  • 1

    @Pedrojoao The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

  • @Pedrojoao? Maniero’s answer answered your question?

Show 7 more comments

1 answer

7

You have two problems. , The loop should start from the 1, after all if each number should be repeated the number of times it itself, has no why to print the 0, and was also printing the counter and not the number that should be repeated, then I switched the e for r. Could have started at 1 on the other loop.

#include <stdio.h>

void tarefa(int r) {
    for (int e = 1; e <= r; e++) printf(" %d  ", r);
    printf("\n");
}

int main(int argc, char *argv[]) {
    int x;
    scanf("%d", &x);
    for (int j = 1; j <= x; j++) tarefa(j);  
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • May I suggest a printf("\n"); at the end of the function tarefa? If it is a competitive programming problem, the lack of line break would generate a Presentation Error. Fez this Fork with this change, and also starting the main with j = 1

  • 1

    It is possible yes, then I move.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.