user2945766
user2945766

Reputation: 1

How to print statements into different columns

Currently my simple array program is generating the data I would like, but I would like it in a much cleaner format, something like so:

Element[0] = 100      Element[26] = 126
 Less than 125         Greater than 125
Element[1] = 101      Element[27] = 127
 Less than 125         Greater than 127

etc.

#include <stdio.h>

int main ()
{
   int arr[ 51 ]; /* arr is an array of 10 integers */
   int x,y;

   /* initialize elements of arr[] to 0 */         
   for ( x = 0; x < 51; x++ )
   {
      arr[ x ] = x + 100; /* set element at int x to x + 100 */
   }

   /* outputs the value of each arra y element */
   for (y = 0; y < 51; y++ )
   {
      if (y <= 25)
       {
            printf("Element[%d] = %d\n", y, arr[y]) && printf("   This is less than 125\n");
       }    
      if (y >=26)
       {
            printf("Element[%d] = %d\n", y, arr[y]) && printf("   This is greater than 125\n");
       } 
    }


   return 0;
}

Any help would be much appreciated!

Upvotes: 0

Views: 65

Answers (2)

clcto
clcto

Reputation: 9648

for( int y = 0; y < 26; ++y )
{
    printf( "Element[%02d] = %d\tElement[%02d] = %d\n",
             y, arr[y], y+26, arr[y+26] );
    if( arr[y] < 125 )
        printf( "is less than 125\t" );
    else
        printf( "is greater than or equal to 125\t" );
    if( arr[y+26] < 125 )
        printf( "is less than 125\n" );
    else
        printf( "is greater than or equal to 125\n" );
}

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409196

If I'm guessing right, you want the "This is ... than 125" printed in on the same line after the value? Then just print the value but without a newline, followed by the text (with a newline).

Like

printf("Element[%02d] = %-3d", y, arr[y]);

if (arr[y] < 125)
    printf("\tThis is less than 125\n");
else if (arr[y] == 125)
    printf("\tThis is equal to 125\n");
else
    printf("\tThis is greater than 125\n");

Upvotes: 1

Related Questions