打印阵列的最大元素
因此,这个标题很垃圾,我真的不知道该如何解释它。当您看到代码时,您会得到它,这就是这样:
#include <stdio.h>
void products(float values[], int prod_num);
int main(void) {
int prod_num;
printf("Enter the number of products: ");
scanf("%d", &prod_num);
float values[prod_num];
for (int i = 0; i < prod_num; i++) {
printf("Enter the value of the product: ");
scanf("%f", &values[i]);
}
products(values, prod_num);
return 0;
}
void products(float values[], int prod_num) {
int i, j, t;
float sum = 0, avg;
for (i = 0; i < prod_num; i++) {
sum = sum + values[i];
avg = sum / prod_num;
}
printf("The average is: %f\n", avg);
for (i = 0; i < prod_num; i++) {
for (j = i + 1; j < prod_num; j++) {
if (values[i] > values[j]) {
t = values[i];
values[i] = values[j];
values[j] = t;
}
}
}
printf("Largest value: %f\n", values[]);
printf("Smallest value: %f", values[0]);
}
因此,您可以说的是使用函数products
找到某些产品的平均值,而某些产品的平均值可以从value
数组中找到最大的值和最小的值。我认为最好的方法是按上升顺序排列数组,然后打印values [0]
,因为那将是最小的值,然后打印最后一个元素。那是我的问题。当我不知道它的大小时,我该如何告诉它要打印出数组的最后一个元素,因为它是用户输入的?实际上,我有一种不同的方法来完成此部分(我只是显示功能):
void products(float values[], int prod_num) {
int i;
float sum = 0, avg;
for (i = 0; i < prod_num; i++) {
sum = sum + values[i];
avg= sum / prod_num;
}
printf("The average is: %f\n", avg);
for (i = 1; i < prod_num; i++) {
if (values[0] < values[i]) {
values[0] = values[i];
}
}
printf("Largest value: %f\n", values[0]);
for (i = 1; i < prod_num; i++) {
if (values[0] > values[i]) {
values[0] = values[i];
}
}
printf("Smallest value: %f\n", values[0]);
}
问题是它将打印出第二小的值,因为该值将会改变最大的价值,因此我尝试了我展示的第一种方法。我不介意该解决方案可以适用于这两种方法中的任何一种。或者我想这可能是一种完全不同的方法,它不是完全垃圾,就像我怀疑我的两个方法一样。
So this title is pretty garbage, I just really don't know how else to explain it. You'll get it when you see the code though, which is this:
#include <stdio.h>
void products(float values[], int prod_num);
int main(void) {
int prod_num;
printf("Enter the number of products: ");
scanf("%d", &prod_num);
float values[prod_num];
for (int i = 0; i < prod_num; i++) {
printf("Enter the value of the product: ");
scanf("%f", &values[i]);
}
products(values, prod_num);
return 0;
}
void products(float values[], int prod_num) {
int i, j, t;
float sum = 0, avg;
for (i = 0; i < prod_num; i++) {
sum = sum + values[i];
avg = sum / prod_num;
}
printf("The average is: %f\n", avg);
for (i = 0; i < prod_num; i++) {
for (j = i + 1; j < prod_num; j++) {
if (values[i] > values[j]) {
t = values[i];
values[i] = values[j];
values[j] = t;
}
}
}
printf("Largest value: %f\n", values[]);
printf("Smallest value: %f", values[0]);
}
So this code, as you can tell, is using the function products
to find the average of the values of some products, which works fine, and also find the largest value and the smallest, from the values
array. I thought the best way to do this is to arrange the array in ascending order and to then print values[0]
, as that would be the smallest value, and then print the very last element. That's my problem. How do I tell it to print out the last element of the array, when I don't know its size, since it's entered by the user? I actually had a different way of doing this part which was this (I'm showing just the function):
void products(float values[], int prod_num) {
int i;
float sum = 0, avg;
for (i = 0; i < prod_num; i++) {
sum = sum + values[i];
avg= sum / prod_num;
}
printf("The average is: %f\n", avg);
for (i = 1; i < prod_num; i++) {
if (values[0] < values[i]) {
values[0] = values[i];
}
}
printf("Largest value: %f\n", values[0]);
for (i = 1; i < prod_num; i++) {
if (values[0] > values[i]) {
values[0] = values[i];
}
}
printf("Smallest value: %f\n", values[0]);
}
The problem is that it would print out the second smallest value, since the values would have changed because of the for loop tasked with finding the largest value, so then I tried the first method that I showed. The solution can be for either one of these two methods, I don't mind. Or I guess it could be a completely different method that's not complete garbage, like how I'm suspecting my two could be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于您的产品中的最小值和最大值,您不需要对数组进行排序(尽管您也会找到正确的解决方案),因为它会在运行时影响程序的性能。
无论如何,如果您仍然想分类,
使用值[prod_num-1] 查找最大元素。
对于查找最小值和最大 ,而无需对您的数组进行排序 ,您可以使用此示例:
For find the min and max in your products you dont need to sort the array(Although even so you will find the right solution) because it will affect the performance of your program in terms of runtime.
Anyway, if you still want to sort ,
use values[prod_num-1] for find the max element .
for find the min and max without sorting your array you can use this example:
您 do 知道它的大小时,我如何才能打印出数组的最后一个元素,实际上它已传递到功能中。
数组的大小为
prod_num
,因此最后一个元素是values [prod_num-1]
。在侧面节点上,不必对列表进行排序以找到最大和最小的列表。使用变量跟踪当前最小和最大的值,将两者都初始化为数组的第一个元素。然后通过数组迭代,检查当前元素是否小于最小值还是大于最大值,以及是否更新最小/最大值的值。
You do know its size, in fact it was passed into the function.
The size of the array is
prod_num
, so the last element isvalues[prod_num-1]
.On a side node, it's not necessary to sort the list to find the largest and the smallest. Use variables to keep track of the current smallest and largest values, initializing both to the first element of the array. Then iterate through the array, checking if the current element is either smaller than the min or larger than the max, and if update the value of the min/max.
您知道阵列的大小。它被指定为第二个函数参数
,因此您
也可以编写,无需计算for循环中的平均值。
您可以在for循环之后仅计算一次。
通常,当更改源数组时,这是一个坏主意。
要找到最小和最大元素,您可以写
You know the size of the array. It is specified as the second function parameter
So you can write
Also there is no need to calculate the average in the for loop.
You could calculate it only one time after the for loop.
In general it is a bad idea when the source array is changed.
To find the minimum and the maximum elements you could write