M4ritimeSeeker

PASS Week 11/17

Nov 15th, 2020 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. Problem #1:
  2.  
  3. Create a program that takes a string as input when the program is started and prints
  4. the words of the sentence in reverse. You will need to use argc and argv in the main
  5. function. You should not need to declare your own array but use the argv array and
  6. a loop to present the input strings in the reverse order.
  7. Note: Recall that char* argv[] is an array of character pointers. So each input string
  8. occupies a row in the array.
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. Problem #2:
  19.  
  20. Compose a program that prompts the user to enter in 10 numbers and store them
  21. into an array of strings one at a time. The array should be a 2d array of characters
  22. that can hold 10 strings up to a length of 20 characters each. Handle the numerical
  23. input as strings and store them into the array (You don’t need to reference the 2nd
  24. dimension). Use a loop to print out the strings to verify you have your numbers
  25. stored as asci characters. Now loop through the array and use the atof() function to
  26. convert the string version of the numbers into a double type and add it to a sum
  27. variable. Print out the sum and end the program. Note: scanf() will read in strings
  28. from standard in until they encounter (a space, end of file, or newline/carriage
  29. return) gets and fgets will include those items, which makes scanf a better choice
  30. here
  31.  
  32. SAMPLE OUTPUT:
  33.  
  34. Enter in a number: 1
  35. Enter in a number: 2
  36. Enter in a number: 3
  37. Enter in a number: 4
  38. Enter in a number: 5
  39. Enter in a number: 6
  40. Enter in a number: 7
  41. Enter in a number: 8
  42. Enter in a number: 9
  43. Enter in a number: 10
  44. 1
  45. 2
  46. 3
  47. 4
  48. 5
  49. 6
  50. 7
  51. 8
  52. 9
  53. 10
  54. Sum: 55.00
  55.  
  56.  
  57.  
  58.  
  59.  
  60. /*** SKELETON CODE ***/
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. int main(void)
  66. {
  67. char ary[10][20];
  68. double sum = 0.0;
  69. int i; //For the loops
  70.  
  71. [YOUR CODE HERE]
  72.  
  73. printf("Sum: %.2lf\n", sum);
  74. return 0;
  75. }//end main
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. Problem #3:
  92.  
  93. Create a program that prompts the user for the name of a text file (example:
  94. input.txt). Your program should verify if the file exists and report to the user if it does
  95. or if it does not.
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. Problem #4:
  118.  
  119. Analyze the following program. Figure what it is doing, and what each of its functions
  120. and if-else conditionals do. ONLY RUN THIS PROGRAM AFTER YOU HAVE FIGURED OUT ITS FUNCTION.
  121.  
  122. #include <stdio.h>
  123. #include <string.h>
  124. #include <stdbool.h>
  125.  
  126. void FlushInput(void);
  127. bool FileExists(char*);
  128. bool CreateFile(char*, char*);
  129.  
  130. int main(void){
  131. FILE* fIn = NULL;
  132. char textIn[300];
  133. char fileName[80];
  134. char saveFile = 'y';
  135. bool result = false;
  136.  
  137. printf("Enter in some text to save:");
  138.  
  139. if(fgets(textIn,300,stdin)){
  140. printf("Your entered in %d characters.\n", (int) strlen(textIn));
  141. printf("Enter in the name of a text file: ");
  142. fscanf(stdin,"%s",fileName);
  143.  
  144. if(FileExists(fileName) == true){
  145. printf("The file exists, enter 'y' to overwrite:");
  146. FlushInput();
  147. scanf("%c", &saveFile);
  148.  
  149. if(saveFile == 'y')
  150. result = CreateFile(textIn, fileName);
  151. else
  152. printf("You chose not to save the data.\n");
  153. }
  154. else{
  155. result = CreateFile(textIn, fileName);
  156. }//end if-else
  157. }
  158. else {
  159. printf("You did not enter in anything to save.\n");
  160. }
  161.  
  162. if(result)
  163. printf("You successfully created a file named %s\n", fileName);
  164. else
  165. printf("You did not create a file named %s\n", fileName);
  166.  
  167. return 0;
  168. }//end main
  169.  
  170.  
  171. bool FileExists(char* fileName){
  172. FILE* fIn = fopen(fileName,"r");
  173.  
  174. if(fIn == NULL)
  175. return false;
  176. else{
  177. fclose(fIn);
  178. return true;
  179. }
  180. }//end function
  181.  
  182.  
  183. void FlushInput(void){
  184. while(getchar() != '\n');
  185. return;
  186. }//end function
  187.  
  188.  
  189. bool CreateFile(char* textIn, char* fName){
  190. FILE* fPtr = fopen(fName,"w");
  191.  
  192. if(fPtr == NULL){
  193. printf("Error opening file.\n");
  194. return false;
  195. }
  196.  
  197. else{
  198. fputs(textIn, fPtr);
  199. fclose(fPtr);
  200. return true;
  201. }//end if-else
  202.  
  203. }//end function
Add Comment
Please, Sign In to add comment