Tags:
create new tag
view all tags

Hands On Arrays 2

Goal

Sorting Arrays and 2 dimensional arrays Arrays with Function calls

Step 1

1. Create a function to enter a list of up to 25 quiz scores in array. A negative number indicates the end.
2. Create a function to print the grade list back out.
3. Write a main program to do the two functions above

Step 2

1. Create a function to remove the low score of an array
2. Enter the scores
3. remove the two lowest grades.
4. Print the reduced list back out.

Step 3

1. Create a function "int lowest(int a[], int start, int size)" to scan from the ith position in an array to the end of the array, returning the index the lowest entry in the range.
2. Create a function "void sort(int a[], int size)" to use the lowest function to scan through each element in the array, swapping the ith lowest value into the ith position.
3. Use the routines from earlier to test the sort function

```#include <iostream>
using namespace std;

int getScores(int scores[],int size);
void printScores(const int scores[],int size);
int lowest(const int a[], int start, int size);
void sort(int a[],int size);

int main() {
const int MAX=10;
int s[MAX];
int numScores;
numScores=getScores(s,MAX);
printScores(s,numScores);
sort(s,numScores);
printScores(s,numScores);
return 0;
}

int getScores(int scores[],int size) {
int index=0;
cout << "Input up to " << size << " scores:";
int s;
do {
cin >> s;
if (s>=0) {
scores[index]=s;
index++;
}
} while (index<size && s>=0);
return index;
}
void printScores(const int scores[],int size){
for (int i=0;i<size;i++){
cout << scores[i] << " ";
}
cout << endl;
}

int lowest(const int a[], int start, int size) {
int lowIndex=start;
for(int i=start+1;i<size;i++) {
if (a[i]<a[lowIndex]){
lowIndex=i;
}
}
return lowIndex;
}

void sort(int a[],int size){
for(int i=0; i<size-1;i++){
int low=lowest(a,i,size);
int temp=a[i];
a[i]=a[low];
a[low]=temp;
}
}
```
Topic revision: r5 - 2017-10-03 - JimSkon

 Home SCMP118 Web P View Edit Account
Copyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback