64,646
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include<iostream>
#include <string.h>
using namespace std;
int main() {
FILE * pFile;
char mystring[100];
int in,tmp;
static int myin[3][20000];
int i=0;
int count;
pFile = fopen ("test.csv" , "r");
/* read the character array into integer array */
if (pFile == NULL) perror ("Error opening file");
else {
while
( fgets (mystring,100, pFile) !=NULL) {
sscanf(mystring,"%d,%d,%d",&myin[0][i],&myin[1][i],&myin[2][i+2]);
i++;
/* Output character array */
/* puts (mystring);*/
}
count=i;
/* Output integer array*/
for(int j=0;j<count;j++)
cout<<myin[0][j]<<","myin[1][j]<<","myin[2][j]<<","<<endl;
fclose (pFile);
cout<<"The total count is:"<< count<<endl;
}
/* sort the array */
int k,m;
for(int k=0;k<count-1;k++) {
for(int m=k+1;m<count;m++) {
if(myin[0][k]<myin[0][m]) {
tmp=myin[0][k];myin[0][k]=myin[0][m];myin[0][m]=tmp;
}
}
}
for(int k=0;k<count-1;k++) {
for(int m=k+1;m<count;m++) {
if(myin[1][k]<myin[1][m]) {
tmp=myin[1][k];myin[1][k]=myin[1][m];myin[1][m]=tmp;
}
}
}
for(int k=0;k<count-1;k++) {
for(int m=k+1;m<count;m++) {
if(myin[2][k]<myin[2][m]) {
tmp=myin[2][k];myin[2][k]=myin[2][m];myin[2][m]=tmp;
}
}
}
/* Output integer array
for(int j=0;j<=count-1;j++)
cout<<myin[j]<<endl;
*/
/* calculate the average of top 50 */
float Avg[3];
float Sum[3];
for (int n=0;n<3;n++) {
Sum[n]=0.0f;
for(int i=0;i<50;i++)
Sum[n]+=(float)myin[n][i];
Avg[n]=Sum[n]/50.0f;
cout<<"The average "<<n+1<<" of top 50 is:"<<Avg[n]<<endl;
}
system("pause");
return 0;
}
#include <stdio.h>
#include<iostream>
#include <string.h>
using namespace std;
int main() {
FILE * pFile;
char mystring[100];
int in;
static int myin[20000];
int i=0;
int count;
pFile = fopen ("test.csv" , "r");
/* read the character array into integer array */
if (pFile == NULL) perror ("Error opening file");
else {
while
( fgets (mystring,100, pFile) !=NULL) {
sscanf(mystring,"%d,%d,%d",&myin[i],&myin[i+1],&myin[i+2]);
i+=3;
/* Output character array */
/* puts (mystring);*/
}
count=i;
/* Output integer array*/
for(int j=0;j<count;j++)
cout<<myin[j]<<endl;
fclose (pFile);
cout<<"The total count is:"<< count<<endl;
}
/* sort the array */
int k,m;
for(int k=0;k<count-1;k++) {
for(int m=k+1;m<count;m++) {
if(myin[k]<myin[m]) {
myin[k]=myin[k]+myin[m];
myin[m]=myin[k]-myin[m];
myin[k]=myin[k]-myin[m];
}
}
}
/* Output integer array
for(int j=0;j<=count-1;j++)
cout<<myin[j]<<endl;
*/
/* calculate the average of top 50 */
float Avg;
float Sum=0.0f;
for(int i=0;i<50;i++)
Sum+=(float)myin[i];
Avg=Sum/50.0f;
cout<<"The average of top 50 is:"<<Avg<<endl;
system("pause");
return 0;
}