Thread: please find a bug !

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    wuhan ,hubei ,china
    Posts
    7

    please find a bug !

    Code:
     /**********************************************
    **		@copyright reserved by the author
    **author:			xuxuelong
    **data:				20009/12/18
    **IDE:				visula c++ 6.0
    **major:			CS-software engineering-08
    **email address:    [email protected]
    **attention:		if you find a bug in the
    **..........		operation ,just email me!
    **********************************************/
    #include  <stdio.h>
    #include  <stdlib.h>
    #include  <windows.h>
    #define Max     100
    #define OK      1
    #define ERROR   0
    /********************/
    typedef struct node 
    {
    	int row;
    	int colum;
    	int value;
    }data;
    typedef struct _matrix
    {
    	int row_size;
    	int colum_size;
    	int non_zero_amount;
    	data  matrix[Max+1];
    }tri_matrix;
    /********subfunctions**********/
    /*******function prototypes****/
    int creat_matrix(tri_matrix &T)
    {//creat a tri_matrix T with row_temp rows and colum_temp colums
    	/*input how many rows matrixs have*/
    	printf("The matrix's row size :");
    	scanf("%d",&T.row_size);
    	/*input how many colums matrixs have*/
    	printf("The matrix's colum size:");
    	scanf("%d",&T.colum_size);
    	printf("Input the number of non_zero amount :");
    	scanf("%d",&T.non_zero_amount);
    	printf("/*****************************************/\n");
    	for(int count=1;count<=T.non_zero_amount;count++)
    	{
    		do
    		{//insure the row mark is valid 
    			printf("Input the mark of <row>:");
    			scanf("%d",&T.matrix[count].row);
    		}while(T.matrix[count].row>T.row_size);
    		do
    		{//insure the colum mark is valid
    			printf("Input the mark of <colum>:");
    			scanf("%d",&T.matrix[count].colum);
    		}while(T.matrix[count].colum>T.colum_size);
    		/*input the value of the matrix*/
    		do
    		{//insure the value is non zero
    			printf("Input the <value> of the node:");
    			scanf("%d",&T.matrix[count].value);
    		}while(T.matrix[count].value==0);
    		printf("/************************************/\n");
    	}//for
    	return OK;
    }//creat_matrix
    
    void print_matrix(tri_matrix T)
    {//print out the info of the matrix
    	printf("\t\tRow\t\t\tColum\t\t\tValue\n");
    	printf("\t\t-------------------------------------------------\n");
    	for(int count=1;count<=T.non_zero_amount;count++)
    	{
    		printf("\t\t%d\t\t\t%d\t\t\t%d\n",T.matrix[count].row,T.matrix[count].colum,T.matrix[count].value);
    		printf("\t\t-------------------------------------------------\n");
    		Sleep(1000);
    
    	}
    }//print_matrix
    
    int add_matrix(tri_matrix one,tri_matrix two, tri_matrix &three)
    {//add matrix one and two then put the result in three	
    	int i,j,k;//count in the loop
    	for(i=1;i<=one.non_zero_amount;i++)
    	{//assign one's info to three
    		three.matrix[i].row=one.matrix[i].row;
    		three.matrix[i].colum=one.matrix[i].colum;
    		three.matrix[i].value=one.matrix[i].value;
    	}
    	three.non_zero_amount=i-1;
    	/*insert two's info to three*/
    	for(j=1;j<=two.non_zero_amount;j++)
    	{//insert  one by one of the  two's nodes
    		for(i=1;i<=three.non_zero_amount;i++)
    		{
    			if(two.matrix[j].row<three.matrix[i].row)
    			{//node in two insert into three
    				for(k=three.non_zero_amount;k>=i;k--)
    				{//backoff the elements in three
    					three.matrix[k+1].row=three.matrix[k].row;
    					three.matrix[k+1].colum=three.matrix[k].colum;
    					three.matrix[k+1].value=three.matrix[k].value;
    				}
    				/*two's insert element replace the origin one*/
    				three.matrix[i].row=two.matrix[j].row;
    				three.matrix[i].colum=two.matrix[j].colum;
    				three.matrix[i].value=two.matrix[j].value;
    				three.non_zero_amount++;
    				break;
    			}
    			else 
    			if(two.matrix[j].row==three.matrix[i].row)
    			{//row equal then compare the colums
    				if(two.matrix[j].colum==three.matrix[i].colum)
    				{//all equal
    					three.matrix[i].row=three.matrix[i].row;
    					three.matrix[i].colum=three.matrix[i].colum;
    					three.matrix[i].value+=two.matrix[j].value;
    					break;//two's element to the next one
    				}
    				else 
    				if(two.matrix[j].colum<three.matrix[i].colum)
    				{
    					for(k=three.non_zero_amount;k>=i;k--)
    					{//backoff the elements in three
    						three.matrix[k+1].row=three.matrix[k].row;
    						three.matrix[k+1].colum=three.matrix[k].colum;
    						three.matrix[k+1].value=three.matrix[k].value;
    					}
    					three.matrix[i].row=two.matrix[j].row;
    					three.matrix[i].colum=two.matrix[j].colum;
    					three.matrix[i].value=two.matrix[j].value;
    					three.non_zero_amount++;
    					break;//two's element to the next one
    				}
    				else
    				{
    					if(i==three.non_zero_amount)
    					{//insert to the last
    						three.matrix[three.non_zero_amount+1].row=two.matrix[j].row;
    						three.matrix[three.non_zero_amount+1].colum=two.matrix[j].colum;
    						three.matrix[three.non_zero_amount+1].value=two.matrix[j].value;
    						three.non_zero_amount++;
    					}
    					else continue;//element in three to the next one
    				}//else
    			}//if
    			else//two.matrix[j].row>three.matrix[i].row
    			{
    				if(i==three.non_zero_amount)
    				{//insert to the last
    					three.matrix[three.non_zero_amount+1].row=two.matrix[j].row;
    					three.matrix[three.non_zero_amount+1].colum=two.matrix[j].colum;
    					three.matrix[three.non_zero_amount+1].value=two.matrix[j].value;
    					three.non_zero_amount++;
    					break;
    				}
    				else continue;
    			}
    		}//inner for
    	}//outer for
    	three.non_zero_amount=three.non_zero_amount;
    	three.row_size=one.row_size;
    	three.colum_size=one.colum_size;	
    	return OK;  
    }//add_matrix
    
    int multiply_matrix(tri_matrix one, tri_matrix two,tri_matrix &three)
    {//multiply one and two ,print the result in three
    	int k=1;//element count in the loop three
    	three.row_size=one.row_size;
    	three.colum_size=two.colum_size;
        for(int i=1;i<=one.non_zero_amount;i++)
    	{
    		for(int j=1;j<=two.non_zero_amount;j++)
    		{
    			if(one.matrix[i].colum==two.matrix[j].row)
    			{//the apply of matrix's attribute
    				three.matrix[k].row=one.matrix[i].row;
    				three.matrix[k].colum=two.matrix[j].colum;
    				three.matrix[k].value=one.matrix[i].value*two.matrix[j].value;
    				k++;
    			}
    		}//for
    		NULL;
    	}//for
    	three.non_zero_amount=--k;
    	return OK;
    }//multiplay_matrix
    
    void swap(int &a,int &b)
    {//exchange the value between a and b
    	int temp;
    	temp=a;
    	a=b;
    	b=temp;
    }//swap
    int trans_matrix(tri_matrix one,tri_matrix &T)
    {//figure out the transfort matrix of one
    	
    	/*sort the colum(bubble sort) and assign to T*/
    	for(int i=1;i<=one.non_zero_amount;i++)
    	{
    		/*initilize to be the minimum one*/
    		T.matrix[i].row=one.matrix[i].colum;
    		T.matrix[i].colum=one.matrix[i].row;
    		T.matrix[i].value=one.matrix[i].value;
    		for(int j=i+1;j<=one.non_zero_amount;j++)
    		{
    			if(T.matrix[i].row<one.matrix[j].colum)
    				continue;
    			if(T.matrix[i].row>one.matrix[j].colum)
    			{
    				swap(T.matrix[i].row,one.matrix[j].colum);
    				swap(T.matrix[i].colum,one.matrix[j].row);
    				swap(T.matrix[i].value,one.matrix[j].value);
    				continue;
    			}else
    			{//T's row equal to one's colum
    				if(T.matrix[i].colum<one.matrix[j].row)
    					continue;
    				if(T.matrix[i].colum>one.matrix[j].row)
    				{//only swap the colum of T
    					swap(T.matrix[i].colum,one.matrix[j].row);
    					swap(T.matrix[i].value,one.matrix[j].value);
    					continue;
    				}
    			}
    		}//for
    		T.matrix[i].row=T.matrix[i].row;
    		T.matrix[i].colum=T.matrix[i].colum;
    		T.matrix[i].value=T.matrix[i].value;
    	}//for
    	T.row_size=one.row_size;
    	T.colum_size=one.colum_size;
    	T.non_zero_amount=one.non_zero_amount;
    	return OK;
    }//trans_matrix
    
    /*******the main function********/
    /*******the main function********/
    int main(void)
    {	
    	/*declare three tri_matrix*/
    	tri_matrix one,two,three;
    	int choice;//as a mark of selection	
    	char flag;//selection mark
    	while(OK)
    	{
    		system("cls");
    		system("color a4");
    		printf("\t$*****************************************************$\n");
    		putchar('\n');
    		printf("\t\t  %c------------Functions------------%c\n",2,2);
    		putchar('\n');	
    		printf("\t\t  %c---------------------------------%c\n",2,2);
    		printf("\t\t  %c    <1>matrix---addition         %c\n",2,2);	
    		printf("\t\t  %c---------------------------------%c\n",2,2);
    		printf("\t\t  %c    <2>matrix---multiplication   %c\n",2,2);	
    		printf("\t\t  %c---------------------------------%c\n",2,2);
    		printf("\t\t  %c    <3>matrix---transformation   %c\n",2,2);	
    		printf("\t\t  %c---------------------------------%c\n",2,2);
    		printf("\t\t  %c    <4>exit-----quit the program %c\n",2,2);	
    		printf("\t\t  %c---------------------------------%c\n",2,2);
    		putchar('\n');
    		printf("\t\t  %c--------Triples Row First--------%c\n",2,2);	
    		printf("\t$*****************************************************$\n");
    		printf("Please choose a choice :(1--4)\n");
    		scanf("%d",&choice);
    		switch(choice)
    		{
    		case 1: printf("\t<addition>\n");
    				/***creat the first one***/
    				printf("\n");
    				printf("Creat the one matrix\n"); 
    				printf("\n");
    				creat_matrix(one);
    				printf("The one tri_matrix is below:\n");
    				print_matrix(one);
    				/*****creat the second one****/
    				printf("Creat the two matrix\n"); 
    				printf("\n");
    				creat_matrix(two);
    				printf("The two tri_matrix is below:\n");
    				print_matrix(two);
    				/*********check*******/
    				while(two.row_size!=one.row_size||two.colum_size!=one.colum_size)
    				{//insure the two matrix are valid to add
    					printf("--------------------------------ERROR---------------------------!\n");
    					printf("!!!The two matrix should have the same row size and colum size!!!\n"); 
    					printf("--------------------------------ERROR---------------------------!\n");
    					printf("Creat the two matrix\n");
    					printf("\n");
    					creat_matrix(two);
    					print_matrix(two);
    				}				
    				/*******add the two matrix********/
    				printf("Add the two matrix\n");
    				putchar('\n');
    				add_matrix(one,two,three);
    				printf("The three matrix is below:\n");
    				Sleep(2000);
    				print_matrix(three);
    				system("pause");
    				break;
    		case 2:printf("\t<multiplication>\n");
    				/***creat the first one***/
    				printf("\n");
    				printf("Creat the one matrix\n"); 
    				printf("\n");
    				creat_matrix(one);
    				printf("The one matrix is below:\n");
    				print_matrix(one);
    				/*****creat the second one****/
    				printf("Creat the two matrix\n"); 
    				printf("\n");
    				creat_matrix(two);
    				printf("The two matrix is below:\n");
    				print_matrix(two);
    				/**********check**********/
    				while(one.colum_size!=two.row_size)
    				{//if not equal ,creat a valid one
    					printf("--------------------------------ERROR---------------------------!\n");
    					printf("!!!!!!!!!!One's colum size should equal to two's row size!!!!!!!!\n"); 
    					printf("--------------------------------ERROR---------------------------!\n");
    					printf("Creat the two matrix\n"); 
    					printf("\n");
    					creat_matrix(two);
    					printf("The two matrix is below:\n");
    					printf("\n");
    					print_matrix(two);
    				}
    				/*******multiply the two matrix********/
    				printf("Multiply the two matrix\n");
    				multiply_matrix(one,two,three);
    				printf("Print the results of multiplication\n");
    				Sleep(2000);
    				print_matrix(three);
    				system("pause");
    				break;
    		case 3:printf("\t<transformation>\n");
    				/***creat a matrix***/
    				printf("\n");
    				printf("Creat a matrix\n"); 
    				printf("\n");
    				creat_matrix(one);
    				printf("The matrix is below:\n");
    				print_matrix(one);
    				/*****transformation of the matrix*****/
    				printf("The transfortid matrix is :\n");
    				Sleep(2000);
    				trans_matrix(one,three);
    				print_matrix(three);
    				system("pause");
    				break;
    		case 4:	printf("Are you sure to quit the system<Y/N>?\n");
    				fflush(stdin);
    				scanf("%c",&flag);
    				if(flag=='y'||flag=='Y'||flag=='\n')
    				{
    					printf("\t\t%c-------%c-------%c-------%c-------%c\n",2,2,2,2,2);
    					putchar('\n');
    					printf("\t\t(^_^)Thanks  For  Using  This!(^_^)\n");	
    					putchar('\n');
    					printf("\t\t%c-------%c-------%c-------%c-------%c\n",2,2,2,2,2);
    					putchar('\n');
    					Sleep(2000);
    					exit(OK);
    				}
    				break;
    		}//switch
    		NULL;
    	}//while
    	return OK;			
    }

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd get rid of fflush(stdin), and rely on you to post up whatever problems you have with your program, for the rest.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by stevesmithx View Post
    LOL.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  3. Can't Find Bug in basic MP3 Sorter
    By trickeastcs in forum C++ Programming
    Replies: 12
    Last Post: 12-14-2007, 05:31 PM
  4. Time for another round of: FIND THAT BUG!
    By Geolingo in forum C++ Programming
    Replies: 0
    Last Post: 10-29-2003, 05:29 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM