I need help, and you guys are the only ones I can think of

This is a discussion on I need help, and you guys are the only ones I can think of within the C Programming forums, part of the General Programming Boards category; Need help with my code, I wrote comments and such on what I need and stuff.... I really need help ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    I need help, and you guys are the only ones I can think of

    Need help with my code, I wrote comments and such on what I need and stuff.... I really need help badly, please become my hero.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    typedef struct {
    	char name[100];
    	int scores[7];
    }STUDENT;
    
    // function declarations
    void getFileName(char *filename);
    int howManyLines(char *filename);
    void readFileIntoArray(char *filename, STUDENT students[], int *size);
    void getStats(STUDENT students[], int size);
    
    int main(void) {
    	char filename[30];
    	STUDENT *students;
    	int size;
    
    	getFileName(filename); // finished
    
    	students = malloc( (howManyLines(filename)+1) * sizeof(STUDENT)); // finished
    
    	readFileIntoArray(filename, students, &size); // i have to finish this function
    
    	getStats(students, size); // i have to finish this function
    
    	return EXIT_SUCCESS;
    }
    
    /*
     * This function goes through the student array and prints out who scored highest
     * on each of the seven assignments. If there is a tie, print out all the students
     * who tied for the highest score.
     *
     * size is the size of the array.
     *
     * You can use fmax in the math.h library if you want.
     */
    
    // this function i do not have a clue on how to do this, but know what i want from it.... i need help
    void getStats(STUDENT students[], int size){
    	int i, hold_score, same_score, x;
    	char hold_name, same_name1, same_name2;
    	i = 0;
    	for(i = 0; i < size; i++)
    	{
    		if(students[i].scores > students[i-1].scores)
    		{
    			x = i;
    			hold_score = students[i].scores;
    			strcpy(hold_name, students[i].name);
    			printf("The student named, &#37;s, had the highest score on assignment%d, which was a %d", students[i].name, x, hold_score);
    		}
    		else if( (hold_score == students[i-1].scores) && (hold_name != students[i-1].name) )
    		{
    			same_score = students[i].scores;
    			strcpy(same_name1, hold_name);
    			strcpy(same_name2, students[i-1].name);
    			printf("Both these students, %s and %s, both had the highest score on assignment%d, which was a %d", hold_name, same_name1, x, same_score);
    		}
    	}
    }
    
    
    /*
     * opens file named filename
     *
     * read from the file while fscanf returns something != EOF  <- end of file
     *
     * for each struct in the array:
     *   reads students names into struct
     *   reads the 7 scores into struct
     * counts the structs and puts the result into *size.
     *
     * The lines in the file come in this form:
     * Wallace, Christine (userid) | studentid int1 int2 int3 int4 int5 int6 int7
     *
     * for extra credit your code should also handle lines like this:
     * Mc Donald, Ronald (userid) | studentid int1 int2 int3 int4 int5 int6 int7
     *
     * In other words, some last names may have 2 strings. But they will always end in ,
     *
     * In either case, this function concatenates lastName, firstName (use strcat) and
     * stores the resulting string in the name member of the STUDENT struct.
     * Throw away the user id, |, and the student id. Don't store them anywhere in the struct.
     */
    // this function i have no idea how to do it...., but i know what i want...
    void readFileIntoArray(char *filename, STUDENT students[], int *size){
    	FILE *fp;
    	char bam[100];
    	int i, hold_score, same_score, x;
    	char hold_name, same_name1, same_name2;
    	char first, last;
    	fp = fopen(filename, "r");
    	
    	do
    	{
    		fscanf(fp, "%s", )
    	}
    
    	while(fscanf(fp, "%s", bam) != EOF);
    	{
    		printf("%s", bam);
    		fflush(stdout);
    
    	}
    }
    
    /*
     * Gets a valid filename from the user. A valid filename ends in .xls
     * filename will point to the valid filename.
     */
    void getFileName(char *filename){
    	int len;
    
    	do {
    		printf("Where are the grades? Enter a filename that ends with .xls ");
    		fflush(stdout);
    		scanf("%s", filename);
    		len = strlen(filename);
    	}
    	while(filename[len-1] != 's' || filename[len-2] != 'l' ||
    			filename[len-3] != 'x' || filename[len-4] != '.');
    }
    
    /*
     * Counts the newlines but consecutive newlines will not be counted.
     */
    int howManyLines(char *filename){
    	FILE *fp;
    	char c;
    	int count=0, newLineflag=0;
    	fp = fopen(filename, "r");
    	while(fscanf(fp, "%c", &c) != EOF){
    		if(c=='\n' && !newLineflag){
    			count++;
    			newLineflag = 1;
    		}
    		else if (c != '\n')
    			newLineflag = 0;
    
    	}
    	fclose(fp);
    	return count;
    
    }
    Last edited by dlwlsdn; 12-05-2008 at 08:19 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    here's an idea for void getStats(STUDENT students[], int size){: make a 100 element char array and set the first character of every element to 0. Then assign each name to the element number corresponding to their score (actually, instead you could use the number of the student from the struct array and put that in an an int array), checking to see if the first character is 0 (or a 3D array to hold multiple student numbers for one score in a tie) and add to the element.

    I'd go with the 3D int array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Hmm, 3D array... I haven't learned 3D array yet...

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sorry, I was just going to go back and edit that. I would mean 2D array, so:

    Code:
    ray[75]={4,5}
    if you get my drift
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by MK27 View Post
    Sorry, I was just going to go back and edit that. I would mean 2D array, so:

    Code:
    ray[75]={4,5}
    if you get my drift

    Using 2D array, hmm... I don't quite understand, let me re-read the post.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, I thought you wanted an idea for void getStats(STUDENT students[], int size){, because it certainly won't work the way it is now. Although you could work it out that way, I think, if you went thru 2 at a time and compared the highest of the two to "hold_score". Perhaps that is a more sane idea than the 2D int array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21