You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: PROGRAMS/10. Files/Solution.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,11 +99,32 @@ int main()
99
99
2.
100
100
101
101
## Explanation:
102
+
This C program reads information about vehicles from an input file and allows the user to search for vehicles by their registration number. Here is the main logic of the code in detail:
103
+
104
+
1. The `vehicle_t` structure is defined to represent a vehicle, with fields for the registration number, name, and address of the vehicle.
105
+
2. The `print` function takes the name of the input file as an argument.
106
+
3. The `print` function opens the input file for reading and checks if it was opened successfully.
107
+
4. If the input file was opened successfully, the `print` function closes it immediately after checking its existence.
108
+
5. The `print` function prompts the user to enter the number of records they want to search for using `printf` and `scanf`.
109
+
6. The `print` function enters a loop to search for 'n' records, where 'n' is the number entered by the user.
110
+
7. For each iteration of the loop, the `print` function re-opens the input file for reading.
111
+
8. The `print` function prompts the user to enter a registration number to search for using `printf` and `scanf`.
112
+
9. The `print` function reads each vehicle from the input file using `fscanf`.
113
+
10. For each vehicle read from the input file, the `print` function compares its registration number with the registration number entered by the user using `strcmp`.
114
+
11. If a match is found, the `print` function prints the vehicle's details using `printf`, sets a flag to indicate that a match was found, and exits the inner loop.
115
+
12. If no match is found after all vehicles have been read from the input file, the `print` function prints "Not Found" using `printf`.
116
+
13. After searching for the current record, the `print` function resets the flag and closes the input file.
117
+
14. After all 'n' records have been searched for, the `print` function returns.
118
+
15. The `main` function prompts the user to enter the name of the input file using `printf` and `scanf`.
119
+
16. The `main` function appends ".txt" to the filename using `strcat`.
120
+
17. The `main` function calls the `print` function with the filename as an argument.
102
121
103
122
## Time and Space Complexity:
104
123
### `Time Complexity`:
124
+
The time complexity of this code is O(nm), where n is the number of records that are searched for and m is the number of vehicles in the input file, because each record search requires reading all vehicles from the input file once.
105
125
106
126
### `Space Complexity`:
127
+
The space complexity of this code is O(1), because it uses a constant amount of memory (i.e., memory usage does not depend on the size of the input). However, this analysis assumes that all strings (i.e., registration numbers, names, and addresses) have a fixed maximum length, as specified by their respective character arrays in their respective structures.
0 commit comments