Formatting Output

This is a discussion on Formatting Output within the C Programming forums, part of the General Programming Boards category; I am trying to format the results of my program into five different columns of an equal number. For example, ...

  1. #1
    Unregistered
    Guest

    Formatting Output

    I am trying to format the results of my program into five different columns of an equal number. For example, my program generates 50 random numbers and I am trying to print them out in sets of ten. Here is my code from my main function. Anyone's help would be greatly appreciated

    int main(void)
    {
    int grades[50];
    float rank[50]={0};
    int x, anotherrecord;

    do
    {
    randFill(grades);
    sortDescend(grades);
    rankCalc(grades, rank);
    for(x=0;x<50;x++)
    {
    printf("%d - Rank:",grades[x]);
    printf("%3.1f\n", rank[x]);
    }

    printf("\n Press 1 to generate another random sample: ");
    scanf("%d", &anotherrecord);
    }
    while (anotherrecord == 1);

    return 0;
    }

  2. #2
    Unregistered
    Guest
    [code]

    for(x=0;x<50;x++)
    {
    printf("%d - Rank:",grades[x]);
    printf("%3.1f\n", rank[x]);
    if( x % 10 == 0) printf("\n");

    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Ive had to log in to edit my answer.

    Code:
    for(x=0;x<50;x++) 
    { 
        printf("%d - Rank:",grades[x]); 
        printf("%3.1f   ", rank[x]);    /* note spaces  - no \n*/
        if(x % 10 == 0) printf("\n");
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    printf("%d - Rank:",grades[x]);
    printf("%3.1f\n", rank[x]);
    You can condense this to one line:

    printf( "%d - Rank: %3.1f\n", grades[x], rank[x] );

    Additionally, you may want to use: "%3d" instead of just "%d".

    However, what exactly are you trying to do? Do you want more
    than one one one line? If so, that \n is your problem. If not, then
    what are you trying to do?

    How about this:
    Code:
    if( x % 10 == 0 )
    {
        printf("\nPress return to see the next 10.\n");
        fgetc( stdin );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    I now am faced with one more problem. They are formatted in sets of ten, but I need the output to be across the screen not down the screen.

    My output currently looks like this

    10
    10
    10

    10
    10
    10

    and so on. It should look like

    10 10 10 10 10
    10 10 10 10 10
    10 10 10 10 10

    I am going to try and figure out how to do this, but if someone has experience with it and can help me it would be appreciated

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We've already told you how to fix this. You have to remove that first \n in your printf statement. Second, you have to adjust your printf statements so that the columns line up correctly. Remember that a standard console window is only 80 letters across. This means that you have:

    "80 / myprintfstatementlength"

    as you maximum number of columns. That being the case, you then have to modify your:

    if( x % y == 0 ) printf("\n");

    In other words, if your printf is 20 characters in length, then at a maximum, you only get 4. So, replace y with 4.

    Quzah.
    Last edited by quzah; 03-25-2002 at 04:36 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    skumar
    Guest
    If what u want is to format across columns something like this:

    col1 col2 col3 col4 col5
    1 11 21 31 41
    2 12 22 32 42
    .. .. .. .. ..

    then something like the below pgm cud work. check it out

    --------------------------------------------------
    #include <stdio.h>

    int main()
    {
    int x[50], i = 0, j = 0;

    for(i = 0; i <= 49; i++)
    {
    x[i] = i; /*initialize the array */
    }

    for(i = 0; i < 10; i++)
    {
    for(j = 0; j < 5; j++) /* print in 5 columns */
    {
    printf("element = (%d) \t" , x[i + j*10]);
    }
    printf("\n"); /* go to the next row */
    }

    return 0;
    }


    --------------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21