Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // arbitrary buffer size for reading file input
- #define BUFSIZE 1000
- int find_array_maxes(int *input_array, int size){
- int max = 0;
- for(int i=0; i < size; i++){
- if(max < input_array[i]){
- max = input_array[i];
- }
- }
- return max;
- }
- int main(int argc, char *argv[]){
- // check to ensure there are sufficient command line arguments
- if(argc <= 2){
- printf("At least two arguments are required!\n");
- exit(1);
- }
- // allocates memory for array that will store file values
- int array_size = atoi(argv[1]);
- int *array_ptr = (int*) malloc(array_size * sizeof(int));
- // creates a file ptr and buffer to read file contents
- FILE *file = fopen(argv[2], "r");
- char buff[BUFSIZE];
- int converted_num;
- int counter = 0;
- // verify the array is allocated
- if(array_ptr == NULL){
- printf("Memory allocation of %d bytes has failed.\n", *array_ptr);
- fclose(file);
- exit(1);
- } else {
- // stores contents of file into the buffer and is converted from the buffer to int
- // to be stored in the dynamically allocated array
- while(fgets(buff, BUFSIZE-1, file) != NULL && counter < array_size){
- converted_num = atoi(buff);
- *array_ptr = converted_num;
- printf("%d\n", *array_ptr);
- array_ptr++;
- counter++;
- }
- fclose(file);
- }
- printf("The max number of the array is %d\n",find_array_maxes(array_ptr, array_size));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement