替换数组中的值 [英] Replacing a value in an array

查看:129
本文介绍了替换数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用最大值替换数组中的最小值,我的输出有问题。



输出:

I'm trying to replace the min value in an array in the max value, I have an issue with the output.

the output:

after replace = 5 
after replace = 6 
after replace = 7 
after replace = 7 
after replace = 7 
after replace = 3 





应该是:

4

5

6

7

7

3



我尝试过:





it should be:
4
5
6
7
7
3

What I have tried:

int main( void )
{
struct str
{
   int  arr[6];   
};

struct str select =
{    
    { 4, 5, 6, 7,1,3 }
};
struct tt{
int a[1];
};
struct tt t;

for (int i=0;i<6;i++){
printf("arr = %d \n",select.arr[i]);
}

int min=select.arr[0];
int max=select.arr[0];
for (int i = 0; i < 6; i++) {
           
        if (select.arr[i] > max){
            max=select.arr[i];
            t.a[i]=max;}
         else if (select.arr[i] < min){
            min = select.arr[i];
            select.arr[i]=max;}
    }

printf("min = %d     max =%d \n",min,max);
for (int i=0;i<6;i++){
printf("after replace = %d \n",select.arr[i]);
}
}

推荐答案

数组有多大 a tt 结构中?

你使用了多少个元素元素?



这不是你的问题,但这是你需要照顾的未来问题。



据我所知,你的代码做了你所描述的应该做的:

How big is the array a in the tt struct?
How many elements of that array do you use?

That isn't your problem, but it's a "future problem" that you need to take care of.

As far as I can see, your code does what you describe it as "should be doing":
arr = 4 
arr = 5 
arr = 6 
arr = 7 
arr = 1 
arr = 3 
min = 1     max =7 
after replace = 4 
after replace = 5 
after replace = 6 
after replace = 7 
after replace = 7 
after replace = 3


Quote:

我正在尝试更换最大值的数组中的最小值,我的输出有问题。

I'm trying to replace the min value in an array in the max value, I have an issue with the output.



这不是你的代码正在做什么。

你的代码是在搜索最小值和最大值时替换最小值。

您的代码需要搜索最小值和最大值,当它们已知时,请替换最小值。

示例输入:{4 ,5,6,7,1,3,1}来处理多个最小值。



建议:学习如何使用调试器。

你的代码没有按照你期望的方式运行,你不明白为什么!



有一个几乎通用的解决方案:在调试器上一步一步运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么以及您的代码问是与它应该做的比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


This is not what your code is doing.
Your code is replacing minimums while you are searching minimum and maximum.
Your code need to search minimum and maximum, and when they are known, replace the minimum.
Example input: { 4, 5, 6, 7, 1, 3, 1 } to handle multiple minimums.

Advice: Learn how to use the debugger.
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于替换数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆