-

2011年8月2日星期二

Programming Help in C?

-Hi So Im having trouble writing a program using one-d arrays and functions to calculate and display test score averages, highest, and ascending sorted order.



There are certain stuff that i understand what my professor assigned but some of the stuff he assignment I am confused on.



I have:



#define NUM_SCORE 5

#define MAX_SCORE 102



void getScores (int a[ ]);

double average (int a[ ]);

void bubbleSort (int a[ ]);

void swap (int *p, int *q);



main(void);

{



printf("Want to calculate midterm scores?");



prinft("Enter midterm scores (0-102) ");



}



I know I will have to put a loop in, but I totally dont understand how to compute this. Any help??

Thanks!include <stdio.h>

#define NUM_SCORE 5

#define MAX_SCORE 102



void getScores (int a[ ]);

double average (int a[ ]);

void bubbleSort (int a[ ]);

void swap (int *p, int *q);



int main(){

int scores[NUM_SCORE] = { 0 };



getScores(scores);

printf("%.2f", average(scores));



return 0;

}



void getScores(int a[]){

int index = 0;

int individual = 0;



do{

do{

printf("Enter midterm scores (0-102) ");

scanf("%d", &individual);

if(individual < 0 || individual > MAX_SCORE)

printf("Error -- invalid score")

}while(individual < 0 || individual > MAX_SCORE);

scores[index] = individual;

index++;



if(index != NUM_SCORE){

printf("Want to calculate midterm scores (-1 to quit)?");

scanf("%d",&individual);

}

}while(individual != -1 || index != NUM_SCORE);



return;

}



double average(int a[]){

double mean;

int sum;

int i;

for(i = 0; i < NUM_SCORE; i++){

sum += a[i];

}

mean = sum / NUM_SCORE;

return mean;

}



double bubbleSort(int a[]){

int i,j;



for(i = 0; i < NUM_SCORE - 1; i++){

for(j = 0; j < NUM_SCORE - 1; j++){

if(a[j] > a[j + 1]){

int temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

}

return;

}
write a function to calculate each average and store the results in an array. Then take this array run a bubblesort on this array to sort the averages in ascending order.

没有评论:

发表评论