Advertisement
Davidwebb

C++ help

Nov 8th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <iostream> // access to cin, cout
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include<fstream>
  5.  
  6.  
  7.  
  8. using namespace std;
  9.  
  10. int buildArrays(int A[],int B[],int C[])
  11. {
  12. int i=0,num;
  13. ifstream inFile;
  14. inFile.open("candycrush.txt");
  15.  
  16. if(inFile.fail())
  17. {
  18. cout<<"The candycrush.txt input file did not open"<<endl;
  19. exit(-1);
  20. }
  21. while(inFile)
  22. {
  23. inFile>>num;
  24. A[i]=num;
  25.  
  26. inFile>>num;
  27. B[i]=num;
  28.  
  29. inFile>>num;
  30. C[i]=num;
  31.  
  32. i++;
  33. }
  34. inFile.close();
  35.  
  36. return i;
  37. }
  38. void printArrays( string reportTitle, int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels )
  39. {
  40. cout<<endl;
  41. cout<<reportTitle<<endl;
  42. cout<<"Levels\tScores\tStars"<<endl;
  43. cout<<"---------------------"<<endl;
  44.  
  45. for(int i=0;i<numberOfLevels;i++)
  46. {
  47. cout<<levelsArray[i]<<"\t"<<scoresArray[i]<<"\t";
  48. for(int j=0;j<starsArray[i];j++)
  49. {
  50. cout<<"*";
  51. }
  52. cout<<endl;
  53. }
  54. }
  55.  
  56. void sortArrays( int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels )
  57. {
  58. for(int i=0;i<numberOfLevels;i++)
  59. {
  60. for(int j=0;j<numberOfLevels;j++)
  61. {
  62. if(levelsArray[i]<levelsArray[j])
  63. {
  64. int temp1=levelsArray[i];
  65. int temp2=scoresArray[i];
  66. int temp3=starsArray[i];
  67.  
  68. levelsArray[i]=levelsArray[j];
  69. scoresArray[i]=scoresArray[j];
  70. starsArray[i]=starsArray[j];
  71.  
  72. levelsArray[j]=temp1;
  73. scoresArray[j]=temp2;
  74. starsArray[j]=temp3;
  75. }
  76. }
  77. }
  78. }
  79.  
  80.  
  81. int main()
  82. {
  83. int MAX=400;
  84. int levelsArray[MAX];
  85. int scoresArray[MAX];
  86. int starsArray[MAX];
  87.  
  88. int numberOfLevels=buildArrays(levelsArray,scoresArray,starsArray);
  89.  
  90. printArrays( "Candy Crush UNSORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels );
  91. sortArrays( levelsArray, scoresArray, starsArray, numberOfLevels);
  92. printArrays( "Candy Crush SORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels );
  93.  
  94. system("pause");
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement