Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // arbitrary buffer size for reading file input
  5. #define BUFSIZE 1000
  6.  
  7. void find_array_maxes(int *input_array){
  8. //int max = 0;
  9. printf("%d\n", *input_array);
  10. while(*input_array != '\0'){
  11.  
  12. //if(max < *input_numbers){
  13. // max = *input_numbers;
  14. //}
  15. input_array++;
  16. }
  17. //return max;
  18. }
  19.  
  20. int main(int argc, char *argv[]){
  21. // check to ensure there are sufficient command line arguments
  22. if(argc <= 2){
  23. printf("At least two arguments are required!\n");
  24. exit(1);
  25. }
  26.  
  27. // allocates memory for array that will store file values
  28. int array_size = atoi(argv[1]);
  29. int *array_ptr = (int*) malloc(array_size * sizeof(int));
  30.  
  31. // creates a file ptr and buffer to read file contents
  32. FILE *file = fopen(argv[2], "r");
  33. char buff[BUFSIZE];
  34. int converted_num;
  35. int counter = 0;
  36.  
  37. // verify the array is allocated
  38. if(array_ptr == NULL){
  39. printf("Memory allocation of %d bytes has failed.\n", *array_ptr);
  40. fclose(file);
  41. exit(1);
  42. } else {
  43. // stores contents of file into the buffer and is converted from the buffer to int
  44. // to be stored in the dynamically allocated array
  45. while(fgets(buff, BUFSIZE-1, file) != NULL && counter < array_size){
  46. converted_num = atoi(buff);
  47. *array_ptr = converted_num;
  48. printf("%d\n", *array_ptr);
  49. array_ptr++;
  50. counter++;
  51. }
  52. fclose(file);
  53. }
  54. find_array_maxes(array_ptr);
  55. //printf("The max number of the array is %d\n",find_array_maxes(array_ptr));
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement