Question: How would I fix this C code with the error argv undeclared, first use in this function I am trying to run arguments so that
How would I fix this C code with the error "argv undeclared, first use in this function" I am trying to run arguments so that i am able to take in any file of strings and place them into an array without having to hardcode the file name, but I am getting this error on line 14.
Here is my code with the attempt at doing so:
#include
// Check that it opened properly if (fptr == NULL) { printf("Cannot open file "); exit(0); }// End of if condition // Reads number of numbers in the file fscanf(fptr, "%d", &numberOfNUM);
// Dynamically allocates memory to pointer pointerNUM pointerNUM = (int *) calloc(numberOfNUM, sizeof(int)); // Loops numberOfNUM times for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++) // Reads each number and stores it in array fscanf(fptr, "%d", &pointerNUM[cntVAR]); // Returns the length of the numbers return numberOfNUM; fclose(fptr); }// End of function
// Function to display numbers void display(int numberOfNUM) { int cntVAR; // Loops numberOfNUM times for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++) // Displays each number printf("%4d, ", pointerNUM[cntVAR]); }// End of function
// Function for insertion sort void insertionSort(int numberOfNUM) { int x, key, y; // Loops numberOfNUM times for (x = 1; x < numberOfNUM; x++) { // Stores i index position data in key key = pointerNUM[x]; // Stores x minus one as y value y = x - 1;
/* Move elements of pointerNUM[0..x - 1], that are greater than key, to one position ahead of their current position */ while (y >= 0 && pointerNUM[y] > key) { // Stores pointerNUM y index position value at pointerNUM y next index position pointerNUM[y + 1] = pointerNUM[y]; // Decrease the y value by one y = y - 1; }// End of while // Stores the key value at pointerNUM y plus one index position pointerNUM[y + 1] = key; }// End of for loop }// End of function void insertionSort(int numberOfNUM); int readFile(); void display(int numberOfNUM); // main function int main(int argc, char *argv[]) { // To store the numbers of number in file int numberOfNUM; // Calls the function to read numbers and stores the length numberOfNUM = readFile(argc, argv); // Calls the function to displays the numbers before sorting printf(" Before sort : "); display(numberOfNUM); // Calls the function for sorting insertionSort(numberOfNUM); // Calls the function to displays the numbers after sorting printf(" After sort: "); display(numberOfNUM); }// End of main function
--------------------------------------------------------------------------------------------------------------------------------------
And this is my original working code that required a hardcoding of the file:
#include
// Check that it opened properly if (fptr == NULL) { printf("Cannot open file "); exit(0); }// End of if condition // Reads number of numbers in the file fscanf(fptr, "%d", &no);
// Dynamically allocates memory to pointer arr arr = (int *) calloc(no, sizeof(int)); // Loops no times for(c = 0; c < no; c++) // Reads each number and stores it in array fscanf(fptr, "%d", &arr[c]); // Returns the length of the numbers return no; }// End of function
// Function to display numbers void display(int no) { int c; // Loops no times for(c = 0; c < no; c++) // Displays each number printf("%4d, ", arr[c]); }// End of function
// Function for insertion sort void insertionSort(int no) { int x, key, y; // Loops no times for (x = 1; x < no; x++) { // Stores i index position data in key key = arr[x]; // Stores x minus one as y value y = x - 1;
/* Move elements of arr[0..x - 1], that are greater than key, to one position ahead of their current position */ while (y >= 0 && arr[y] > key) { // Stores arr y index position value at arr y next index position arr[y + 1] = arr[y]; // Decrease the y value by one y = y - 1; }// End of while // Stores the key value at arr y plus one index position arr[y + 1] = key; }// End of for loop }// End of function
// main function int main() { // To store the numbers of number in file int no; // Calls the function to read numbers and stores the length no = readFile(); // Calls the function to displays the numbers before sorting printf(" Before sorting: "); display(no); // Calls the function for sorting insertionSort(no); // Calls the function to displays the numbers after sorting printf(" After sorting: "); display(no); }// End of main function
Sample Run:
Before sorting: 10, 20, 11, 22, 45, 78, 5, 3, 7, 18, 28, 1, 0, 5, 8, 51, 32, 80, 9, 27, After sorting: 0, 1, 3, 5, 5, 7, 8, 9, 10, 11, 18, 20, 22, 27, 28, 32, 45, 51, 78, 80,
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
