Weird Hanging

This is a discussion on Weird Hanging within the C Programming forums, part of the General Programming Boards category; Okay so I made a program that is supposed to find the values of 5 variables given a system of ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    19

    Weird Hanging

    Okay so I made a program that is supposed to find the values of 5 variables given a system of 5 equations. Everything goes through the compiler without error and when i run the program, it outputs the right values. But when I click any button to end the program (I program on a windows so I have to include the system("pause") line in my code so the program doesn't end immediately without me being able to view the printed output), my computer hangs and a window pops up saying that abc.exe has stopped working.

    I don't know if the problem is with my computer or not, but I had someone else try it on their computer and the same thing happened to them Help!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    //function for the input of the coefficients a11 to a55 and b1 to b5
    void get_coefficients(float a[6][7])
    {
    	printf("Enter coefficients a11-a15: ");
    	scanf("%f %f %f %f %f",&a[1][1],&a[1][2],&a[1][3],&a[1][4],&a[1][5]);
    	printf("\nEnter coefficients a21-a25: ");
    	scanf("%f %f %f %f %f",&a[2][1],&a[2][2],&a[2][3],&a[2][4],&a[2][5]);
    	printf("\nEnter coefficients a31-a35: ");
    	scanf("%f %f %f %f %f",&a[3][1],&a[3][2],&a[3][3],&a[3][4],&a[3][5]);
    	printf("\nEnter coefficients a41-a45: ");
    	scanf("%f %f %f %f %f",&a[4][1],&a[4][2],&a[4][3],&a[4][4],&a[4][5]);
    	printf("\nEnter coefficients a51-a55: ");
    	scanf("%f %f %f %f %f",&a[5][1],&a[5][2],&a[5][3],&a[5][4],&a[5][5]);
    	printf("\nEnter constants b1-b5:      ");
    	scanf("%f %f %f %f %f",&a[6][1],&a[6][2],&a[6][3],&a[6][4],&a[6][5]);
    }
    
    
    //function to check the user input.
    void check_input(float a[6][7])
    {
    	a[1][1]=0;
    	a[2][2]=0;
    	a[3][3]=0;
    	a[4][4]=0;
    	a[5][5]=0;
    	
    	while((a[1][1]==0)|(a[2][2]==0)|(a[3][3]==0)|(a[4][4]==0)|(a[5][5]==0))
    	{
    		get_coefficients(a);
    		if((a[1][1]==0)|(a[2][2]==0)|(a[3][3]==0)|(a[4][4]==0)|(a[5][5]==0))
    		{
    			printf("Error: Coefficients aNN cannot be zero for all values of N. Please provide new input.\n");
    		}
    	}
    }
    
    
    //function to print the values of the variables x1 to x5 after each iteration
    void print_variables(int j,float x1,float x2,float x3,float x4,float x5)
    {
    	printf("%d	%.3f	%.3f	%.3f	%.3f	%.3f\n",j,x1,x2,x3,x4,x5);	
    }
    
    
    //function to get the absolute value of non-integer numbers
    float absf(float x)
    {
    	if(x<0)
    	{
    		x=-x;
    	}
    	return x;
    }
    
    
    //function for the actual calculation of the Gauss-Seidel implementation
    void gauss_seidel(float a[6][7])
    {
    	float v[6]={0,0,0,0,0,0};
    	int i,j=0;
    	float x1,x2,x3,x4,x5;
    	float dif1,dif2,dif3,dif4,dif5;
    	
    	print_variables(j,v[1],v[2],v[3],v[4],v[5]);
    	
    	for(i=0,j=1;i<200;i++)//200 iterations
    	{
    	//Variables		//Coefficients and variables
    		v[1]=		(a[6][1]	-a[1][2]*v[2]	-a[1][3]*v[3]	-a[1][4]*v[4]	-a[1][5]*v[5]	)/a[1][1];
    		v[2]=		(a[6][2]	-a[2][1]*v[1]	-a[2][3]*v[3]	-a[2][4]*v[4]	-a[2][5]*v[5]	)/a[2][2];
    		v[3]=		(a[6][3]	-a[3][1]*v[1]	-a[3][2]*v[2]	-a[3][4]*v[4]	-a[3][5]*v[5]	)/a[3][3];
    		v[4]=		(a[6][4]	-a[4][1]*v[1]	-a[4][2]*v[2]	-a[4][3]*v[3]	-a[4][5]*v[5]	)/a[4][4];
    		v[5]=		(a[6][5]	-a[5][1]*v[1]	-a[5][2]*v[2]	-a[5][3]*v[3]	-a[5][4]*v[4]	)/a[5][5];
    		
    		print_variables(j,v[1],v[2],v[3],v[4],v[5]);
    		j++;
    		
    		dif1=v[1]-x1;
    		dif2=v[2]-x2;
    		dif3=v[3]-x3;
    		dif4=v[4]-x4;
    		dif5=v[5]-x5;
    		
    		if((absf(dif1)<0.0001)&&((absf(dif2))<0.0001)&&((absf(dif3))<0.0001)&&((absf(dif4))<0.0001)&&((absf(dif5))<0.0001))
    		{
    			break;
    		}
    
    
    		x1=v[1];
    		x2=v[2];
    		x3=v[3];
    		x4=v[4];
    		x5=v[5]; 
    	}
    }
    
    
    int main()
    {
    	float inputarray[6][7];
    	check_input(inputarray);
    	gauss_seidel(inputarray);
    	system("pause");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The problem is with your program, not the computers. XD

    It's tough to debug a program that isn't all there.

    What I would do is:

    *replace the system pause with getchar() or even
    Code:
    int c;
    while((c=getchar()) != '\n');
    c = getchar();
    That removes multiple chars from the input stream.

    If you call a system function that is external, and your current directory is not in the path, you may crash the program. I had this situation with an earlier compiler that did not hold the console window open when it ended running my program. (Turbo C).

    *check that your warning levels are high in the compiler.
    *if you have any warnings, fix them. Treat them as errors until you KNOW they aren't.

    *remove the call to one function at a time (maybe more). Try and narrow down which lines of code must have the error, roughly.

    *add printf() statements to the suspect area. Verify that your index is staying within the 0 to N-1 legit range of the array, etc. This is VERY likely to be the problem - by far the most popular one.

    You may not like it, but it pays big dividends if you code a bit, and then test it, then code some more, etc. You should never be faced with 100 or more lines of code, in a program that crashes (regardless of first last, or whenever).

    You will need to develop your troubleshooting skill, and it does take kindly to a methodical method - highly recommended.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    Okay so I did what you told me and I narrowed it down to this piece of code:
    Code:
    //function for the input of the coefficients a11 to a55 and b1 to b5
    void get_coefficients(float a[6][7])
    {
    	printf("Enter coefficients a11-a15: ");
    	scanf("%f %f %f %f %f",&a[1][1],&a[1][2],&a[1][3],&a[1][4],&a[1][5]);
    	printf("\nEnter coefficients a21-a25: ");
    	scanf("%f %f %f %f %f",&a[2][1],&a[2][2],&a[2][3],&a[2][4],&a[2][5]);
    	printf("\nEnter coefficients a31-a35: ");
    	scanf("%f %f %f %f %f",&a[3][1],&a[3][2],&a[3][3],&a[3][4],&a[3][5]);
    	printf("\nEnter coefficients a41-a45: ");
    	scanf("%f %f %f %f %f",&a[4][1],&a[4][2],&a[4][3],&a[4][4],&a[4][5]);
    	printf("\nEnter coefficients a51-a55: ");
    	scanf("%f %f %f %f %f",&a[5][1],&a[5][2],&a[5][3],&a[5][4],&a[5][5]);
    	printf("\nEnter constants b1-b5:      ");
    	scanf("%f %f %f %f %f",&a[6][1],&a[6][2],&a[6][3],&a[6][4],&a[6][5]);
    }
    
    
    //function to check the user input.
    void check_input(float a[6][7])
    {
    	a[1][1]=0;
    	a[2][2]=0;
    	a[3][3]=0;
    	a[4][4]=0;
    	a[5][5]=0;
    	
    	while((a[1][1]==0)|(a[2][2]==0)|(a[3][3]==0)|(a[4][4]==0)|(a[5][5]==0))
    	{
    		get_coefficients(a);
    		if((a[1][1]==0)|(a[2][2]==0)|(a[3][3]==0)|(a[4][4]==0)|(a[5][5]==0))
    		{
    			printf("Error: Coefficients aNN cannot be zero for all values of N. Please provide new input.\n");
    		}
    	}
    }
    I still don't know whats wrong with it though So maybe Ill just think of another way to rewrite it.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You found it.
    Code:
    scanf("%f %f %f %f %f",&a[6][1],&a[6][2],&a[6][3],&a[6][4],&a[6][5]);
    a[6][anyNumber] is out of bounds. Arrays in C start at zero index, and stop at N-1. <Hint hint!>

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    Can't believe I missed that! HAHA. Finally finished the program. Thanks I owe you one

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well done. You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program is hanging
    By mramazing in forum Networking/Device Communication
    Replies: 7
    Last Post: 03-18-2009, 05:13 PM
  2. XML writing and bzr hanging
    By dwks in forum Tech Board
    Replies: 3
    Last Post: 06-16-2008, 04:31 PM
  3. pthread hanging
    By munna_dude in forum C Programming
    Replies: 7
    Last Post: 05-17-2007, 09:51 AM
  4. pthread hanging
    By munna_dude in forum C Programming
    Replies: 1
    Last Post: 05-17-2007, 06:07 AM
  5. If statement hanging...
    By tameeyore in forum C Programming
    Replies: 9
    Last Post: 10-08-2004, 11:34 PM

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