simple output problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RadiationX

    #1

    simple output problem

    Hello, I have a simple print output questioin. What I'm trying to do is
    print an array of lenth 20 which holds 20 ints in the form of five
    columns and four rows. like this below

    12 80 90 6 7
    1 6 99 8 9
    8 3 5 3 12
    9 4 2 2 15


    I have no idea how to print it like this. Below is what I have so far.
    It's wrong but I think I'm on the right track. What the code below does
    is print one column broken up into five parts. I really would
    appreciate some help.



    #include <stdio.h>
    #include <stdlib.h>
    #include<time.h >
    int random();


    int main()
    {
    int j;
    int user;
    int k;

    int golf[20]= {0};
    int b;
    srand(time(NULL ));

    // printf("%s%13s\ n", "Element", "Value" );

    for ( k = 0; k< 20; k++ )
    {
    b = random();
    golf[20] = b;

    if(k%4==0)
    {
    printf("\n");
    }


    printf("%8d\n", golf[20]);
    }






    system("PAUSE") ;
    return 0;
    }

    int random()
    {
    int x;

    x = 1 + rand()%99;
    return x;
    }

  • Michael Mair

    #2
    Re: simple output problem

    RadiationX schrieb:[color=blue]
    > Hello, I have a simple print output questioin. What I'm trying to do is
    > print an array of lenth 20 which holds 20 ints in the form of five
    > columns and four rows. like this below
    >
    > 12 80 90 6 7
    > 1 6 99 8 9
    > 8 3 5 3 12
    > 9 4 2 2 15
    >
    > I have no idea how to print it like this. Below is what I have so far.
    > It's wrong but I think I'm on the right track. What the code below does
    > is print one column broken up into five parts. I really would
    > appreciate some help.
    >
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include<time.h >
    > int random();[/color]

    Use full prototypes:
    int random (void);
    This helps your compiler warn you if you give a wrong
    argument list.
    [color=blue]
    > int main()[/color]

    Mainly a matter of style:
    int main (void)
    [color=blue]
    > {
    > int j;
    > int user;
    > int k;
    >
    > int golf[20]= {0};
    > int b;
    > srand(time(NULL ));
    >
    > // printf("%s%13s\ n", "Element", "Value" );
    >
    > for ( k = 0; k< 20; k++ )
    > {
    > b = random();
    > golf[20] = b;
    >
    > if(k%4==0)
    > {
    > printf("\n");
    > }
    >
    >
    > printf("%8d\n", golf[20]);
    > }[/color]

    What you are doing at the moment is do two things at a
    time.

    Split this into two things:
    1. Populating the array
    2. Printing it

    Ideally, make these functions of their own:
    #define GOLF_SIZE 20
    #define NUM_COLUMNS 5

    int main (void)
    {
    int golf[GOLF_SIZE] = {0};

    populateArray(g olf, GOLF_SIZE);
    printArray(golf , GOLF_SIZE, NUM_COLUMNS);

    #ifdef USE_PAUSE
    /* Whatever happens in system() is not topical to comp.lang.c */
    system("PAUSE") ;
    #endif

    return 0;
    }

    where we have
    void populateArray(i nt *array, int size);
    void printArray(int *array, int size, int columns);
    If you like the conceptual cleanness, use size_t instead
    of int as type for the size and column number parameters.
    [color=blue]
    >
    >
    >
    >
    >
    >
    > system("PAUSE") ;
    > return 0;
    > }
    >
    > int random()
    > {
    > int x;
    >
    > x = 1 + rand()%99;
    > return x;
    > }[/color]

    There are better ways to implement random but I will not
    dwell on that.

    Let us go back to your question:
    void printArray(int *array, int size, int columns)
    {
    int rowcount, colcount, rows, rest;

    if ((NULL == array) || (size < 0) || (columns <= 0))
    {
    /* insert your error handling here */
    return;
    }

    rows = size/columns;
    rest = size%columns;

    /* print the full rows */
    for (rowcount = 0; rowcount < rows; rowcount++)
    {
    for (colcount = 0; colcount < columns; colcount++)
    {
    printf("%3d\t", array[rowcount*column s + colcount]);
    }
    putchar('\n');
    }
    /* Now for the rest*/
    for (colcount = 0; colcount < rest; colcount++)
    {
    printf("%3d\t", array[rows*columns + colcount]);
    }
    putchar('\n');
    }
    The function is not tested but I think you get the
    concept.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • Fred Kleinschmidt

      #3
      Re: simple output problem


      "RadiationX " <heavyreader@gm ail.com> wrote in message
      news:1141885763 .892830.67670@i 39g2000cwa.goog legroups.com...[color=blue]
      > Hello, I have a simple print output questioin. What I'm trying to do is
      > print an array of lenth 20 which holds 20 ints in the form of five
      > columns and four rows. like this below
      >
      > 12 80 90 6 7
      > 1 6 99 8 9
      > 8 3 5 3 12
      > 9 4 2 2 15
      >
      >
      > I have no idea how to print it like this. Below is what I have so far.
      > It's wrong but I think I'm on the right track. What the code below does
      > is print one column broken up into five parts. I really would
      > appreciate some help.
      >
      >
      >
      > #include <stdio.h>
      > #include <stdlib.h>
      > #include<time.h >
      > int random();
      >
      >
      > int main()
      > {
      > int j;
      > int user;
      > int k;
      >
      > int golf[20]= {0};
      > int b;
      > srand(time(NULL ));
      >
      > // printf("%s%13s\ n", "Element", "Value" );
      >
      > for ( k = 0; k< 20; k++ )
      > {
      > b = random();
      > golf[20] = b;[/color]
      Bad news - attempt to store a value outside the bounds of golf.
      Anything can happen.[color=blue]
      >
      > if(k%4==0)
      > {
      > printf("\n");
      > }
      >
      >
      > printf("%8d\n", golf[20]);[/color]
      Why are you using golf array? What's wrong with just "b"?[color=blue]
      > }
      >
      > system("PAUSE") ;
      > return 0;
      > }
      >
      > int random()
      > {
      > int x;
      >
      > x = 1 + rand()%99;
      > return x;
      > }
      >[/color]

      #define RANGE 20
      #define COLUMNS 5

      for ( k=0; k > RANGE; k++) {
      b = random();
      printf( "%-8d", b);
      if ( k%COLUMNS == (COLUMNS-1) ) {
      printf("\n");
      }
      }

      For right-justified values, use "8d" instead of "%-8d".
      --
      Fred L. Kleinschmidt
      Boeing Associate Technical Fellow
      Technical Architect, Software Reuse Project


      Comment

      • Micah Cowan

        #4
        Re: simple output problem

        "RadiationX " <heavyreader@gm ail.com> writes:
        [color=blue]
        > #include <stdio.h>
        > #include <stdlib.h>
        > #include<time.h >
        > int random();[/color]

        int random(void);
        [color=blue]
        >
        >
        > int main()[/color]

        int main(void);
        [color=blue]
        > {
        > int j;
        > int user;
        > int k;
        >
        > int golf[20]= {0};
        > int b;
        > srand(time(NULL ));
        >
        > // printf("%s%13s\ n", "Element", "Value" );
        >
        > for ( k = 0; k< 20; k++ )[/color]

        "Magic numbers" should be discouraged. What happens if you want to
        expirement with a 24-element array? or a 16-element array? This for
        loop would stop working, unless you remember to change the number
        here, too. You should probably use a macro:

        #define NUM_ELEMS 20
        [color=blue]
        > {
        > b = random();
        > golf[20] = b;
        >
        > if(k%4==0)
        > {
        > printf("\n");
        > }
        >
        >
        > printf("%8d\n", golf[20]);
        > }[/color]

        There's no need to populate golf[] here (which, as someone else
        pointed out, you're doing wrong anyway: you're actually writing one
        past the end of golf[]). However, assuming this is a homework
        assignment where you actually have to have an array that you print
        out, I recommend that you use two loops: one to populate the array
        using random(), and another to print it out. Ideally, these could be
        in two separate functions.

        To fix the off-by-one thing, you'll pretty much want to use golf[k]
        where you've been using golf[20].

        Just copy the loop into two functions, take the "if (k%4)" and
        printf()s out of the first one, and the assignments out of the other,
        and you should be golden. The functions could look like:

        void populate_array( int *golf);
        void print_array(int *golf);

        Use the defined macro in the loops for these functions, or else pass
        in the size of the array as well.

        [color=blue]
        > system("PAUSE") ;[/color]

        My system doesn't have the above, and yours very likely doesn't need
        it. In windows, you can configure your command window to not close
        automatically when the program exits (don't ask me how--read your
        documentation).
        [color=blue]
        > return 0;
        > }
        >
        > int random()
        > {
        > int x;
        >
        > x = 1 + rand()%99;
        > return x;
        > }[/color]

        The vast majority of rand() implementations have very poor randomness
        in the lower bits: it is usually recommended that you use something like:

        x = 1.0 + (99.0*rand()/(RAND_MAX+1.0)) ;

        ....not that it matters a whole lot for your purposes...

        Comment

        Working...