|
1鱼币
#include "stdafx.h"
#include "stdio.h"
void sorting(int *, float *, int);
float average(float *, int);
float *store_score(int);
int *store_number(int);
int _tmain(int argc, _TCHAR* argv[])
{
int * intNo, i = 0, intCount;
float * fltScore;
printf("Enter the count of students: ");
scanf("%d", &intCount);
intNo = store_number(intCount);
fltScore = store_score(intCount);
sorting(intNo, fltScore, intCount);
printf("The result of sorting is:\n");
for (i = 0; i < intCount; i++)
printf("%2d: %0.0f\n", intNo[i], fltScore[i]);
printf("The average score is %0.2f.\n", average(fltScore, intCount));
scanf("%f", &fltScore[0]);
return 0;
}
void sorting(int * ip, float * fp, int count)
{
float f, * fsp;
bool state = false;
int i, j, n, *isp;
fp = fp + count - 1;
ip = ip + count - 1;
for (i = 1; i < count; i++)
{
if (state) break;
state = true;
isp = ip; fsp = fp;
for (j = count; j > i; j--)
{
if (* fsp > * (fsp - 1))
{
f = * fsp; n = * isp;
* fsp = * (fsp - 1); * isp = * (isp - 1);
* (fsp - 1) = f; * (isp - 1) = n;
state = false;
}
fsp--; isp--;
}
}
}
float average(float * fp, int count)
{
int i;
float a = 0;
for (i = 0; i < count; i++)
a += *(fp++);
return a/count;
}
float *store_score(int n)
{
float *p = new float[n];
int i;
printf("Enter scores:\n");
for (i = 0; i < n; i++)
{
printf("%2d: ", i + 1);
scanf("%f", p + i);
}
return p;
}
int *store_number(int n)
{
int *p = new int[n], i;
for (i = 0; i < n; i++) * (p + i)= i + 1;
return p;
}
请问这2个程序有什么不一样??
|
|