目前我的简单数组程序正在生成我想要的数据,但我希望它的格式更清晰,类似:
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;
}
非常感谢任何帮助!
答案 0 :(得分: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");
答案 1 :(得分: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" );
}