Thread: problem with my matrix functions.

  1. #1
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35

    problem with my matrix functions.

    hello, everyone!

    i am working on a program which lets the user make and display a matrix. so far, i'm still on the making a matrix part. but everytime i run the program, after i typed the numbers. the program will crash.
    here's the program:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void make_mat();
    char cmd_str[50]="\0";
    char *array[50];
    char get_cmd();
    int mat_A[50][50], mat_B[50][50], mat_C[50][50];
    int r1, c1, r2, c2, r3, c3, i, j; 
    int loop;
    
    int main()
    {                 
    	while(1)
    	{
    		get_cmd();
    		if (strcmp(array[0], "MAKE")==0)
    				make_mat();
    		else if (strcmp(array[0], "DISP")==0)
    				printf("DISP");
    		else	
    				printf("Invalid Command!");			
    	}
    } 					
    
    /*Get Command Function*/
    char get_cmd()
    {
    	printf("\n>>");
    	gets(cmd_str);
    	
       array[0]=strtok(cmd_str," ");
       if(array[0]==NULL)
       {
       	printf("Error.\n");
       }
       
       for(loop=1;loop<50;loop++)
       	array[loop]=strtok(NULL," ");
       	
    	return (*array[0]);
    }
    
    /*Make Function*/
    void make_mat()
    {
    	if (strcmp(array[1], "A")==0)
    	{
    		printf("Enter rows: ");
          scanf("%d", &r1);
          printf("Enter columns: ");
             		scanf("%d", &c1);
             		for(i=0;i<r1;i++)
             			for(j=0;j<c1;j++)
             			{
             				printf("A[%d][%d] = ", i, j);
             				scanf("%d",&mat_A[i][j]); 
             			}
    	}
    	else if (strcmp(array[1], "B")==0)
    	{
    		printf("Enter rows: ");
          scanf("%d", &r2);
          printf("Enter columns: ");
             		scanf("%d", &c2);
             		for(i=0;i<r2;i++)
             			for(j=0;j<c2;j++)
             			{
             				printf("A[%d][%d] = ", i, j);
             				scanf("%d",&mat_B[i][j]); 
             			}
    	}
    	else if (strcmp(array[1], "C")==0)
    	{
    		printf("Enter rows: ");
          scanf("%d", &r3);
          printf("Enter columns: ");
             		scanf("%d", &c3);
             		for(i=0;i<r3;i++)
             			for(j=0;j<c3;j++)
             			{
             				printf("A[%d][%d] = ", i, j);
             				scanf("%d",&mat_C[i][j]); 
             			}
    	}
    }
    and here's a sample output:
    Code:
    >>MAKE A
    Enter row: 2
    Enter column: 2
    A[0][0] = 1
    A[0][1] = 2
    A[1][0] = 3
    A[1][1] = 4
    
    >>Error.
    then it crashes.
    what do you think is the problem?

  2. #2
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Nyekknyakk,

    I think we described it as 'gets' reading a leftover newline in the input and returning nothing from the user's prompted response. I.e. line 57 when the integer is read from the input stream, the newline is left waiting to removed from the queue.... Then the loop in main directs the program (get_cmd) to read the next input from the user, which returns nothing. get_cmd interprets this as an error, thus the message, and the loop using strtok makes a mess trying to tokenize a string that is null -- the stack becomes segmented so when you make reference to array the segmentation fault is raised.

    Try moving that loop for tokenizing so that it is not run when get_cmd decides it's got an error. Or perhaps avoid tokenizing the string entirely if it is empty. strtok and gets are both somewhat misfits...I suggest checking out the man page for gets: GETS and the man page for strtok: STRTOK. They're both considered a little bit unstable.

    Nice beginner material Nyekknyakk -- keep it up.

    Best Regards,

    New Ink -- Henry
    Last edited by new_ink2001; 08-18-2010 at 11:34 PM. Reason: typographical

  3. #3
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    Hi, new ink!

    can you give an example on how to do it properly? i've got no idea how to avoid tokenizing the string but i know i must do it because i have to seperate the string into words.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you need to stop posting topics all over the place about the same thing with nothing really fixed. Solve one problem, learn from it, post a new one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    right. i'm really sorry about this. because after posting my first question here, i didn't see the thread so i made a new one. i was going to delete it but i don't know how.
    i'm sorry.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No harm done so long as you know that it's a bad thing.
    You can search for your own threads, too, you know. Just search for topics started by your own username. Consider it a tip. It might come in handy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Nyekknyakk,

    Just like when you used strcmp to compare the contents of array with a non-empty string, test against an empty string. Something like this prevented the crash you were describing:

    Code:
    char get_cmd()
    {
    	printf("\n>>");
    	gets(cmd_str);
    	
    	if( 0 != strcmp( cmd_str, "" ) )
    	{
          // do the command line tokenizing as before...now that it is non-empty
        
       	}
       	else
       	{
       	     	printf("Error.\n");
        }
        
    	return (*array[0]);
    }
    You may have to change the contents of your array after processing a command in order to avoid re-reading them, but that's no big deal. You're getting the hang of that I noticed.

    I also noticed that you had been pressing the thread a little too, in one of them some one mentioned that all of your data members are global. That was a big no-no when I was in school -- but people did it anyway. In fact, it is analogous to what strtok does to provide the sub-strings...a little un-stable. It becomes an issue as the program gets larger, because (the most notable issue I can recall) is the name space becomes convoluted. That is, variable names that may be involved with a function are occupied with useful data prior to starting the function (i.e. entering the function's scope), I'm confident that side-effects are also an issue....

    Please post your results with the suggestion above, I am eager to see them.

    Best Regards,

    New Ink -- Henry

  8. #8
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    Hi, New Ink!

    I can't figure out how to change the contents of my array after processing a command.
    But I've already done your suggestions, it still has the same problem. It still crashes like before.
    Last edited by nyekknyakk; 08-20-2010 at 08:56 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Get rid of gets!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  2. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. Overloaded [] with matrix class problem
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 02:52 PM