Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- One Dimensional Arrays
- Project: STUDENT SCORES - Sort Parallel Arrays Using Insertion Sort
- // Hongkai Chang
- // What IDE did you use? both Visual Studio 2012 and code block
- */
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- void insertionSort(int arrayA[], int arrayB[], int size);
- int Scores(int id[] , int avg[]);
- // NOTE: main() contains code to demonstrate how the insertion sort works.
- // Please delete this code before submission.
- // DOCUMENTATION
- // 1. Write a beginning comment // see above
- // 2. Write documentation for each function // see below, the definition of the Insertion Sort function
- // Choose your own style and be consistent!
- // Please delete this comment before submission
- int main()
- {
- int size = 50, tempS[size], tempA[size];
- int id[size], avgS[size], cavg = 0;
- Scores(tempS, tempA);
- for (int i = 0; i < 39 ; i++)
- {
- id[i] = tempS[i];
- avgS[i] = tempA[i];
- }
- insertionSort(avgS, id, size);
- for (int i = 0; i < size; i++)
- {
- cout<< id[i] << " " << avgS[i] << endl;
- }
- return 0;
- }
- //**************************************************************
- // Definition of function insertionSort.
- // This function performs an ascending order insertion sort on
- // array. size is the number of elements in the array.
- //**************************************************************
- void insertionSort(int arrayA[], int arrayB[], int size)
- {
- for (int curr = 1; curr < size; curr++)
- {
- int holdscore = arrayA[curr];// copy current element to hold
- int holdid = arrayB[curr];
- int back = curr - 1;
- while (back >= 0 && holdscore > arrayA[back]) // search where to insert the current element
- {
- arrayA[back + 1] = arrayA[back];// shift to the right
- arrayB[back + 1] = arrayB[back];
- back--;
- }
- arrayA[back + 1] = holdscore;
- arrayB[back + 1] = holdid; // put hold back to the array
- }
- }
- //*************************************************************
- //This is the score function.
- //This function does both read score from the file and
- //calculate the average of the score.
- //************************************************************
- int Scores(int id[], int avg[])
- {
- ifstream inFile;
- inFile.open ("scores.txt");
- int sid, score[8], i = 0;
- inFile >> sid >> score[0] >> score [1] >> score[2] >> score[3] >> score[4] >> score[5] >> score[6] >> score[7];
- while (!inFile.eof())
- {
- id[i] = sid;
- avg[i] = (score[0]+ score[1] + score[2] + score[3]+ score[4] + score [5] + score [6] + score [7])/8;
- inFile >> sid >> score[0] >> score [1] >> score[2] >> score[3] >> score[4] >> score[5] >> score[6] >> score[7];
- cout <<id[i] << " " << avg[i] << endl;
- i++;
- }
- inFile.close();
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement