gpt4 book ai didi

c - 如何打印我已经在函数中排序但不能打印的数组 vector 排序需要一些建议

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:46 25 4
gpt4 key购买 nike

无法在对 vector 数组进行排序后打印我认为我的算法是正确的。我想要的是在排序之后打印 *CalVector 但是在我为数组下标编译无效类型 float[int] 之后它的错误有什么建议吗?

#include <stdio.h>
#include <stdlib.h>
float Vector[7],*CalVector[7];
void Menu (){
printf("################## MENU ##################\n");
printf("1. Find the maximum number in the vector\n");
printf("2. Find the minimum number in the vector\n");
printf("3. Find the total of numbers in the vector\n");
printf("4. Sort numbers in the vector in the ascending order\n");
printf("0. Quit Program\n");
}

这是我的排序 vector 函数

   float sortVector()  {
int i;
float Sort,SwapVector;
for(i=0;i<7;i++){
if(*CalVector[i-1]>*CalVector[i]){
SwapVector = *CalVector[i];
*CalVector[i] = *CalVector[i-1];
*CalVector[i] = SwapVector;
}
}
}
float findMin() {
int i;
float Min ;
for (i =0 ; i < 7 ; i++){
Min += *CalVector[i];
}
for (i = 0 ; i < 7 ;i++){
if(*CalVector[i] < Min)
Min = *CalVector[i];
}
return Min;
}
float findMax() {
int i;
float Max = 0 ;
for(i = 0 ; i < 7; i++){
if(*CalVector[i] > Max){
Max = *CalVector[i];
}
}
return Max;
}
float findSum() {
int i;
float Sum = 0;
for( i = 0 ; i < 7 ; i++){
Sum += *CalVector[i];
}
return Sum;
}
int main () {
int Count,Choice;
float MaxiumVector,Min,Sum,Sort ;
printf("Enter 7 numbers into the vector: ");
for(Count=0;Count<7;Count++){
scanf("%f",&Vector[Count]);
}
for (Count = 0; Count<7;Count++){
CalVector[Count]=&Vector[Count];
// printf("%f",*CalVector[Count]);
}
do{
Menu();
printf("Enter your choice <1, 2, 3, 4 or 0> : ");
scanf("%d",&Choice);
switch (Choice) {
case 1 : printf("%.2f",findMax()); break;
case 2 : printf("%.2f",findMin()); break;
case 3 : printf("%.2f",findSum()); break;

还有我要打印的案例4

    case 4  :  for(Count = 0; Count < 7; Count++){
Sort = sortVector();
printf("%.2f",Sort[Count]);
}
default : printf("Invalid Choice!!\n");
}
}while(Choice!=0);
printf("");
return 0 ;
}

任何关于更舒适的代码的建议我只是编码的初学者 :D

最佳答案

findMax 中名为Max 的变量隐藏了全局Max,因此在函数中设置Max 不会'不影响全局。你确实从函数返回了这个值,但是你没有对该返回值做任何事情:

case 1  : findMax(); 
printf("%f",Max);

您需要将返回值赋给Max:

case 1  : Max = findMax(); 
printf("%f",Max);

此外,您应该将 findMax 中的 Max 定义为 float,否则您将得到截断的值。

关于c - 如何打印我已经在函数中排序但不能打印的数组 vector 排序需要一些建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53361461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com