C 程序从数组元素中标记为 "Min" 和 "Max" 并在控制台上的 min/max 相应位置列上打印
C Program to Mark as "Min" and "Max" from the array elements and print on the min/max corresponding location column on console
我正在尝试标记对应于 int Min & Max 数字的 Min 和 Max,例如:
Serial No. Min/Max
1 Min
2
3
4
5 Max
在控制台,其他列正在按照我的要求打印,但无法像上面那样打印这个“Min/Max”列。
这部分我的代码如下:
int max = userArray[0];
int min = userArray[0];
for(i=1; i<=10; i++){
//excluded codes to print other columns
//code for my concerned column print
if(userArray[i]>max){
max = userArray[i];
}
if(userArray[i]==max){
printf("%49s\n","Max");
}
if(min > userArray[i]){
min = userArray[i];
}
if(userArray[i]==min){
printf("%49s\n","Min");
}
}
您无法在打印其他列时找到并打印最小值和最大值。仅仅是因为在查看所有元素之前,您无法知道最小值和最大值。
您需要在 开始打印循环之前找到最小值和最大值 。所以你需要一个额外的循环。
// Find index of min and max values
int max_idx = 0;
int min_idx = 0;
int max = userArray[max_idx];
int min = userArray[min_idx];
for(int i=0; i<10; i++)
{
if (userArray[i]>max)
{
max_idx = i;
max = userArray[max_idx];
}
else if (userArray[i] < min)
{
min_idx = i;
min = userArray[min_idx];
}
}
// Now do the print
for(i=0; i<10; i++)
{
//excluded codes to print other columns
if(i == max_index && i == min_index)
{
printf("%49s\n","Min/Max");
}
else if(i == max_index)
{
printf("%49s\n","Max");
}
else if(i == min_index){
printf("%49s\n","Min");
}
}
请注意,使用 min_index
和 max_index
可确保例如即使最大值出现多次,Max也只打印一次。
您需要在打印 min
或 max
之前完成一次循环以识别 min
和 max
值。
int main()
{
int userArray[10] = {3, 2, 1, 4, 6, 5, 7, 8, 10, 9};
int max = userArray[0];
int min = userArray[0];
for(int i=0; i<10; i++){
//excluded codes to print other columns
//code for my concerned column print
if(userArray[i]>max){
max = userArray[i];
}
if(userArray[i] < min){
min = userArray[i];
}
}
printf("Serial No.\tMin/Max\n");
for(int i=0;i<10;i++)
{
printf("%d\t", userArray[i]);
if(userArray[i] == min)
printf("\tMin");
if(userArray[i] == max)
printf("\tMax");
printf("\n");
}
printf("\n");
return 0;
}
打印所需的列
Serial No. Min/Max
3
2
1 Min
4
6
5
7
8
10 Max
9
我正在尝试标记对应于 int Min & Max 数字的 Min 和 Max,例如:
Serial No. Min/Max
1 Min
2
3
4
5 Max
在控制台,其他列正在按照我的要求打印,但无法像上面那样打印这个“Min/Max”列。
这部分我的代码如下:
int max = userArray[0];
int min = userArray[0];
for(i=1; i<=10; i++){
//excluded codes to print other columns
//code for my concerned column print
if(userArray[i]>max){
max = userArray[i];
}
if(userArray[i]==max){
printf("%49s\n","Max");
}
if(min > userArray[i]){
min = userArray[i];
}
if(userArray[i]==min){
printf("%49s\n","Min");
}
}
您无法在打印其他列时找到并打印最小值和最大值。仅仅是因为在查看所有元素之前,您无法知道最小值和最大值。
您需要在 开始打印循环之前找到最小值和最大值 。所以你需要一个额外的循环。
// Find index of min and max values
int max_idx = 0;
int min_idx = 0;
int max = userArray[max_idx];
int min = userArray[min_idx];
for(int i=0; i<10; i++)
{
if (userArray[i]>max)
{
max_idx = i;
max = userArray[max_idx];
}
else if (userArray[i] < min)
{
min_idx = i;
min = userArray[min_idx];
}
}
// Now do the print
for(i=0; i<10; i++)
{
//excluded codes to print other columns
if(i == max_index && i == min_index)
{
printf("%49s\n","Min/Max");
}
else if(i == max_index)
{
printf("%49s\n","Max");
}
else if(i == min_index){
printf("%49s\n","Min");
}
}
请注意,使用 min_index
和 max_index
可确保例如即使最大值出现多次,Max也只打印一次。
您需要在打印 min
或 max
之前完成一次循环以识别 min
和 max
值。
int main()
{
int userArray[10] = {3, 2, 1, 4, 6, 5, 7, 8, 10, 9};
int max = userArray[0];
int min = userArray[0];
for(int i=0; i<10; i++){
//excluded codes to print other columns
//code for my concerned column print
if(userArray[i]>max){
max = userArray[i];
}
if(userArray[i] < min){
min = userArray[i];
}
}
printf("Serial No.\tMin/Max\n");
for(int i=0;i<10;i++)
{
printf("%d\t", userArray[i]);
if(userArray[i] == min)
printf("\tMin");
if(userArray[i] == max)
printf("\tMax");
printf("\n");
}
printf("\n");
return 0;
}
打印所需的列
Serial No. Min/Max
3
2
1 Min
4
6
5
7
8
10 Max
9