Home > Enterprise >  printing * corresponding to the number of iteration
printing * corresponding to the number of iteration

Time:11-07

So in C I'm supposed to let the user input an integer n from the interval [5, 25]. And then, for every number from 1 to n, in a new line print that many stars so it would look something like this:

"*

**

***"

I tried doing it like this, but it's not working. What am I doing wrong here?

`

#include <stdio.h>

int main(void)
{

    int n, i;
    char star = '*';

    do {
        printf("Input an int from [5, 25]");
        scanf("%d", &n);
    } while (n < 5 || n >= 25);


    for (i=0; i < n; i  ){
        star  = '*';
        printf("%c", star);

    }

return 0;
}

`

CodePudding user response:

You cannot write star = '*'; because you declared star as a char, C is strongly typed, a char is a char not a table of char.

You have to use nested loop, like this for example:

#include <stdio.h>

int main(void)
{

    int n, i, j;
    char star = '*';

    do
    {
        printf("Input an int from [5, 25]");
        scanf("%d", &n);
    } while (n < 5 || n >= 25);

    for (i = 1; i <= n; i  )
    {
        for (j = 1; j <= i; j  )
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

CodePudding user response:

You need nested loops

    for (int i=0; i < n; i  )
    {
        for(int j = 0; j <= i; j  )
            printf("*");
        printf("\n");
    }         

or if you want to use strings:

    char str[n   1];
    for (int i=0; i < n; i  )
    {
        str[i] = '*';
        str[i   1] = 0;
        puts(str);
    }      

https://godbolt.org/z/aT8brP1ch

CodePudding user response:

The statement

star  = '*';

is not the correct way to concatenate two strings in C. In order to do this, you can define an array with sufficient space for the string and use the function strcat, like this:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int n;

    //initialize "stars" to an empty string
    char stars[20] = {0};

    do {
        printf("Input an int from [5, 25]: ");
        scanf("%d", &n);
    } while (n < 5 || n >= 25);

    //build the string containing the stars using repeated
    //string concatentation
    for ( int i = 0; i < n; i   ) {
        strcat( stars, "*" );
    }

    //print the string
    printf( "%s\n", stars );

    return 0;
}

This program has the following behavior:

Input an int from [5, 25]: 5
*****

However, this is highly inefficient and unnecessarily complicated. Instead of first building the string in an array before printing it out all at once, it is usually easier to simply print it one character at a time:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int n;

    do {
        printf("Input an int from [5, 25]: ");
        scanf("%d", &n);
    } while (n < 5 || n >= 25);

    //print the stars one character at a time
    for ( int i = 0; i < n; i   ) {
        putchar( '*' );
    }

    //end the line
    putchar( '\n' );

    return 0;
}

This program has the same output as the first program.

You now have the solution for printing out a single line. However, your task involves printing out several lines. This will require a nested loop. In accordance with the community guidelines on homework questions, I will not provide the full solution at this time, as you should attempt to do this yourself, first.

CodePudding user response:

char is an integral type - that is, it represents a number. '*' is a Character Constant, which actually has the type int.

char star = '*';
star  = '*';

In ASCII, this is no different from

char star = 42;
star  = 42;

A string is a series of nonzero bytes, followed by a zero byte (the null terminating character, '\0'). You cannot build a string by adding two integers together.

To build a string, you must place each byte in a buffer in sequence, and ensure a null terminating byte follows.

#include <stdio.h>

#define MIN 5
#define MAX 25

int main(void)
{
    int n;

    do {
        printf("Input an int from [%d, %d): ", MIN, MAX);

        if (1 != scanf("%d", &n)) {
            fprintf(stderr, "Failed to parse input.\n");
            return 1;
        }
    } while (n < MIN || n >= MAX);

    char buffer[MAX   1] = { 0 };

    for (int i = 0; i < n; i  ) {
        buffer[i] = '*';
        buffer[i   1] = '\0';
        puts(buffer);
    }
}

Aside: never ignore the return value of scanf.

Or you can avoids strings, and just print the characters directly.

for (int i = 0; i < n; i  ) {
    for (int j = 0; j <= i; j  )
        putchar('*');

    putchar('\n');
}

CodePudding user response:

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

int main() {
    
    int n,i,j;
    printf("enter a number between 5 & 25");
    scanf("%d",&n);
    
    for(i=1;i<=n;i  ){
        for(j=1;j<=i;j  ){
            printf("*");
        }
        printf("\n");
    }
    
    
    return 0;
}

CodePudding user response:

String concatenation does not work like that in C, instead use strcat().

  • Related