C-对从文件读取的数组中的值进行排序

C-对从文件读取的数组中的值进行排序,c,arrays,function,C,Arrays,Function,因此,我的程序应该从一个文件中读入值,将它们设置到一个数组中,然后找到每个值被找到的次数以及最频繁找到的值。第二个函数(称为createVArray)是我需要读入值并存储在数组中的地方。我试图实现一种在这里发现的技术:这就是为什么有些变量还没有被使用的原因。我在打印数组以查看循环是否正常工作时也遇到了问题。谢谢,这是我目前的代码: #include <stdlib.h> #include <stdio.h> #define MAX 100 int main(){ i

因此,我的程序应该从一个文件中读入值,将它们设置到一个数组中,然后找到每个值被找到的次数以及最频繁找到的值。第二个函数(称为createVArray)是我需要读入值并存储在数组中的地方。我试图实现一种在这里发现的技术:这就是为什么有些变量还没有被使用的原因。我在打印数组以查看循环是否正常工作时也遇到了问题。谢谢,这是我目前的代码:

#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(){

int votes[100]={0},i,num[100],count[100];

//reading in file
FILE *input, *output;
input = fopen("votes.txt","r");

//error checking that file opened correctly
if (input == NULL){
    fprintf(stderr, "Can't open input file!\n");
    exit(1);
}


system("pause");
return 0;
}

void createVArray(FILE *input, int votes[]){

  int i,j,k,temp;

    //storing values into array
  while(fscanf !=30){
    for(i=0;i<=MAX;i++){
      fscanf(input,"%d",&votes[i]);
        printf("%d\n",votes[i]);

    }
  }//bubble sorting array
  for(j=1;j<MAX;j++){
    for(k=0;k<MAX -1; k++){
        if(votes[k]>votes[k+1]){
            temp=votes[k];
            votes[k]=votes[k+1];
            votes[k+1]=temp;
          }

      }
  }
//  for(i=0;i<MAX;i++){
//  count[i] = -1;
//  }
//  for(i=0;i<MAX;i++){
//  num[i]= -999
//  }
// in the middle of adding in technique to sort
}


int getMaxVote(int votes[]){


}

void printSummary(int votes[]){
    printf("Choice        Num Votes\n");
    printf("-----------------------\n");

    printf("-----------------------\n");


}
#包括
#包括
#定义最大值100
int main(){
int投票[100]={0},i,num[100],count[100];
//在文件中读取
文件*输入,*输出;
输入=fopen(“voates.txt”,“r”);
//检查文件是否正确打开时出错
如果(输入==NULL){
fprintf(stderr,“无法打开输入文件!\n”);
出口(1);
}
系统(“暂停”);
返回0;
}
void createVArray(文件*输入,整数票数[]){
内部i、j、k、温度;
//将值存储到数组中
而(fscanf!=30){

对于(i=0;iit不清楚您的要求是什么。我对没有正确保存到数组中的值有问题,并且对这些值进行排序以便能够打印出它们出现的次数也有问题。例如:“14出现5次”@JohnField
fscanf!=30
:这有什么意义吗?也
用于(i=0;i
(i=0;i您可以先为子函数添加缺少的原型,这样编译器就不会猜测参数和返回值。如果在编译时启用所有警告,则编译器可以在代码完全兼容时,在尝试执行代码之前告诉您需要修复的问题s、 然后我们可以真正帮助您进行调试
    int main(){

int votes[100]={0},i,num[100],count[100];

//reading in file
FILE 
*input, *output;
    input = fopen("votes.txt","r");

    //error checking that file opened correctly
    if (input == NULL){
        fprintf(stderr, "Can't open input file!\n");
        exit(1);
    }
     int j,k,temp,ncount;

    //storing values into array
  while(fscanf !=30){
    for(i=0;i<=100;i++){
      fscanf(input,"%d",&votes[i]);
        printf("%d\n",votes[i]);

    }
  }//bubble sorting array
  for(j=1;j<100;j++){
    for(k=0;k<100 -1; k++){
        if(votes[k]>votes[k+1]){
            temp=votes[k];
            votes[k]=votes[k+1];
            votes[k+1]=temp;
          }

      }
  }
  for(i=0;i<100;i++){
    count[i] = -1;
    }
  for(i=0;i<100;i++){
    num[i]= -999;
    }
  j=0;
  num[j]=votes[0];
  count[j]=1;
  ncount=1;
  i=0;
  for(i=0;i<100-1;i++){
    if(votes[i+1]!=votes[i]){
        j++;
        num[j]=votes[i+1];
        ncount=1;
        count[j]=ncount;
      }
    else{
        count[j]=ncount++;
    }
  }
 for(i=0;i<j;i++){
    printf("number %d occurs %d times\n",num[i],count[i]);
 }
 getchar();

system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
//defining constant
#define size 100
//prototypes of functions
int createVArray(FILE *input,int votes[],int count[],int counter);
int getMaxVote(int count[]);
void printSummary(int count[],int counter,int high);
int main(){
    //intializing variables
int votes[100]={0},i=0,count[21]={0}, j,k,n,length, high=0,counter=0;
//reading in file
FILE *input;
    input = fopen("votes.txt","r");

    //error checking that file opened correctly
    if (input == NULL){
        fprintf(stderr, "Can't open input file!\n");
        exit(1);
    }
    //setting all values in count array to 0
    for(k=0;k<22;k++){
        count[k]=0;
    }
    //calling in functions
    counter = createVArray(input,votes,count,counter);
    high = getMaxVote(count);
    printSummary(count,counter,high);


system("pause");
return 0;
}
int createVArray(FILE *input,int votes[],int count[],int counter){
     int i=0;
     while(votes[i] != 30){
    //scanning in values from file to array
    fscanf(input,"%d",&votes[i]);
        //switch statement to keep count of frequency of values
        switch(votes[i]){
            case 1:
                count[1]++;
                break;
            case 2:
                count[2]++;
                break;
            case 3:
                count[3]++;
                break;
            case 4:
                count[4]++;
                break;
            case 5:
                count[5]++;
                break;
            case 6:
                count[6]++;
                break;
            case 7:
                count[7]++;
                break;
            case 8:
                count[8]++;
                break;
            case 9:
                count[9]++;
                break;
            case 10:
                count[10]++;
                break;
            case 11:
                count[11]++;
                break;
            case 12:
                count[12]++;
                break;
            case 13:
                count[13]++;
                break;
            case 14:
                count[14]++;
                break;
            case 15:
                count[15]++;
                break;
            case 16:
                count[16]++;
                break;
            case 17:
                count[17]++;
                break;
            case 18:
                count[18]++;
                break;
            case 19:
                count[19]++;
                break;
            case 20:
                count[20]++;
                break;
        }
        //counting total number of values
        counter++;
    }
    return counter;
} 
int getMaxVote(int count[]){
    int high,i=0;
    high = 1;
    //calculating most frequently voted for value
for (i=2; i<=20; i++){
    if (count[i] > count[high])
        high = i;
    }
    return high;
}
void printSummary(int count[],int counter,int high){
 int k=0;
 //printing out top 2 lines
 printf("Choice          Num Votes\n");
 printf("-------------------------\n");
 //for loop to print out individual values and 
 //frequency of said values
 for(k=1;k<21;k++){
    printf("%2d%20d\n",k,count[k]);
 }
 //continue printing last lines
 printf("-------------------------\n");
 printf("Total Votes         %d  \n",counter);
 printf("winning option     %d\n",high);
 printf("-------------------------\n");
}


        }