skip to Main Content

I’m trying to create an output of sequential asterisks using for-loops. The idea is to use inputs by the user to determine the number of rows and the increase of asterisks between each row. I can’t make the program print asterisks beyond the first row and when I have been able to it’s been the same amount of asterisks.
What am I doing wrong? I know there’s some other issues, I’d like to try them on on my own so my question is specifically about the output.

Only one row:

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

int main(int argc, char *argv[])
{
    int arg1, arg2, x, y, z;
    scanf("%d, %d", &arg1, &arg2);
    arg1 = atoi(argv[1]);
    z = arg2 = atoi(argv[2]);
    x = 0;
    if (argc < 2 || argc > 3)
    {
        printf("Wrong number of arguments, input 2 arguments.n");
    }
    else
    {
        for (y = 1; y <= arg1; y ++)
        {
            for (; arg2 > 0; arg2 --)    
            {
                printf("*");
            }
        arg2 = arg2 + arg2;
        printf("n");
        }
    printf("Total: %dn", x);
    return 0;
    }
}
ubuntu@lab1:~$ gcc p4.c -o p4
ubuntu@lab1:~$ ./p4 4 5
*****



Total: 0
ubuntu@lab1:~$

Intended example output:

$ gcc p4.c
$ ./a.out 3 2
**
****
******
Totalt: 12
$ ./a.out 0 25
Totalt: 0
$ ./a.out 4 4
****
********
************
****************
Totalt: 40
$ ./a.out
Usage: ./a.out rows growth
$

2

Answers


  1. For starters this if statement

    if (argc < 2 || argc > 3)
    {
        printf("Wrong number of arguments, input 2 arguments.n");
    }
    

    must precede these statements

    arg1 = atoi(argv[1]);
    z = arg2 = atoi(argv[2]);
    

    that must be moved into the else part.

    Otherwise the program can invoke undefined behavior.

    And moreover the condition is incorrect. It must look at least like

    if ( argc != 3 )
    {
        printf("Wrong number of arguments, input 2 arguments.n");
    }
    

    The variable x is not changed within the program. So its output does not make a sense

    x = 0;
    //...
    printf("Total: %dn", x);
    

    After the inner for loop

    for (; arg2 > 0; arg2 --)    
    {
        printf("*");
    }
    

    the variable arg2 is equal to 0.

    So this statement

    arg2 = arg2 + arg2;
    

    keeps the value of arg2 equal to 0.

    The program can look for example the following way

    #include <stdio.h>
    #include <stdlib.h>
    
    int main( int argc, char * argv[] )
    {
        if (argc != 3)
        {
            puts( "Wrong number of arguments, input 2 arguments." );
        }
        else
        {
            int n = atoi( argv[1] );
            int m = atoi( argv[2] );
    
            if (!( 0 < n ) || !( 0 < m ))
            {
                puts( "The arguments must be positive integeres. Try again" );
            }
            else
            {
                const char c = '*';
                long long int total = n * ( n + 1 ) / 2 * m;
    
                for (int i = 0; i < n; i++ )
                {
                    for (long long int j = 0; j < ( i + 1ll ) * m; j++)
                    {
                        putchar( c );
                    }
                    putchar( 'n' );
                }
    
                printf( "Total: % lldn", total );
            }
        }
    }
    

    If to pass to the program the values 4 and 5 then the program output will look like

    *****
    **********
    ***************
    ********************
    Total:  50
    

    Within this for loop

    for (long long int j = 0; j < ( i + 1ll ) * m; j++)
    

    the variable j declared as having the type long long int instead of just int because the prodxct of n * m can be a big number that can not fit in an object of the type int.

    Login or Signup to reply.
  2. Have a look at this:

    int main() { /* OP: "...so my question is specifically about the output." */
    
        // To generate output, use some values
        int rows = 5; // = atoi( argv[ 1 ] );
        int grow = 4; // = atoi( argv[ 2 ] );
        int star = grow;
        int total = 0;
    
        // in lieu of a command line...
        printf( "rows: %d, growth: %dn", rows, grow );
    
        while( rows-- ) {
    
            // output the stars for this row
            for( int n = 0; n < star; n++ )
                putchar( '*' );
            putchar( 'n' );
    
            total += star; // accumulate a total
    
            star += grow; // and increase for the next loop
        }
    
        printf( "Total: %dn", total );
    
        return 0;
    }
    

    Output

    rows: 5, growth: 4
    ****
    ********
    ************
    ****************
    ********************
    Total: 60
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search