simple output problem

Discussion in 'C Programming' started by RadiationX, Mar 9, 2006.

  1. RadiationX

    RadiationX Guest

    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;
    }
     
    RadiationX, Mar 9, 2006
    #1
    1. Advertising

  2. RadiationX

    Michael Mair Guest

    RadiationX schrieb:
    > 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();


    Use full prototypes:
    int random (void);
    This helps your compiler warn you if you give a wrong
    argument list.

    > int main()


    Mainly a matter of style:
    int main (void)

    > {
    > 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]);
    > }


    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(golf, 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(int *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.

    >
    >
    >
    >
    >
    >
    > system("PAUSE");
    > return 0;
    > }
    >
    > int random()
    > {
    > int x;
    >
    > x = 1 + rand()%99;
    > return x;
    > }


    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*columns + 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.
     
    Michael Mair, Mar 9, 2006
    #2
    1. Advertising

  3. "RadiationX" <> wrote in message
    news:...
    > 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;

    Bad news - attempt to store a value outside the bounds of golf.
    Anything can happen.
    >
    > if(k%4==0)
    > {
    > printf("\n");
    > }
    >
    >
    > printf("%8d\n", golf[20]);

    Why are you using golf array? What's wrong with just "b"?
    > }
    >
    > system("PAUSE");
    > return 0;
    > }
    >
    > int random()
    > {
    > int x;
    >
    > x = 1 + rand()%99;
    > return x;
    > }
    >


    #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
     
    Fred Kleinschmidt, Mar 9, 2006
    #3
  4. RadiationX

    Micah Cowan Guest

    "RadiationX" <> writes:

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


    int random(void);

    >
    >
    > int main()


    int main(void);

    > {
    > 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++ )


    "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

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


    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.


    > system("PAUSE");


    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).

    > return 0;
    > }
    >
    > int random()
    > {
    > int x;
    >
    > x = 1 + rand()%99;
    > return x;
    > }


    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...
     
    Micah Cowan, Mar 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    601
    John Bentley
    Sep 10, 2003
  2. chuck amadi
    Replies:
    1
    Views:
    499
    Larry Bates
    Jun 23, 2004
  3. RadiationX

    Simple output problem

    RadiationX, Jan 27, 2006, in forum: C Programming
    Replies:
    13
    Views:
    518
    Dave Thompson
    Feb 6, 2006
  4. Paul
    Replies:
    1
    Views:
    438
  5. dillon
    Replies:
    1
    Views:
    185
    higabe
    Nov 17, 2003
Loading...

Share This Page