Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. Answered the 1st 4 sub-parts of the question as per Chegg's answering guidelines. This will provide with almost everything required for the next 2 sub-parts. Hope this helps!
  2.  
  3. All the explanations is in the code comments.
  4.  
  5. Code:
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <stdlib.h>
  10. #include<time.h>
  11. #include <iomanip> // std::setprecision
  12.  
  13. using namespace std;
  14.  
  15. // (0) function does not take any parameters
  16. // returns an integer representing your user's menu choice
  17. int displayMenu()
  18. {
  19. int choice;
  20.  
  21. while(true)
  22. {
  23. cout << "Menu ----------------------- " << endl;
  24. cout << "Choose 0 to exit" << endl;
  25. cout << "Choose a function from 1 to 5" << endl;
  26. cin >> choice;
  27.  
  28. // return valid user input
  29. if(choice>=0 && choice<=5)
  30. return choice;
  31. }
  32. }
  33.  
  34. // (1) function
  35. void function1()
  36. {
  37. // required variables
  38. string filename;
  39. int rows, cols;
  40. long double val;
  41.  
  42. // prompt the user for the name of a file to output as a text file
  43. // that will hold a two dimensional array of the long double data type.
  44. cout << "Enter name of output file: ";
  45. cin >> filename;
  46.  
  47. // open file
  48. ofstream fout;
  49. fout.open (filename);
  50.  
  51. fout << "UNSORTED" << endl; // 1st line in file
  52.  
  53. // prompt the user to enter the number of rows
  54. // and the number of columns for the two dimensional array.
  55. cout << "Enter number of rows: ";
  56. cin >> rows;
  57. cout << "Enter number of columns: ";
  58. cin >> cols;
  59.  
  60. fout << rows << " " << cols << endl; // 2nd line
  61.  
  62. // prompt the user to enter the values for each row
  63. // and column element in the two dimensional array.
  64. for(int i=0; i<rows; i++)
  65. {
  66. for(int j=0; j<cols; j++)
  67. {
  68. cout << "Enter value for (" << (i+1) << ", " << (j+1) << ") cell: ";
  69. cin >> val;
  70. fout << std::setprecision(15) << val << " "; // write value to file
  71. }
  72. fout << endl;
  73. }
  74.  
  75. // close file
  76. fout.close();
  77. }
  78.  
  79. // (2) function like function #1, except this time,
  80. // instead of asking the user for the values for each row and column element,
  81. // have the function create a randomly generated values for the number of rows and columns
  82. // (a minimum of 2 rows and 2 columns, and a maximum of 10 rows and 10 columns).
  83. void function2()
  84. {
  85. // required variables
  86. string filename;
  87. int rows, cols;
  88. long double val;
  89.  
  90. // prompt the user for the name of a file to output as a text file
  91. // that will hold a two dimensional array of the long double data type.
  92. cout << "Enter name of output file: ";
  93. cin >> filename;
  94.  
  95. // open file
  96. ofstream fout;
  97. fout.open (filename);
  98.  
  99. fout << "UNSORTED" << endl; // 1st line in file
  100.  
  101. // Use current time as seed for random generator
  102. srand(time(0));
  103. // generate the number of rows and the number of columns
  104. // for the two dimensional array. (between 2 and 10)
  105. rows = rand() % 9 + 2;
  106. cols = rand() % 9 + 2;
  107.  
  108. fout << rows << " " << cols << endl; // 2nd line
  109.  
  110. // prompt the user to enter the values for each row and column element in the two dimensional array.
  111. for(int i=0; i<rows; i++)
  112. {
  113. for(int j=0; j<cols; j++)
  114. {
  115. long double a = (double)rand() / (double)(RAND_MAX);
  116. long double b = (double)rand() / (double)(RAND_MAX);
  117.  
  118. // divide the smaller number by larger number
  119. if(a<b)
  120. val = a/b;
  121. else
  122. val = b/a;
  123. // fprintf(fout, "%.15Lf\n", val);
  124. fout << std::setprecision(15) << val << " "; // write value to file
  125. }
  126. fout << endl;
  127. }
  128.  
  129. // close file
  130. fout.close();
  131. }
  132.  
  133. // (3) function
  134. void function3()
  135. {
  136. // required variables
  137. string filename, temp;
  138. int rows, cols;
  139. long double val;
  140.  
  141. // prompt the user for the name of a file to input as a text file
  142. // to read in the two dimensional array of long double type.
  143. cout << "Enter name of input file: ";
  144. cin >> filename;
  145.  
  146. // open file
  147. ifstream fin(filename);
  148. if (fin.is_open())
  149. {
  150. fin >> temp;
  151.  
  152. // read rows and columns number
  153. fin >> rows >> cols;
  154.  
  155. // read values
  156. for(int i=0; i<rows; i++)
  157. {
  158. for(int j=0; j<cols; j++)
  159. {
  160. fin >> val;
  161. cout << std::setprecision(15) << val << " "; // print contents
  162. }
  163. cout << endl;
  164. }
  165.  
  166. // close file
  167. fin.close();
  168. }
  169. else cout << "Unable to open file";
  170. }
  171.  
  172. // main function
  173. int main()
  174. {
  175. // sample run
  176. while(true)
  177. {
  178. // user's choice for menu
  179. int choice = displayMenu();
  180.  
  181. if(choice == 0)
  182. {
  183. break;
  184. }
  185. else if(choice == 1)
  186. {
  187. function1();
  188. }
  189. else if(choice == 2)
  190. {
  191. function2();
  192. }
  193. else if(choice == 3)
  194. {
  195. function3();
  196. }
  197. }
  198.  
  199. return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement