candyapplecorn

hw2

Oct 6th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. int randgen(int magnitude);
  10. int subtract(int, int deduction);
  11. void populateArray(int a[][20],int,int);
  12. void getTotal(int a[][20],int,int,int&);
  13. void getAverage(int a[][20],int,int,float&, int);
  14. int getRowTotal(int a[][20],int,int,int);
  15. int getColumnTotal(int a[][20],int,int,int);
  16. int getHighestInRow(int table[][20],int,int);
  17. int getLowestInRow(int table[][20],int,int);
  18. void fout(ofstream&, int table[][20],int, int, int, float); //This function is HUGE; it takes care of practically all the output operations besides the pretty bars
  19. void pretty(int, ofstream &file); //my previous professor, prof. Ahren, said it is good to personalize our work (this is a generalization). If you're going to mark me down for this please don't and tell me not to do things like this in the future?
  20.  
  21. int main()
  22. {
  23. srand(time(NULL));
  24. ofstream file;
  25. file.open("out.txt");
  26. int columns=randgen(16)+5, rows=randgen(16)+5, table[20][20]={0}, total; //awwwww yeah look at that functions as initializers
  27. float average;
  28. populateArray(table,rows,columns);
  29. getTotal(table,rows,columns,total);
  30. getAverage(table,rows,columns,average, total);
  31. fout(file, table, rows, columns, total, average);
  32. return 0;
  33. }
  34.  
  35. void fout(ofstream &file, int table[][20],int rows, int columns, int total, float average)
  36. {
  37. file << setw(2) << left << rows << " " << "rows\n";
  38. file << setw(2) << left << columns << " "<< "columns\n";
  39.  
  40. for(int i=0;i<rows;i++){
  41. if(i==0)
  42. pretty(columns,file);
  43. for(int k=0;k<columns;k++){
  44. file << setw(3) << right << table[i][k] << " ";
  45. if(k==columns-1)
  46. file << endl;
  47. }
  48. if(i==rows-1)
  49. pretty(columns,file);
  50. }
  51. file << endl;
  52. for(int index=0;index<rows;index++){
  53. file << "Row " << setw(2) << index+1 << " Total: " << right << setw(4) << getRowTotal(table,columns,rows,index);
  54. file << setw(9)<< " Highest: " << setw(3) << getHighestInRow(table, index, columns);
  55. file << setw(9)<< " Lowest: " << setw(3) << getLowestInRow(table, index, columns);
  56. file << endl;
  57.  
  58. }
  59.  
  60. file << endl;
  61. for(int indexx=0;indexx<columns;indexx++)
  62. file << setw(7) << "Column " << setw(2) << indexx+1 << setw(7) << " Total: " << setw(4) << getColumnTotal(table,columns,rows,indexx) << endl;
  63.  
  64. file << endl;
  65. file << "The total of all " << columns*rows << " numbers is: " << total <<". ";
  66. file << "The average is " << average;
  67. pretty(columns,file);
  68. return;
  69. }
  70.  
  71. void pretty(int columns, ofstream &file)
  72. {
  73. static int greeting=0;
  74. if (greeting!=2){
  75. for(int pretty=0;pretty<columns*4;pretty++)
  76. file << "=";
  77. file << endl;
  78. }
  79. else if (greeting==2)
  80. cout << "Look in the same folder that HOMEWORK2 is saved in. You will\nfind a text file named out.txt; open it up!\n(You can close this window now)\n";
  81. greeting++;
  82. }
  83.  
  84. int getLowestInRow(int table[][20], int row, int columns)
  85. {
  86. int lowest=666;
  87. for(int index=0;index<columns;index++)
  88. if(table[row][index]<lowest)
  89. lowest=table[row][index];
  90. return lowest;
  91. }
  92.  
  93. int getHighestInRow(int table[][20], int row, int columns)
  94. {
  95. int highest=0;
  96. for(int index=0;index<columns;index++)
  97. if(table[row][index]>highest)
  98. highest=table[row][index];
  99. return highest;
  100. }
  101.  
  102. int getColumnTotal(int table[][20], int columns, int rows, int column)
  103. {
  104. int columnTotal=0;
  105. for(int index=0;index<rows; index++)
  106. columnTotal+=table[index][column];
  107. return columnTotal;
  108. }
  109.  
  110. int getRowTotal(int table[][20], int columns, int rows, int row)
  111. {
  112. int rowTotal=0;
  113. for(int index=0;index<columns; index++)
  114. rowTotal+=table[row][index];
  115. return rowTotal;
  116. }
  117. void getAverage(int table[][20],int rows, int columns, float &average, int total)
  118. {
  119. average=static_cast<float>(total)/(rows*columns);
  120. return;
  121. }
  122.  
  123. void getTotal(int table[][20], int rows, int columns, int &total)
  124. {
  125. total=0;
  126. for(int i=0;i<rows;i++)
  127. for(int k=0;k<columns;k++){
  128. total+=table[i][k];
  129. }
  130. return;
  131. }
  132.  
  133. int randgen(int magnitude)
  134. {
  135. return rand()%magnitude;
  136. }
  137.  
  138. int subtract(int number, int deduction)
  139. {
  140. if(rand()%2==0)
  141. return number-deduction;
  142. else
  143. return number;
  144. }
  145.  
  146. void populateArray(int a[][20],int rows,int columns)
  147. {
  148. for(int i=0;i<rows;i++)
  149. for(int k=0;k<columns;k++)
  150. a[i][k]=subtract(randgen(26),25);
  151. }
Advertisement
Add Comment
Please, Sign In to add comment