Please Help me with debugging "Arrays & Functions" in code...

This is a discussion on Please Help me with debugging "Arrays & Functions" in code... within the C Programming forums, part of the General Programming Boards category; I'm having trouble with setting up the Functions for my program. I have created the program without the use of ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    Question Please Help me with debugging "Arrays & Functions" in code...

    I'm having trouble with setting up the Functions for my program. I have created the program without the use of Functions and it works perfect. However, when I added the functions it will either give me 3-4 errors and/or several warnings when compiling. Can someone tell me what I'm doing wrong with my "Functions" code, and what I need to do to fix it? Thanks!

    Here is the code (Note: this code is supposed to use an input file that has a list of names & numberic grades in string format, I'm supposed to extract the data and sort the names and grades from highest grade to lowest as well as changing the numeric grades to alpha grades, i.e., A B C D F. Then I'm supposed to send the results to an output file.) :

    Code:
     Code was modified and updated below...
    Sorry if I didn't use comments in the code, I figured it was simple enough to understand... If you need them, I will provide an update with comments.
    Once again, Thank you for your help!
    Last edited by FYX193; 04-28-2003 at 08:10 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    I'm not sure, but is this the problem (I'm not near a computer that has a C programming compiler):

    Code:
     Code was modified and updated below...
    Last edited by FYX193; 04-28-2003 at 08:11 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    Well I think I've solved it on my own... Trial and Error method.

    Here's my final code, which compiles with "Zero" errors & "Zero" warnings ... (I had to install VS6.0 & SP5 on my work PC to tweak my code a bit till it worked.) :

    Code:
      Code was modified and updated below...
    Last edited by FYX193; 04-28-2003 at 08:09 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    Ok, Now I've compiled it and it says 0 errors, 0 warnings; however, when I Execute the Program it gives me an Error Window displaying the following...

    ____________________________________________

    "Application Error"

    The instruction at "0x00401c5a" referenced memory at "0xccccccc". The memory could not be "written".

    Click on OK to terminate the program
    Click on CANCEL to debug the program
    ____________________________________________

    Can someone please explain what i'm doing wrong with
    my code....

    In order to run the program you will need a text file in
    the C:\TEMP\ path with a filename: namegradein.txt

    the contents of the text file should be the following:
    __________________________________________

    @
    John
    44
    Nick
    23
    Crystal
    87
    Bill
    56
    Joey
    98
    Chris
    78
    @
    __________________________________________

    The output file will be created within the same path...
    and should look something like this:

    NAME GRADE
    ---------------------
    name A
    name C
    etc....

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void main (void)
    No, main returns an int, not void.

    >>char *name[MAXN][MAX]
    This is a 2-d array of pointers, no memory has been allocated for you to store actual text in.

    >>inFile = fopen ("junk1.txt","r");
    You open the say file twice, you don't need to. Just the once will do, if you want to do it in main(), simply pass the FILE* variable to the functions you call that need access to it.

    >>*gradenum[j] = ((*grade[j][0] - 48) * 10) + (*grade[j][1] - 48);
    Are you trying a number convertion here? if so, read this to find a better way.

    You might want to declare your input function like this:
    >>void inputs (int gradenum[MAX], char name[][MAX], char grade[][MAX]) {
    and then invoke it like so
    >>inputs inputs (gradenum, name, grade);

    If you're having problems, try breaking your program down in smaller, more managable parts.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    If you're having problems, try breaking your program down in smaller, more managable parts.
    Think of programming like your leg. If you're having problems with your leg, you want to break it down into smaller manageable parts. Oh sure, it'll hurt like hell, but you'll be better off for doing so in the long run.

    So as they say in show biz, "Break a leg!"



    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    Originally posted by Hammer
    >>void main (void)
    No, main returns an int, not void.

    >>char *name[MAXN][MAX]
    This is a 2-d array of pointers, no memory has been allocated for you to store actual text in.

    >>inFile = fopen ("junk1.txt","r");
    You open the say file twice, you don't need to. Just the once will do, if you want to do it in main(), simply pass the FILE* variable to the functions you call that need access to it.

    >>*gradenum[j] = ((*grade[j][0] - 48) * 10) + (*grade[j][1] - 48);
    Are you trying a number convertion here? if so, read this to find a better way.

    You might want to declare your input function like this:
    >>void inputs (int gradenum[MAX], char name[][MAX], char grade[][MAX]) {
    and then invoke it like so
    >>inputs inputs (gradenum, name, grade);

    If you're having problems, try breaking your program down in smaller, more managable parts.
    I'm well i've done what you've explained, but I'm not quite sure what you mean by:

    >>char *name[MAXN][MAX]
    This is a 2-d array of pointers, no memory has been allocated for you to store actual text in.

    Where & how am I supposed to allocated array memory for text?
    I thought that I accomplished this in the inputs function, where it reads in the data file?

    Another problem I'm having is when I setup the "declare" Funtion line and the function call... When I do it your way, the VS6.0 compiler gives me the following warnings:

    : warning C4047: 'function' : int ** ' differs in levels of indirection from 'int *(*)[10]'

    : warning C4024: 'inputs' : different types for formal and actual parameter 2

    : warning C4048: different array subscripts : 'char *(*)[10]' and 'char *(*)[15][10]'

    : warning C4024: 'inputs' : different types for formal and actual parameter 3

    : warning C4048: different array subscripts : 'char *(*)[10]' and 'char *(*)[15][10]'

    : warning C4024: 'inputs' : different types for formal and actual parameter 4

    Final_ipop.obj - 0 error(s), 6 warning(s)
    Last edited by FYX193; 04-28-2003 at 08:07 PM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    I am familiar with breaking code down into smaller parts, Every thing else will compile and run fine except the " inputs" function, and I believe it has to do with what HAMMER said about allocating memory for the string-list array.

    I dono... My code works fine with out the use of these functions...as is, without allocating memory for the name array.

    The problem is that I'm doing this as an assigned "Final Program" for a C-Programming course at my school. I have to turn in this program tomarrow, and I need it to work. The assignment has the following requirements, and I can create it any way we want as long as the requirements are met. I'm having difficulty with either the Pointers, Arrays, Functions, or a combination of all of them. I have only about 3 months of experience with programming in the C Language, so I'm not quite sure how to fix my code problems. I'm not used to the syntax, but I'm familiar with another language, QuickBasic.

    REQUIREMENTS:
    Write a non-interactive program that will read student names and percentage grades from an input file, sort them (highest grade first) and create an output file as shown in the following example .

    Example :
    Input file : (created by you)
    JONES
    55
    BROWN
    81
    SMITH
    89
    ALAZAR
    93
    FORSTER
    63
    JOPLIN
    67
    GREY
    33
    @

    Output file: (created by your program)
    NAME GRADE
    -----------------
    ALAZAR A
    SMITH B
    BROWN B
    JOPLIN D
    FORSTER D
    JONES F
    GREY F

    You must :

    create the input file using a text editor (before you start writing the program)
    convert numeric grades to letter grades
    make sure that names and grades correspond before and AFTER the sort.
    use functions (your main program should consist of function calls only)

    Be sure to include source, input and output file.
    Last edited by FYX193; 04-28-2003 at 07:33 PM.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    Here's an update to my code with a few things fixed... but it still gives me a Memory Execution Error.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    #define MAX 10 
    #define MAXN 15
    
    int i;
    
    void inputs (FILE *inFile, int *gradenum[MAX], char *name[MAXN][MAX], char *grade[MAXN][MAX]); 
    void bubble (int *gradenum[MAX], char *name[MAXN][MAX], char *grade[MAXN][MAX]); 
    void cpyletters (int *gradenum[MAX], char *grade[MAXN][MAX]); 
    void outputs (char *name[MAXN][MAX], char *grade[MAXN][MAX]); 
    
    void main (void) { 
    	
    	FILE *inFile;
    
    	char *name[MAXN][MAX], *grade[MAXN][MAX];  
    	int *gradenum[MAX]; 
    	
    	inFile = fopen ("c:\\temp\\namegradein.txt","r");
    	
    	if (inFile == NULL) 
    		printf ("Can not read the file!\n"); 
    	else { 
    		inputs (inFile, &gradenum[MAX], &name[MAX], &grade[MAX]);
    		bubble (&gradenum[MAX], &name[MAX], &grade[MAX]);
    		cpyletters (&gradenum[MAX], &grade[MAX]);
    		outputs (&name[MAX], &grade[MAX]);
    	}
    
    }
    
    void inputs (FILE *inFile, int *gradenum[MAX], char *name[MAXN][MAX], char *grade[MAXN][MAX]) {
    	int j;
    
    	i = 0; 
    	fgets (*name[i],MAX,inFile);
    	
    	fscanf (inFile, "%s", *name[0]); 
    	while (*name[i][0] != '@' && i < MAXN -1) { 
    		fscanf (inFile, "%s", *grade[i]); 
    		i++; 
    		fscanf (inFile, "%s", *name[i]); 
    	} 
    	
    	for (j=0; j<i; j++)
    //--I figure if this line of code works in my situation, there's no need to change it...
    		*gradenum[j] = ((*grade[j][0] - 48) * 10) + (*grade[j][1] - 48);
    //--cause it's not that important for the assignment...
    
    	
    	fclose (inFile); 
    
    }	 
    	
    void bubble (int *gradenum[MAX], char *name[MAXN][MAX], char *grade[MAXN][MAX]) {
    	char hGrade[MAXN][MAX], hName[MAXN][MAX]; 
    	int hGradenum, k, m;
    
    	for (k=i; k>=1; k--){
    		for (m=1; m<=k; m++){
    			if (*gradenum[m-1] < *gradenum[m]){
    				hGradenum = *gradenum[m-1];
    				strcpy (hGrade[m], *grade[m-1]);
    				strcpy (hName[m], *name[m-1]);
    				*gradenum[m-1] = *gradenum[m];
    				strcpy (*grade[m-1], *grade[m]);
    				strcpy (*name[m-1], *name[m]);
    				*gradenum[m] = hGradenum;
    				strcpy (*grade[m], hGrade[m]);
    				strcpy (*name[m], hName[m]);
    			}
    		}
    	}
    
    }
    
    void cpyletters (int *gradenum[MAX], char *grade[MAXN][MAX]) {
    	int n;
    
    	for (n=0; n<i; n++)
    		if (*gradenum[n] >= 90 && *gradenum[n] <= 100)
    			strcpy (*grade[n],"A"); 
    		else if (*gradenum[n] >= 80 && *gradenum[n] <= 98)
    			strcpy (*grade[n],"B"); 
    		else if (*gradenum[n] >= 70 && *gradenum[n] <= 79)
    			strcpy (*grade[n],"C");
    		else if (*gradenum[n] >= 60 && *gradenum[n] <= 69)
    			strcpy (*grade[n],"D");
    		else if (*gradenum[n] >= 0 && *gradenum[n] <= 59)
    			strcpy (*grade[n],"F");
    		
    }
    	
    void outputs (char *name[MAXN][MAX], char *grade[MAXN][MAX]) {		
    	FILE *outFile;
    	int z;
    
    	outFile = fopen ("c:\\temp\\namez.txt","w"); 
    	
    	fprintf (outFile, "NAME		GRADE\n"); 
    	fprintf (outFile, "---------------------\n"); 
    	for (z=0; z<i; z++){
    		fprintf (outFile, "%s		%s\n", *name[z],*grade[z]); 
    	}
    	fclose (outFile); 
    	
    }
    Last edited by FYX193; 04-28-2003 at 08:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. backward debugging in Visual Studio??
    By George2 in forum Tech Board
    Replies: 12
    Last Post: 11-05-2006, 01:17 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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