Advertisement
tom3531

hwk2

Jul 2nd, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. //
  2. // main.c
  3. // hwk2
  4. //
  5. // Created by Mike Witter on 6/29/15.
  6. // Copyright (c) 2015 Mike Witter. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #define NAME_SIZE 25
  14. #define NUM_SCORES 3
  15. #define MAX_SIZE 20
  16.  
  17. //Struct student
  18. typedef struct student {
  19. char name[NAME_SIZE];
  20. int scores[NUM_SCORES];
  21. } Student;
  22.  
  23. // Function Prototype
  24. int read_data(Student *s, int size, const char *fileName, int index);
  25. void print_data(Student *s, int size);
  26. void selection_sort(Student *s, int size);
  27. int binary_search(Student *s, char *target, int start_idx, int end_idx);
  28. void display_stats(Student *s, int size);
  29. int lowest_score(Student *s, int size);
  30. int highest_score(Student *s, int size);
  31. // end Function Prototype
  32.  
  33. int main(int argc, const char * argv[]) {
  34.  
  35. // Hold index value of were to search in file, given by user
  36. int index = 0;
  37.  
  38. // Hold size of number of student to read
  39. int size = 0;
  40.  
  41.  
  42.  
  43.  
  44. // Char array to hold binary search value
  45. int found_student = 0;
  46.  
  47. // argv[0] = program
  48. // argv[1] = input.txt
  49. // arvg[2] = index position
  50.  
  51. // Make sure there are 3 arguments if not display error and exit
  52.  
  53. if (argc != 3) {
  54. printf("Arguments passed through command line not equal to 3");
  55. return -1;
  56. }
  57.  
  58.  
  59. // Convert argv[2] to int
  60. index = atoi(argv[2]);
  61. /*
  62. Check that (index is not less than 1 OR index is not greater than 100)
  63. */
  64.  
  65. do{
  66. // Ask user how many students they would like to load
  67. printf("How many students would you like to load: ");
  68. scanf("%d", &size);
  69. }while (index + size > 100);
  70.  
  71. // Malloc space for size given by user
  72. Student *s = malloc(sizeof(Student) * size);
  73.  
  74. // Call read_data(s, size, *fileName, index)
  75. read_data(s, size, argv[1], index);
  76.  
  77. // Print data before Selection Sort
  78. printf("Before Selection Sort....\n");
  79. print_data(s, size);
  80.  
  81. //Call selection sort
  82. selection_sort(s, size);
  83.  
  84. //Print data after selection sort
  85. printf("After Selection Sort....\n");
  86. print_data(s, size);
  87.  
  88. // Char to hold value
  89.  
  90. // Question
  91. char ans = 'y';
  92. int c;
  93.  
  94. do{
  95.  
  96. // Char array to hold string name to search for
  97. char student_Name[NAME_SIZE];
  98. // Ask user user to enter name
  99. printf("Enter a name you would like to search for: ");
  100. while ((c=getchar())!='\n' && c!=EOF);
  101.  
  102. // Look for keyboard input
  103. scanf("%[^\n]", student_Name);
  104. getchar();
  105.  
  106.  
  107. // Call binary search
  108. found_student = binary_search(s, student_Name, 0, size - 1);
  109.  
  110.  
  111.  
  112. // If found_student is == -1 student not found
  113. if(found_student == -1){
  114.  
  115. printf("\nStudent was not found");
  116. }
  117. else{
  118.  
  119. // Make another function to find student name at index
  120. // char
  121. // char convert[10];
  122. // Convert int to string
  123. //snprintf(found_student, "%d", convert);
  124. //printf("Student found at index: %d\n", found_student);
  125. //read_data(s, NULL, argv[1], found_student);
  126. printf("%s was found at index %d", student_Name, found_student+1);
  127. }
  128. printf("\nWould you like to contiune (y/n): ");
  129. scanf("%c", &ans);
  130. } while (ans != 'n');
  131.  
  132.  
  133. if (lowest_score(s, size)) {
  134.  
  135.  
  136. }
  137. else{
  138. printf("Could not find: ");
  139. }
  140.  
  141. if (highest_score(s, size)) {
  142.  
  143. }
  144. else{
  145. printf("Cound not find: ");
  146. }
  147. free(s);
  148.  
  149. } // end main
  150.  
  151.  
  152. int read_data(Student *s, int size, const char *fileName, int index){
  153.  
  154. // filePtr
  155. FILE *fileName_Open;
  156.  
  157. // hold data from read
  158. int data = 0;
  159.  
  160. // Check to see if file pointer is null
  161. if((fileName_Open = fopen(fileName, "rb+")) == NULL){
  162. printf("\nCannot open file\n");
  163. return -1;
  164. }
  165.  
  166. //Go to position in file given by user
  167. fseek(fileName_Open, (index - 1) * sizeof(Student), SEEK_SET);
  168.  
  169. // Read from pointer position which is set from index
  170. data = fread(s, sizeof(Student), size, fileName_Open);
  171.  
  172. //free(fileName_Open);
  173. fclose(fileName_Open);
  174.  
  175. return data;
  176.  
  177. } // end of read_data
  178.  
  179. void print_data(Student *s, int size){
  180.  
  181. int i, j = 0;
  182.  
  183. //Print the records
  184. printf("Printing %d records\n", size);
  185.  
  186. for (i = 0; i < size; i++) {
  187. printf("Student Name: %s\n", s[i].name);
  188.  
  189. for (j = 0; j < NUM_SCORES; j++) {
  190. printf("Score %d: %d\n",j+1, s[i].scores[j]);
  191. }
  192. }
  193. }
  194.  
  195. void selection_sort(Student *s, int size){
  196.  
  197. // Variable to hold value
  198. Student temp;
  199.  
  200. // for loop variable
  201. int i = 0;
  202. int j = 0;
  203.  
  204. //Variable to hold smallest
  205. int smallest = 0;
  206.  
  207. // Loop through size value given by user
  208. for(i = 0; i < size - 1; i++){
  209. smallest = i;
  210.  
  211. // strcmp name with ASCII characters
  212. for(j = i + 1; j < size; j++){
  213. if(strcmp(s[j].name, s[smallest].name) < 0){
  214. smallest = j;
  215. } // end if
  216.  
  217. } // end inner
  218.  
  219. if (smallest != i) {
  220. temp = s[i];
  221. s[i] = s[smallest];
  222. s[smallest] = temp;
  223. } // end if
  224.  
  225. }// end outer loop
  226.  
  227. } // end selection
  228.  
  229. int binary_search(Student *s, char *target, int startIndex, int endIndex){
  230.  
  231. int midIndex = 0;
  232.  
  233. if(startIndex > endIndex){
  234. return -1;
  235. }
  236.  
  237. midIndex = (startIndex + endIndex) / 2;
  238.  
  239. if(strcmp(target, s[midIndex].name) < 0){
  240. return binary_search(s, target, startIndex, midIndex-1);
  241. }
  242. else if (strcmp(target, s[midIndex].name ) > 0){
  243. return binary_search(s, target, midIndex+1, endIndex);
  244. }
  245. else if (strcmp(target, s[midIndex].name ) == 0){
  246. return midIndex;
  247. }
  248.  
  249. return 0;
  250. }
  251.  
  252. int lowest_score(Student *s, int size){
  253.  
  254. //Variable to hold lo5west value
  255. int lowest = 101;
  256. int indexName = -1;
  257. int indexScore = -1;
  258.  
  259. // For loop
  260. int i = 0;
  261. int j = 0;
  262.  
  263. for (i = 0; i < size; i++) {
  264. for(j = 0; j < NUM_SCORES; j++){
  265. if(s[i].scores[j] < lowest){
  266. lowest = s[i].scores[j];
  267. indexName = i;
  268. indexScore = j;
  269. }
  270. }
  271. }
  272. FILE *fp;
  273.  
  274. // Check to see if file pointer is null
  275. if((fp = fopen("stats.txt", "w+")) == NULL){
  276. printf("\nCannot open file\n");
  277. return -1;
  278. }
  279. else{
  280.  
  281. fprintf(fp, "\n%s had the lowest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
  282. }
  283. printf("\n%s had the lowest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
  284.  
  285. return 1;
  286. }
  287.  
  288. int highest_score(Student *s, int size){
  289.  
  290. //Variable to hold lo5west value
  291. int lowest = 0;
  292. int indexName = -1;
  293. int indexScore = -1;
  294.  
  295. // For loop
  296. int i = 0;
  297. int j = 0;
  298.  
  299. for (i = 0; i < size; i++) {
  300. for(j = 0; j < NUM_SCORES; j++){
  301. if(s[i].scores[j] > lowest){
  302. lowest = s[i].scores[j];
  303. indexName = i;
  304. indexScore = j;
  305. }
  306. }
  307. }
  308.  
  309. FILE *fp;
  310.  
  311. // Check to see if file pointer is null
  312. if((fp = fopen("stats.txt", "a+")) == NULL){
  313. printf("\nCannot open file\n");
  314. return -1;
  315. }
  316. else{
  317.  
  318. fprintf(fp, "\n%s had the lowest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
  319. }
  320. printf("\n%s had the lowest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
  321.  
  322. return 1;
  323.  
  324. printf("\n%s had the highest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
  325.  
  326.  
  327. return 1;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement