C 如何打印数组向量排序I Sort已在函数中,但可以';我需要一些建议
无法打印后对向量数组进行排序,我认为我的算法是正确的。我想要的是在排序之后打印*CalVector,但是在我编译数组下标的无效类型float[int]之后它会出错。有什么建议吗C 如何打印数组向量排序I Sort已在函数中,但可以';我需要一些建议,c,C,无法打印后对向量数组进行排序,我认为我的算法是正确的。我想要的是在排序之后打印*CalVector,但是在我编译数组下标的无效类型float[int]之后它会出错。有什么建议吗 #include <stdio.h> #include <stdlib.h> float Vector[7],*CalVector[7]; void Menu (){ printf("################## MENU ##################\n"); p
#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");
}
#包括
#包括
浮点向量[7],*CalVector[7];
无效菜单(){
印刷品;
printf(“1.查找向量中的最大数\n”);
printf(“2.查找向量中的最小数\n”);
printf(“3.查找向量中的数字总数\n”);
printf(“4.按升序对向量中的数字进行排序\n”);
printf(“0.退出程序\n”);
}
这是我的sortVector函数
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;
float-sortVector(){
int i;
浮动排序,SwapVector;
对于(i=0;i*CalVector[i]){
SwapVector=*CalVector[i];
*CalVector[i]=*CalVector[i-1];
*CalVector[i]=SwapVector;
}
}
}
浮点findMin(){
int i;
浮动最小值;
对于(i=0;i<7;i++){
Min+=*CalVector[i];
}
对于(i=0;i<7;i++){
if(*CalVector[i]最大值){
Max=*CalVector[i];
}
}
返回最大值;
}
浮点findSum(){
int i;
浮点数和=0;
对于(i=0;i<7;i++){
总和+=*卡尔维克特[i];
}
回报金额;
}
int main(){
整数计数,选择;
float MaxiumVector、Min、Sum、Sort;
printf(“在向量中输入7个数字:”);
对于(Count=0;Count,findMax
中名为Max
的变量隐藏全局Max
,因此在函数中设置Max
不会影响全局。您可以从函数中返回此值,但不使用该返回值执行任何操作:
case 1 : findMax();
printf("%f",Max);
您需要将返回值指定给Max
:
case 1 : Max = findMax();
printf("%f",Max);
另外,您应该将findMax
中的Max
定义为float
,否则会得到截断值。在findMax
中调用的Max
变量隐藏全局Max
,因此在函数中设置Max
不会影响全局值。您确实会从函数返回此值,但是,您不使用该返回值执行任何操作:
case 1 : findMax();
printf("%f",Max);
您需要将返回值指定给Max
:
case 1 : Max = findMax();
printf("%f",Max);
另外,您应该将findMax
中的Max
定义为float
,否则会得到截断值。一般来说,我建议您不要使用全局变量,但如果您想这样做,您可以删除findMax
中的Max
声明。该声明隐藏了全局变量
另一件事(你的编译器应该提醒你)是在findMax
中你声明Max
为int,但它应该是一个float。如果你删除声明,这个问题就会消失,但我想我应该指出它
还有一件事是,如果您使用globals(我再次建议您不要这样做),则根本不需要返回任何值
void findMax() {
int i;
Max = *CalVector[0];
for (i = 0; i < 7; i++) {
if (*CalVector[i] > Max) {
printf("Max %f Calvector %f", Max, *CalVector[i]);
Max = *CalVector[i];
}
}
}
void findMax(){
int i;
Max=*CalVector[0];
对于(i=0;i<7;i++){
如果(*CalVector[i]>最大值){
printf(“最大%f Calvector%f”,最大,*Calvector[i]);
Max=*CalVector[i];
}
}
}
一般来说,我建议您不要使用globals,但如果您想这样做,可以删除findMax
中的Max
声明。该声明隐藏了全局变量
另一件事(你的编译器应该提醒你)是在findMax
中你声明Max
为int,但它应该是一个float。如果你删除声明,这个问题就会消失,但我想我应该指出它
还有一件事是,如果您使用globals(我再次建议您不要这样做),则根本不需要返回任何值
void findMax() {
int i;
Max = *CalVector[0];
for (i = 0; i < 7; i++) {
if (*CalVector[i] > Max) {
printf("Max %f Calvector %f", Max, *CalVector[i]);
Max = *CalVector[i];
}
}
}
void findMax(){
int i;
Max=*CalVector[0];
对于(i=0;i<7;i++){
如果(*CalVector[i]>最大值){
printf(“最大%f Calvector%f”,最大,*Calvector[i]);
Max=*CalVector[i];
}
}
}
从注释1和注释2中,我得到这样的结果:我尝试从Max声明全局值,现在我在Main函数中创建Max,但它无法编译,因为他们不知道Main如何修复它
*下午,我通过将findMax()
设为浮点并将值Max
返回到main来修复此问题
并使用与答案2类似的赋值Max=findMax()
#include <stdio.h>
#include <stdlib.h>
#include <math.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 thevector in the ascending order\n");
printf("0. Quit Program\n");
}
float findMax() {
int i;
float Max = 0 ;
Max = *CalVector[0];
for(i = 0 ; i < 7; i++){
if(*CalVector[i] > Max){
Max = *CalVector[i];
}
}
}
int main () {
int Count,Choice;
float Max = 0 ;
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]);
}
Menu();
printf("Enter your choice <1, 2, 3, 4 or 0> : ");
scanf("%d",&Choice);
switch (Choice) {
case 1 : Max = findMax();
printf("%f",Max);
default : ;
}
return 0 ;
}
#包括
#包括
#包括
浮点向量[7],*CalVector[7];
无效菜单(){
印刷品;
printf(“1.查找向量中的最大数\n”);
printf(“2.查找向量中的最小数\n”);
printf(“3.查找向量中的数字总数\n”);
printf(“4.按升序对向量中的数字进行排序\n”);
printf(“0.退出程序\n”);
}
浮点findMax(){
int i;
浮动最大值=0;
Max=*CalVector[0];
对于(i=0;i<7;i++){
如果(*CalVector[i]>最大值){
Max=*CalVector[i];
}
}
}
int main(){
整数计数,选择;
浮动最大值=0;
printf(“在向量中输入7个数字:”);
对于(Count=0;Count注释1和注释2,我得到这样的结果,我尝试从Max声明全局值,现在我在Main函数中创建Max,它无法编译,因为他们不知道Main如何修复它
*下午,我通过将findMax()
设为浮点并将值Max
返回到main来修复此问题
并使用与答案2类似的赋值Max=findMax()
#include <stdio.h>
#include <stdlib.h>
#include <math.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 thevector in the ascending order\n");
printf("0. Quit Program\n");
}
float findMax() {
int i;
float Max = 0 ;
Max = *CalVector[0];
for(i = 0 ; i < 7; i++){
if(*CalVector[i] > Max){
Max = *CalVector[i];
}
}
}
int main () {
int Count,Choice;
float Max = 0 ;
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]);
}
Menu();
printf("Enter your choice <1, 2, 3, 4 or 0> : ");
scanf("%d",&Choice);
switch (Choice) {
case 1 : Max = findMax();
printf("%f",Max);
default : ;
}
return 0 ;
}
#包括
#包括
#包括
浮点向量[7],*CalVector[7];
无效菜单(){
印刷品;
printf(“1.查找向量中的最大数\n”);
printf(“2.查找向量中的最小数\n”);
printf(“3.找到托特