Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. /**
  2. One Dimensional Arrays
  3. Project: STUDENT SCORES - Sort Parallel Arrays Using Insertion Sort
  4.  
  5. // Hongkai Chang
  6. // What IDE did you use? both Visual Studio 2012 and code block
  7. */
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <cstdlib>
  12.  
  13. using namespace std;
  14.  
  15. void insertionSort(int arrayA[], int arrayB[], int size);
  16. int Scores(int id[] , int avg[]);
  17. // NOTE: main() contains code to demonstrate how the insertion sort works.
  18. // Please delete this code before submission.
  19. // DOCUMENTATION
  20. // 1. Write a beginning comment // see above
  21. // 2. Write documentation for each function // see below, the definition of the Insertion Sort function
  22. // Choose your own style and be consistent!
  23. // Please delete this comment before submission
  24.  
  25. int main()
  26. {
  27.  
  28. int size = 50, tempS[size], tempA[size];
  29. int id[size], avgS[size], cavg = 0;
  30.  
  31. Scores(tempS, tempA);
  32. for (int i = 0; i < 39 ; i++)
  33. {
  34. id[i] = tempS[i];
  35. avgS[i] = tempA[i];
  36. }
  37.  
  38.  
  39. insertionSort(avgS, id, size);
  40. for (int i = 0; i < size; i++)
  41. {
  42. cout<< id[i] << " " << avgS[i] << endl;
  43. }
  44.  
  45. return 0;
  46. }
  47.  
  48. //**************************************************************
  49. // Definition of function insertionSort.
  50. // This function performs an ascending order insertion sort on
  51. // array. size is the number of elements in the array.
  52. //**************************************************************
  53.  
  54. void insertionSort(int arrayA[], int arrayB[], int size)
  55. {
  56. for (int curr = 1; curr < size; curr++)
  57. {
  58. int holdscore = arrayA[curr];// copy current element to hold
  59. int holdid = arrayB[curr];
  60. int back = curr - 1;
  61. while (back >= 0 && holdscore > arrayA[back]) // search where to insert the current element
  62. {
  63. arrayA[back + 1] = arrayA[back];// shift to the right
  64. arrayB[back + 1] = arrayB[back];
  65. back--;
  66. }
  67. arrayA[back + 1] = holdscore;
  68. arrayB[back + 1] = holdid; // put hold back to the array
  69. }
  70. }
  71. //*************************************************************
  72. //This is the score function.
  73. //This function does both read score from the file and
  74. //calculate the average of the score.
  75. //************************************************************
  76. int Scores(int id[], int avg[])
  77. {
  78. ifstream inFile;
  79. inFile.open ("scores.txt");
  80. int sid, score[8], i = 0;
  81.  
  82. inFile >> sid >> score[0] >> score [1] >> score[2] >> score[3] >> score[4] >> score[5] >> score[6] >> score[7];
  83. while (!inFile.eof())
  84. {
  85. id[i] = sid;
  86. avg[i] = (score[0]+ score[1] + score[2] + score[3]+ score[4] + score [5] + score [6] + score [7])/8;
  87. inFile >> sid >> score[0] >> score [1] >> score[2] >> score[3] >> score[4] >> score[5] >> score[6] >> score[7];
  88. cout <<id[i] << " " << avg[i] << endl;
  89. i++;
  90. }
  91.  
  92. inFile.close();
  93. return i;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement