Print sequence of numbers repeating in arithmetic progression

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 value and prints up to the nth line.

Why does not 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;
}
    
asked by anonymous 22.09.2017 / 02:56

1 answer

7

There are two problems., The loop should start from 1, after all if each number should be repeated the number of times of itself, it does not have to print 0, and also was printing the counter and not the number that should be repeated , so I changed e to r . It could have started by 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);  
}

See running on ideone . And at Coding Ground . Also I put it in GitHub for future reference .

    
22.09.2017 / 03:03