Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // hwk2
- //
- // Created by Mike Witter on 6/29/15.
- // Copyright (c) 2015 Mike Witter. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define NAME_SIZE 25
- #define NUM_SCORES 3
- #define MAX_SIZE 20
- //Struct student
- typedef struct student {
- char name[NAME_SIZE];
- int scores[NUM_SCORES];
- } Student;
- // Function Prototype
- int read_data(Student *s, int size, const char *fileName, int index);
- void print_data(Student *s, int size);
- void selection_sort(Student *s, int size);
- int binary_search(Student *s, char *target, int start_idx, int end_idx);
- void display_stats(Student *s, int size);
- int lowest_score(Student *s, int size);
- int highest_score(Student *s, int size);
- // end Function Prototype
- int main(int argc, const char * argv[]) {
- // Hold index value of were to search in file, given by user
- int index = 0;
- // Hold size of number of student to read
- int size = 0;
- // Char array to hold binary search value
- int found_student = 0;
- // argv[0] = program
- // argv[1] = input.txt
- // arvg[2] = index position
- // Make sure there are 3 arguments if not display error and exit
- if (argc != 3) {
- printf("Arguments passed through command line not equal to 3");
- return -1;
- }
- // Convert argv[2] to int
- index = atoi(argv[2]);
- /*
- Check that (index is not less than 1 OR index is not greater than 100)
- */
- do{
- // Ask user how many students they would like to load
- printf("How many students would you like to load: ");
- scanf("%d", &size);
- }while (index + size > 100);
- // Malloc space for size given by user
- Student *s = malloc(sizeof(Student) * size);
- // Call read_data(s, size, *fileName, index)
- read_data(s, size, argv[1], index);
- // Print data before Selection Sort
- printf("Before Selection Sort....\n");
- print_data(s, size);
- //Call selection sort
- selection_sort(s, size);
- //Print data after selection sort
- printf("After Selection Sort....\n");
- print_data(s, size);
- // Char to hold value
- // Question
- char ans = 'y';
- int c;
- do{
- // Char array to hold string name to search for
- char student_Name[NAME_SIZE];
- // Ask user user to enter name
- printf("Enter a name you would like to search for: ");
- while ((c=getchar())!='\n' && c!=EOF);
- // Look for keyboard input
- scanf("%[^\n]", student_Name);
- getchar();
- // Call binary search
- found_student = binary_search(s, student_Name, 0, size - 1);
- // If found_student is == -1 student not found
- if(found_student == -1){
- printf("\nStudent was not found");
- }
- else{
- // Make another function to find student name at index
- // char
- // char convert[10];
- // Convert int to string
- //snprintf(found_student, "%d", convert);
- //printf("Student found at index: %d\n", found_student);
- //read_data(s, NULL, argv[1], found_student);
- printf("%s was found at index %d", student_Name, found_student+1);
- }
- printf("\nWould you like to contiune (y/n): ");
- scanf("%c", &ans);
- } while (ans != 'n');
- if (lowest_score(s, size)) {
- }
- else{
- printf("Could not find: ");
- }
- if (highest_score(s, size)) {
- }
- else{
- printf("Cound not find: ");
- }
- free(s);
- } // end main
- int read_data(Student *s, int size, const char *fileName, int index){
- // filePtr
- FILE *fileName_Open;
- // hold data from read
- int data = 0;
- // Check to see if file pointer is null
- if((fileName_Open = fopen(fileName, "rb+")) == NULL){
- printf("\nCannot open file\n");
- return -1;
- }
- //Go to position in file given by user
- fseek(fileName_Open, (index - 1) * sizeof(Student), SEEK_SET);
- // Read from pointer position which is set from index
- data = fread(s, sizeof(Student), size, fileName_Open);
- //free(fileName_Open);
- fclose(fileName_Open);
- return data;
- } // end of read_data
- void print_data(Student *s, int size){
- int i, j = 0;
- //Print the records
- printf("Printing %d records\n", size);
- for (i = 0; i < size; i++) {
- printf("Student Name: %s\n", s[i].name);
- for (j = 0; j < NUM_SCORES; j++) {
- printf("Score %d: %d\n",j+1, s[i].scores[j]);
- }
- }
- }
- void selection_sort(Student *s, int size){
- // Variable to hold value
- Student temp;
- // for loop variable
- int i = 0;
- int j = 0;
- //Variable to hold smallest
- int smallest = 0;
- // Loop through size value given by user
- for(i = 0; i < size - 1; i++){
- smallest = i;
- // strcmp name with ASCII characters
- for(j = i + 1; j < size; j++){
- if(strcmp(s[j].name, s[smallest].name) < 0){
- smallest = j;
- } // end if
- } // end inner
- if (smallest != i) {
- temp = s[i];
- s[i] = s[smallest];
- s[smallest] = temp;
- } // end if
- }// end outer loop
- } // end selection
- int binary_search(Student *s, char *target, int startIndex, int endIndex){
- int midIndex = 0;
- if(startIndex > endIndex){
- return -1;
- }
- midIndex = (startIndex + endIndex) / 2;
- if(strcmp(target, s[midIndex].name) < 0){
- return binary_search(s, target, startIndex, midIndex-1);
- }
- else if (strcmp(target, s[midIndex].name ) > 0){
- return binary_search(s, target, midIndex+1, endIndex);
- }
- else if (strcmp(target, s[midIndex].name ) == 0){
- return midIndex;
- }
- return 0;
- }
- int lowest_score(Student *s, int size){
- //Variable to hold lo5west value
- int lowest = 101;
- int indexName = -1;
- int indexScore = -1;
- // For loop
- int i = 0;
- int j = 0;
- for (i = 0; i < size; i++) {
- for(j = 0; j < NUM_SCORES; j++){
- if(s[i].scores[j] < lowest){
- lowest = s[i].scores[j];
- indexName = i;
- indexScore = j;
- }
- }
- }
- FILE *fp;
- // Check to see if file pointer is null
- if((fp = fopen("stats.txt", "w+")) == NULL){
- printf("\nCannot open file\n");
- return -1;
- }
- else{
- fprintf(fp, "\n%s had the lowest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
- }
- printf("\n%s had the lowest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
- return 1;
- }
- int highest_score(Student *s, int size){
- //Variable to hold lo5west value
- int lowest = 0;
- int indexName = -1;
- int indexScore = -1;
- // For loop
- int i = 0;
- int j = 0;
- for (i = 0; i < size; i++) {
- for(j = 0; j < NUM_SCORES; j++){
- if(s[i].scores[j] > lowest){
- lowest = s[i].scores[j];
- indexName = i;
- indexScore = j;
- }
- }
- }
- FILE *fp;
- // Check to see if file pointer is null
- if((fp = fopen("stats.txt", "a+")) == NULL){
- printf("\nCannot open file\n");
- return -1;
- }
- else{
- fprintf(fp, "\n%s had the lowest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
- }
- printf("\n%s had the lowest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
- return 1;
- printf("\n%s had the highest score with %d", s[indexName].name, s[indexName].scores[indexScore]);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement