How to print a Sort of Array Vector I sort already in function but can't printf need some suggestion

Can't print after to sort array of vector I think my algorithm is correctly. the things I want is to print *CalVector after sort already but its error after I compile invalid types float[int] for array subscript any suggest pls ?

#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");
}

this is my sortVector function

   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;

and case 4 that i want to print

    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 ;
}

any suggestion for another code for more comfortable i'm just beginner of coding :D

Upvotes: 0

Views: 56

Answers (3)

From Comment 1 and 2 I get like this i try to declaration global value from Max and now i create Max in Main function and its can't compile because they don't know main how I can fix this ?

*P.M. I fix this by make findMax() as float and return value Maxto main and used assign like answer 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 ;
}

Upvotes: 0

klutt
klutt

Reputation: 31389

In general, I would advice you not to use globals, but if you want to do that, you could just remove the declaration of Max in findMax. That declaration hides the global variable.

Another thing (that your compiler should have warned you about) is that inside findMax you declared Max as an int, but it should be a float. That problem disappears if you remove the declaration, but I think I should point it out.

One more thing is that if you're using globals (which I, again, advice you not to do) there's no real need to return any value at all. You can write findMax like this:

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];
    }
  }
}

Upvotes: 1

dbush
dbush

Reputation: 223972

The variable called Max in findMax hides the global Max, so setting Max in the function doesn't affect the global. You do return this value from the function, however you don't do anything with that return value:

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

You need to assign the return value to Max:

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

Also, you should define Max in findMax as a float, otherwise you'll get truncated values.

Upvotes: 1

Related Questions