Thread: Compiles fine, freezes when Run?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    11

    Compiles fine, freezes when Run?

    I'm working on an assignment and ran into some problems about halfway through my code, so I decided to start over going a little slower now. However now I've written the first two functions and they look to me like they should work and they compile with no errors. When I hit run though, it doesn't do anything at all and I have to manually end the run process. What it should do is open a data file (containing 56 numbers - 7 numbers in 8 rows) and read each value into an array then print out the array (to check to see if the data file was read correctly). Is there something I've forgotten or maybe written wrong that is causing this to lock up (by lock up I don't mean the screen actually freezes, just the program sits and does nothing)?

    Code:
    #include<stdio.h>
    #define FILENAME "assign8in.txt"
    #define nCols 7
    #define nRows 8
    
    int read2D(int array[][nCols], int numRows, int numCols);
    void print2D(int array[][nCols]);
    
    int main()
    {
    	int array[nRows][nCols];
    	int numRows, numCols;
    	int colNum, rowNum;
    
    	numRows == nRows;
    	numCols == nCols;
    	
    	read2D(array, numRows, numCols);
    	print2D(array);
    		
    	return 0;
    }
    
    //This function will read the data file into a 2D array.
    
    int read2D(int array[nRows][nCols], int numRows, int numCols)
    {
     	int i, j;
     	
    	FILE *assign8in;
    	assign8in = fopen(FILENAME, "r");
       if (assign8in == NULL)
       	printf("Error Opening input file \n");
    	else
    	{
    		for (i=1; i<=numRows; i++)
    		{
    			for (j=1; j<=numCols; j++)
    			{
    				fscanf(assign8in, "%d", &array[i][j]);
    			}	
    		}
    	}
    	
    	return array[numRows][numCols];
    	
    }
    
    //  This is a temporary function that will print out the
    //  array filled by function read2D to check if the
    //  data was read correctly.  To use this function remove
    //  the /* and */ around the function and remove the // before
    //  the print2D call in function main and its declaration
    //  at the beginning of the code.
    
    void print2D(int array[nRows][nCols])
    {
    	int i, j;
    	
    	for (i=1; i<=nRows; i++)
    	{
    		for (j=1; j<=nCols; j++)
    		{
    			printf("%3d ", array[i][j]);
    		}
    		printf("\n");
    	}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... I find your array indexing suspicious. For an array of size n, valid indices are in the range [0,n). You appear to be accessing the array at index n, thus there is an array out of bounds error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    These lines do nothing, and your compiler should have warned you about them if you'd turned on warnings.
    Code:
    	numRows == nRows;
    	numCols == nCols;
    You probably meant to use assignment, a single '='.

    Code:
    assign8in = fopen(FILENAME, "r");
    You never close that file. This could cause problems later on.

    Code:
    		for (i=1; i<=numRows; i++)
    		{
    			for (j=1; j<=numCols; j++)
    			{
    				fscanf(assign8in, "&#37;d", &array[i][j]);
    			}	
    		}
    Remember, for an array
    Code:
    type array[N];
    the elements you can access are [0]..[N-1]. You're accessing [1]..[N]. Bad idea. Use the standard form of a for loop:
    Code:
    for(x = 0; x < N; x ++) {}
    and not
    Code:
    for(x = 1; x <= N; x ++) {}
    The same sort of code appears in print2D().

    Code:
    return array[numRows][numCols];
    Again, this is beyond the end of the array. Why are you returning that value anyway? It could be a void function.

    [edit] Too late again. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    11
    Ok so I rewrote everything thusly:

    Code:
    #include <stdio.h>
    #define nRows 8
    #define nCols 7
    #define FILENAME "assign8in.txt"
    
    int read2D(int array[][nCols], int numRows, int numCols);
    void print2D(int array[][nCols], int numRows, int numCols);
    
    int main()
    {
    	int array[nRows][nCols];
    	int numRows, numCols;
    	
    	numRows = nRows;
    	numCols = nCols;
    	
    	array[nRows][nCols] = read2D(array, numRows, numCols);
    	print2D(array, numRows, numCols);
    	
    	return 0;
    }
    	
    int read2D(int array[nRows][nCols], int numRows, int numCols)
    {
    	int i, j;
    	
    	FILE *assign8in;
    	assign8in = fopen(FILENAME, "r");
    	if (assign8in == NULL)
    		printf("Error opening input file.\n");
    	else
    	{
    		for (i=0; i<numRows; i++)
    		{
    			for (j=0; j<numCols; j++)
    			{
    				fscanf(assign8in, "&#37;d", array[i][j]);
    			}
    		}
    	}
    	
    	fclose(assign8in);
    
    	return array[nRows][nCols];	
    		
    }
    
    void print2D(int array[nRows][nCols], int numRows, int numCols)
    {
    	int i, j;
    	
    	for (i=0; i<numRows; i++)
    	{
    		for (j=0; j<numCols; j++)
    		{
    			printf("%3d ", array[i][j]);
    		}
    		printf("\n");
    	}
    }
    now instead of just sitting and doing nothing for forever, it begins and immediately
    gets "----jGRASP cygwin wedge2: process died on signal 11.".

    I made return2D return the array because thats what it said to do in the assignment instructions, I'm not sure why its that way - I figured I would realize that as I got to the rest of the code. I'm using jGRASP and cygwin because thats what we have to use for the class (I've had people tell me its a terrible enviroment and compiler).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Delete this bit
    array[nRows][nCols] =

    and replace this bit with say return 0;
    return array[nRows][nCols];

    You already pass the array by reference anyway, so it gets updated by what you do.
    You can't return a whole array anyway, and all this does it generate out of bound accesses to the array you do have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    11
    Alright, I did what Salem suggested and also tried changing the function read2D() to a void function; however, both ways still give me the "process died on signal 11" error.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I can't believe I missed this . . .
    Code:
    fscanf(assign8in, "&#37;d", array[i][j]);
    You probably want
    Code:
    fscanf(assign8in, "%d", &array[i][j]);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    11
    haha, looks like that was the problem - I could've sworn I already had that ampersand there, if I had noticed it I would've fixed that myself. Anyways its working for what I have now, thanks for the help. If I run into anymore problems I can't figure out I'll just post it here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  2. making 2 processes run simultaniously
    By smegly in forum C Programming
    Replies: 6
    Last Post: 05-14-2004, 11:07 PM
  3. Wont run in X, works fine in alt+F1 terminal
    By zmerlinz in forum Linux Programming
    Replies: 5
    Last Post: 04-28-2004, 11:58 AM
  4. Trying to make rand different per program run
    By Dreamerv3 in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2002, 03:26 AM
  5. one fine day...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 11-15-2001, 06:45 PM