0

目前,我的简单数组程序正在生成我想要的数据,但我希望它采用更清晰的格式,如下所示:

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

等等

#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;
}

任何帮助将非常感激!

4

2 回答 2

1

如果我猜对了,您希望"This is ... than 125"在值之后的同一行上打印?然后只打印值但没有换行符,然后是文本(带有换行符)。

喜欢

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");
于 2013-11-01T17:44:10.513 回答
0
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" );
}
于 2013-11-01T17:51:15.190 回答