English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

given there are 5 scores for test1 until test 5.how can i sort them and how to find the highest,lowest and average scores??

2007-03-27 16:03:24 · 6 answers · asked by n f 1 in Computers & Internet Programming & Design

6 answers

I am going to show you how to use an algorithm called selection sort. It's not the most efficient sorting algorithm out there but it's easy to understand.

// The following function sorts the array
void selectionSort(double array[], int arraySize);

// The following function finds the lowest value in the array
// starting at startIndex, and ending at the last element of the
// array
int findLowest(double array[], int startIndex, int arraySize);

// The following function swaps two values in the array given
// their location in the array.
void swap(double array[], int firstIndex, int secondIndex);

int main()
{
const int ARRAY_SIZE = 5;
double array[ARRAY_SIZE];
double lowest = 0;
double highest = 0;
double sum = 0.0;
double avgerage = 0.0;

// Here you have to initialize the array by assigning
// values to it

selectionSort(array, ARRAY_SIZE);

// Note that after sorting the array, the lowest score will be in
// the first block of the array and the highest score will be in
// the last block

lowest = array[0];
highest = array[ARRAY_SIZE - 1];

// Here is how to find the average
for(int i = 0; i < ARRAY_SIZE; i++)
{
sum += array[i];
}
average = sum / (double) ARRAY_SIZE;

return 0;
}

void selectionSort(double array[], int arraySize)
{
int smallestIndex = 0;

for(int i = 0; i < arraySize - 1; i++)
{
smallestIndex = findLowest(array, i, arraySize);
swap(array, i, smallestIndex);
}
}

int findLowest(double array[], int startIndex, int arraySize)
{
int smallestIndex = startIndex;

for(int i = startIndex + 1; i < arraySize; i++)
{
if(array[i] < array[smallestIndex ])
smallestIndex = i;
}
return smallestIndex;
}

void swap(double array[], int firstIndex, int secondIndex)
{
double temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
}


// Notice that I have sorted the array from lowest to highest
// (in ascending order). If you want it in descending order
// please let me know and I will fix that for you.

// Also, I coded in C++. If you want the code in any other
// language please let me know.

2007-03-27 18:05:49 · answer #1 · answered by Silver_Sword 3 · 0 0

A bubble sort is not the most efficient way to sort a list, but it is the simplest.

You compare test1 to test2, if test1 is lower than test2, then you swap them. Next you compare test2 and test3. You keep swapping the lower test scores down until you finally compared test4 and test5.

Finally, you repeat that test again (starting at test1 and test2) and swap the values over every time a lower score is found in the higher position.

Keep doing that until you do not have to swap any more scores. Then just display the list and it should be in order.

2007-03-27 16:18:51 · answer #2 · answered by SteveN 7 · 0 0

Any type of sorting algorithm is good for you. But, bubble sort is easy.

Bubble sort

The bubble sort is a selection sort that uses a different scheme for finding the minimum (or maximum) value. Each iteration puts the smallest unsorted element into its correct place, but it also changes the locations of the other elements in the array. The first iteration puts the smallest element in the array into the first array position. Starting with the last array element we compare successive pairs of elements, swapping them whenever the bottom element of the pair is smaller than the one above it. In this way, the smallest element "bubbles up" to the top of the array. The next iteration puts the smallest element in the unsorted part of the array into the second array position, using the same technique.

The basic algorithm for the bubble sort follows:

Set current to the index of first item in the array
while more items in unsorted part of array
"Bubble up" the smallest item in the unsorted part,
causing intermediate swaps as needed
Shrink the unsorted part of the array by incrementing current

BubbleUp(values, startlndex, endlndex)

for index going from endlndex DOWNTO startlndex + 1
if values[index] < values[index - 1]
Swap(values[index], values[index - 1])

And regarding the average scores i think u dont have to sum up all the values and divide them by 5 thats the only way

2007-03-27 16:26:52 · answer #3 · answered by keerthi 2 · 0 0

Hi. The technique is called "bubble sort' or "sink sort" depending on which direction you go. If you do a search you'll find a better answer than this, but the concept is: if (x) > (y) then (y)=(x) - bubble sort. Make sense?

2007-03-27 16:09:15 · answer #4 · answered by Cirric 7 · 0 0

There so many ways to find largest and smallest number.
By sorting these numbers ascending and print the first number for lowest and print the last number for highest.
Otherwise store the previous number and compare with present number
example for highest.
large=0
for(i=0;i if(large < mark[i])
large=mark[i];

example for lowest.
small=0
for(i=0;i if(small > mark[i])
small=mark[i];
printf("Large: %d\nSmall: %d",large,small);

2007-03-27 16:29:19 · answer #5 · answered by sridhar b 2 · 0 0

Assume you are writting the program to sort them out. If so, look at bubble sort and insertion sort.

2007-03-27 16:12:01 · answer #6 · answered by jetkidz 3 · 0 0

fedest.com, questions and answers