2

The program takes an arbitrary amount of integers as an input and gives an output of that integer and that amount of stars.

E.g.

In: 1 2 3
Out: 
1 | *
2 | **
3 | ***

Another example:

In: 2 5 6 8
Out:
2 | **
5 | *****
6 | ******
8 | ********

How do I do it??

Btw, amateur C programmer

And how do I add a single line space "\n" between lines in Stack Overflow question format

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
tcatchy
  • 849
  • 1
  • 7
  • 17

3 Answers3

4

for reading numbers from a line, you can :

#include <stdio.h>

int main(){
    char buffer[1000];
    if (fgets(buffer, sizeof(buffer), stdin) != 0){
        int i,j,a;
        for(i=0; sscanf(buffer+i,"%d%n",&a,&j)!=EOF; i+=j){
            while(a-->0){
                printf("*");
            }
            printf("\n");
        }
    }
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lavin
  • 2,276
  • 2
  • 13
  • 15
0

It's better to have a loop for this manner.
While user has not entered "\n" your program should be able to consider them as integers. Of course you can add some other checks as well.

Something like this:

int number = 0;
char c = '';
while(c != '\n'){
    getch(c);
    scanf("%d", &number);
    /*Do your star thing or add this number to an array for the later consideration*/
}


This is not fully tested and you might have to have some changes.

Matin Kh
  • 5,192
  • 6
  • 53
  • 77
0
#include <stdio.h>

#define SIZE 8

int input_numbers(int numbers[]){
    int i=0,read_count;
    char ch;

    printf("In: ");
    while(EOF!=(read_count=scanf("%d%c", &numbers[i], &ch))){
        if(read_count==2)
            ++i;
        if(i==SIZE){
            fprintf(stderr, "Numeric number has reached the Max load.\n");
            return i;
        }
        if(ch == '\n')
            break;
    }
    return i;
}

void output_numbers(int numbers[], int size){
    int i,j;
    printf("Out:\n");
    for(i=0;i<size;++i){
        printf("%d | ", numbers[i]);
        for(j=0;j<numbers[i];++j){
            printf("*");
        }
        printf("\n");
    }
}

int main(void){
    int numbers[SIZE];
    int n;

    n=input_numbers(numbers);
    output_numbers(numbers, n);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70