Advertisement
M4ritimeSeeker

PASS Week 13 Solutions

Nov 30th, 2021 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. PASS WEEK 13 SOLUTIONS
  2.  
  3.  
  4.  
  5.  
  6. PROBLEM #1
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. int main(){
  12.  
  13. int* ptr = NULL;
  14. int numElem;
  15.  
  16. printf("Enter in number of elements: ");
  17. scanf("%d", &numElem);
  18.  
  19. ptr = (int*)calloc(numElem, sizeof(int));
  20.  
  21. for(int i = 0; i < numElem; i++)
  22. {
  23. scanf("%d", &ptr[i]);
  24. }
  25.  
  26. for(int i = 0; i < numElem; i++)
  27. {
  28. printf("%d ", ptr[i]);
  29. }
  30.  
  31. free(ptr);
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. PROBLEM #2
  53.  
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57.  
  58. int* createArray(int);
  59. void fillArray(int, int, int*);
  60. void printArray(int, int*);
  61.  
  62. int main (int argc, char* argv[]) {
  63.  
  64. int sz = atoi(argv[1]);
  65. int value = atoi(argv[2]);
  66. int* array = createArray(sz);
  67. fillArray(value, sz, array);
  68. printArray(sz, array);
  69.  
  70. return(0);
  71. }
  72.  
  73. int* createArray(int size){
  74. int* ary = (int*) calloc(size, sizeof(int));
  75. return ary;
  76. }
  77.  
  78. void fillArray(int value, int size, int* ary){
  79.  
  80. int startValue = value;
  81. for (int i = 0; i < size; i++){
  82. ary[i] = value;
  83. value += startValue;
  84. }
  85.  
  86. return;
  87. }
  88.  
  89. void printArray(int size, int* ary){
  90. for (int i = 0; i < size; i++){
  91. printf(" %d ", ary[i]);
  92. }
  93. printf("\n");
  94. return;
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. PROBLEM #3
  105.  
  106. #include <stdio.h>
  107. #include <stdlib.h>
  108. #include <string.h>
  109.  
  110. int main () {
  111.  
  112. char *str;
  113.  
  114. /* Initial memory allocation */
  115. str = (char *) malloc(15);
  116. strcpy(str, "wordswordswords");
  117. printf("String = %s, Address = %s\n", str, str);
  118.  
  119. /* Reallocating memory */
  120. str = (char *) realloc(str, 25);
  121. strcat(str, “, morewords");
  122. printf("String = %s, Address = %s\n", str, str);
  123. free(str);
  124.  
  125. return(0);
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. PROBLEM #4
  134.  
  135. #include <stdio.h>
  136. #include <string.h>
  137. #include <stdlib.h>
  138. void PrintArray(int, char**);
  139. int main(void) {
  140. char* ary[5];
  141. char tmp[80];
  142. int strSz = 0;
  143. int i =0;
  144. printf("Enter in a series of 5 strings:");
  145. while(i < 5){
  146. fscanf(stdin, "%s", tmp);
  147. ary[i] = (char*) malloc(sizeof(tmp));
  148. strcpy(ary[i++],tmp);
  149. }
  150. PrintArray(i, ary);
  151. return 0;
  152. }
  153. void PrintArray(int sz, char** aryIn){
  154. int i = 0;
  155. for(i = 0; i < 5; i++){
  156. printf("The value is %s\n", aryIn[i]);
  157. }
  158. return;
  159. }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. PROBLEM #5
  176.  
  177. #include <stdio.h>
  178. #include <string.h>
  179. #include <stdlib.h>
  180.  
  181. int GetDimensions(int * , int * , char * );
  182.  
  183. int main(void) {
  184. char fName[80];
  185. int rows = 0;
  186. int cols = 0;
  187. printf("Enter the file name: ");
  188. scanf(" %s", fName);
  189.  
  190. if (GetDimensions( & rows, & cols, fName)) {
  191. printf("The file has %d records and the largest is %d characters in length\n",
  192. rows, cols);
  193. } else {
  194. printf("The file could not be processed.\n");
  195. }
  196.  
  197. return 0;
  198. }
  199. int GetDimensions(int * rows, int * max, char * fn) {
  200. FILE * fp = fopen(fn, "r");
  201. int cnt = 0;
  202. int tmp = 0;
  203. char buffer[1024];
  204. * max = 0;
  205.  
  206. if (fp == NULL)
  207. return 0;
  208. else {
  209. while (fgets(buffer, 1024, fp) != NULL) {
  210. cnt++;
  211. tmp = strlen(buffer);
  212. if (tmp > * max) {
  213. * max = tmp;
  214. }
  215. }
  216. * rows = cnt;
  217. fclose(fp);
  218. }
  219. return 1;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement