Thread: validation of 2d array

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    59

    validation of 2d array

    So I have written a program to compute the determinant of a 3x3 matrix which is read from a file....

    however, I think I am missing some important validation. I get ridiculous results or infinite loops for example, when 1 entry of the 3x3 is missing, or even when 1 entry is replaced with a letter. Is there any way I can validate this before I put it in the array, or after it is in the array? Perhaps using a while loop?

    And while someone is reading it, does the rest of the coding work well for what I am doing? It compiles and works fine for actual 3x3 results.

    Thanks (P.S.-Only my second program so please be gentle...

    Code:
    /**********************************************
    ****Include all necessary standard libraries***
    **********************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /**********************************
    ****Define the external function***
    **********************************/
    
    double CalcDet(double matrix[][]);
    
    /***********************
    ****Start the program***
    ***********************/
    
    int main(void)	
    {
        int         i, j;
        FILE*       input;
    	double		matrix[3][3], line1, line2, line3, det;
    	const char  input_fn[]="matrix.dat";
    	
    	input=fopen("matrix.dat","r");
    	
    	if (input==(FILE*) NULL)
        {
    	    printf("Cannot open required file!\n");
            printf("Please ensure your matrix file\n");
    	    printf("is in the correct directory\n");
    	    exit(EXIT_FAILURE);
    	}
    	else
    	{
    	for(i=0; i<3; i++)
        {
            for(j=0; j<3; j++)
    		{
    			fscanf(input, "%lg", &matrix[i][j]);
    		}
    	}
    	fclose(input);
    	det = CalcDet(matrix);
    	printf("The matrix entered and it's determinant is\n");
    	printf("|%.1lg %.1lg %.1lg|\n", matrix[0][0], matrix[0][1], matrix[0][2]);
    	printf("|%.1lg %.1lg %.1lg| = &.6lg\n", matrix[1][0], matrix[1][1], matrix[1][2], det);
    	printf("|%.1lg %.1lg %.1lg|\n", matrix[2][0], matrix[2][1], matrix[2][2]);
    	}
    	return(0);
    	}
    	
    
    double CalcDet(double matrix[3][3])
    		{
    			double  det, line1, line2, line3;
          
    			line1 = ((matrix[0][0]) * ((matrix[1][1])*(matrix[2][2]) - (matrix[1][2])*(matrix[2][1])));
    			line2 = ((matrix[0][1]) * ((matrix[1][0])*(matrix[2][2]) - (matrix[1][2])*(matrix[2][0])));
    			line3 = ((matrix[0][2]) * ((matrix[1][0])*(matrix[2][1]) - (matrix[1][1])*(matrix[2][0])));
    			
    			det = line1 - line2 + line3;
    	
    			return(det);
    		}

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you want to validate, you have 2 options (maybe more, but these are the practical options).

    1) Read into a string and validate yourself
    2) check the return value of fscanf(). See here: fscanf [C++ Reference]
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by browser
    however, I think I am missing some important validation. I get ridiculous results or infinite loops for example, when 1 entry of the 3x3 is missing, or even when 1 entry is replaced with a letter. Is there any way I can validate this before I put it in the array, or after it is in the array? Perhaps using a while loop?
    You mean when your data file is corrupt you get unexpected results? How shocking!

    Why don't you just read a whole line at a time and then pull each line apart?
    Code:
    for( x = 0; x < 3; x++ )
        if( fgets( buf, BUFSIZ, fp ) != NULL )
        {
            if( sscanf( buf, "%d %d %d", &a[x][0], &a[x][1], &a[x][2] ) == 3 )
                /*...yay...*/
            else
                /*...fail...*/
        }
        else
            /*...fail...*/
    Edit: Dang phone calls.


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

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Could you explain the following construct a bit further please (and I have google'd it....)

    Code:
    if( fgets( buf, BUFSIZ, fp )
    I understand buf has to be a character but what about BUFSIZ and would fp be my input file?

    My rough attempt to understand it and apply it to my own code leads to the following...is it correct?

    Code:
    for( i = 0; i < 3; i++ )
        if( fgets( buf, 3, input ) != NULL )
        {
            if( sscanf( buf, "%lg %lg %lg", &matrix[i][0], &matrix[i][1], &matrix[i][2] ) == 3 )
                /*...yay...*/
            else
                /*...fail...*/
        }
        else
            /*...fail...*/

    Also, I read into fscanf and I would expect it to return the value of 9, right?

    Would that be;

    Code:
    while (fscanf (input, "%lg %lg %lg %lg %lg %lg %lg %lg %lg", matrix[0][0], matrix[0][1], matrix[0][2], .....etc) == 9)
    I suppose fgets has the advantage of validating that each line has 3 values rather than just 9 values in total...are there any other advantages? Do I need to worry about EOFs?

    Will fgets also get rid of any non numeric inputs and return invalid input?

    Thanks

    edit: Also came across this....is it helpful to me?

    Code:
    if( scanf("%[0987654321.-]s", buf) == 1 )
    {
          if( sanit_check( buf ) ) 
          {
          /* good input */
          }
          else
          {
          /* invalid input */
          }
    }
    else 
    {
    /* invalid input */
    }
    Last edited by browser; 11-17-2009 at 08:35 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by browser View Post
    Could you explain the following construct a bit further please (and I have google'd it....)
    I find this hard to believe. You can't find information on how fgets works? Come on! The first google hit explains it:

    fgets - C++ Reference

    Or just use the man pages if you have them:

    fgets

    fgets( yourbuffergoeshere, yourbuffersizegoeshere, yourfilepointergoeshere )


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

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Right, so I tried the validation code below and it's not really working....

    Any ideas

    Code:
    /******************************************************************
    ***Program to find the determinant of a 3 x 3 matrix from a file***
    ******************************************************************/
    
    /**********************************************
    ****Include all necessary standard libraries***
    **********************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /**********************************
    ****Declare the external function***
    **********************************/
    
    double CalcDet(double matrix[][]);
    
    /***********************
    ****Start the program***
    ***********************/
    
    int main(void)	
    {
        /*Define variables*/
    	int         i, j, tmp;  
        FILE*       input;
    	double		matrix[3][3], line1, line2, line3, det;
    	const char  input_fn[]="matrix.dat";
    	
    	/*Open file*/
    	input=fopen("matrix.dat","r");
    	
    	/*Verify file*/
    	if (input==(FILE*) NULL)
        {
    	    printf("Cannot open required file!\n");
            printf("Please ensure your matrix file\n");
    	    printf("is in the correct directory\n");
    	    exit(EXIT_FAILURE);
    	}
    	else
    	{
    	
    	/*test code*/
    	while(fscanf(input, "%.1f %.1lf %.1lf %.1lf %.1lf %.1lf %.1lf .1%lf %.1lf", matrix[0][0], matrix[0][1], matrix[0][2], matrix[1][0], matrix[1][1], matrix[1][2], matrix[2][0], matrix[2][1], matrix[2][2], &tmp) == 9);
    	{
    	
    	/*Write matrix from matrix.dat to an array*/
    	for(i=0; i<3; i++)
        {
            for(j=0; j<3; j++)
    		{
    			fscanf(input, "%lf", &matrix[i][j]);
    		}
    	}
    	/*Close file*/
    	fclose(input);
    	
    	/*Print the stored matrix and it's determinant*/
    	det = CalcDet(matrix);
    	printf("The matrix entered and it's determinant is\n");
    	printf("|%.1lf %.1lf %.1lf|\n", matrix[0][0], matrix[0][1], matrix[0][2]);
    	printf("|%.1lf %.1lf %.1lf| = %.6lf\n", matrix[1][0], matrix[1][1], matrix[1][2], det);
    	printf("|%.1lf %.1lf %.1lf|\n", matrix[2][0], matrix[2][1], matrix[2][2]);
    	}
    	} 
    	return(0);
    }
    	
    /***********************
    ****End of program******
    ***********************/
    
    /*********************************
    ****Define external function, ****
    ****it returns the determinant****
    *********************************/
    double CalcDet(double matrix[3][3])
    {
    	double  det, line1, line2, line3;
          
    	line1 = ((matrix[0][0]) * ((matrix[1][1])*(matrix[2][2]) - (matrix[1][2])*(matrix[2][1])));
    	line2 = ((matrix[0][1]) * ((matrix[1][0])*(matrix[2][2]) - (matrix[1][2])*(matrix[2][0])));
    	line3 = ((matrix[0][2]) * ((matrix[1][0])*(matrix[2][1]) - (matrix[1][1])*(matrix[2][0])));
    	
    	det = line1 - line2 + line3;
    	
    	return(det);
    }
    Last edited by browser; 11-18-2009 at 08:34 AM.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Code:
    while(fscanf(input, "%.1f %.1lf %.1lf %.1lf %.1lf %.1lf %.1lf .1%lf %.1lf", matrix[0][0], matrix[0][1], matrix[0][2], matrix[1][0], matrix[1][1], matrix[1][2], matrix[2][0], matrix[2][1], matrix[2][2], &tmp) == 9);
    	{
    .
    .
    }
    Seems like you are having trouble with an extra character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM