الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

أرجو المساعدة في تصحيح الكود التالي

مغلق
بدأه Al-saidi في 27 نوفمبر 2002 · 3 رد · 347 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم و رحمة الله و بركاته

أرجو المساعدة في تصحيح الكود التالي

و الذي هو عبارة عن برنامج لجمع و طرح و ضرب و قلب و طباعة مصفوفتان من أبعاد

10في 10 و الكود ما فيه أخطاء و لكن عندما

أطبقه يصير infinte loop و ما أدري ما المشكلة

و قد أرفقت الكود كملف

/***************************************************

** Title : This program work with matrix.It can  **

**          add , subtract , multiply , transpose **

**          and display a matrix .                **

** Name : Zayed Amer Salim Al-saidi               **

** I.D : 31035/00                                 **

***************************************************/

#include<stdio.h>

# define MAX_COL   10



 void readMatrix (int x[][MAX_COL],int y[][MAX_COL],int *raw,int *col,int *raw1,int *col1);

 void add        (int M1[][MAX_COL],int M2[][MAX_COL],int result1 [][MAX_COL],int x,int y);

 void subtract   (int M3[][MAX_COL],int M4[][MAX_COL],int result2 [][MAX_COL],int x,int y);

 void multiply   (int M5[][MAX_COL],int M6[][MAX_COL],int result3 [][MAX_COL],int x,int y,int w,int z);

 void transpose  (int M7[][MAX_COL],int M8[][MAX_COL],int result4 [][MAX_COL],int x,int y);

 void display    (int M9[][MAX_COL],int M10[][MAX_COL],int result5 [][MAX_COL],int x,int y);



 void main (void )

 {

     int matrx1[10][MAX_COL] ,matrx2[10][MAX_COL],reslut[10][MAX_COL],raw,col,n,raw1,col1;

     char test= 'a';





     printf(" This programe play with matrixn ");

     do

      {

                  fflush(stdin);

      printf(" Choose one option from list :n");

      printf("     1- Add         2- Subtract n");

      printf("     3- Multiplay   4- Transpoe n");

      printf("     5- Display     6- Quit     n");

      PP:

      printf(" Enter the number you want : ");

      n= getchar();

      if (n >6 && n <=0 )

        {

        printf("n You enter number not exist!!n");

        goto PP;

        }

      XX:

      readMatrix(matrx1,matrx2,&raw,&col,&raw1,&col1);

      switch (n)

      {   case 1: if ( raw1 !=raw || col1!=col)

           {

           printf( "n The raws and colums in two matrixes must be equal ");

           goto XX;

           }

            add (matrx1,matrx2,reslut,raw,col);

            break;

         case 2: if ( raw1 !=raw || col1!=col)

           {

           printf( "n The raws and colums in two matrixes must be equal ");

           goto XX;

           }

           subtract (matrx1,matrx2,reslut,raw,col);

            break;

         case 3: if ( col != raw1 )

           {

           printf( "n The raws and colums in two matrixes must be equal ");

           goto XX;

           }

            multiply (matrx1,matrx2,reslut,raw,col,raw1,col1);

            break;

         case 4: transpose (matrx1,matrx2,reslut,raw,col);

            break;

         case 5: display (matrx1,matrx2,reslut,raw,col);

            break;

         case 6: test = 'q';

            break;

      }

      }

     while ( test!= 'q');

 }

/********************** The functions list ***********************************/

/*********************** Read Matrix functin *********************************/

  void readMatrix (int x[][MAX_COL],int y[][MAX_COL],int *raw,int *col,int *raw1,int *col1)

  {

  int n,i,j,n1;

  FILE *file1,*file2;

  char a[30], b[30];



  printf(" From where you want to read matrixes : n");

  printf("   1- file        2- keyboard     3- a both   n");

  ZZ:

  printf(" You choose : ");

  scanf ("%d" ,&n);

  if (n >6 && n <=0 )

        {

        printf("n You enter number not exist!!n");

        goto ZZ;

        }

  printf("n be sure that raw and colume be equal in two matrixes in Add and Subtract n");

  printf("n in Muliiplay be sure that raws in first matrix are equal the colume in second martixn");

  switch (n)

    {

    case 1 : XX:

       printf(" Please type the file name of first matrix : ");

       gets(a);

       file1=fopen(a,"r");

       fscanf(file1,"%d %d",raw,col);

       if ( *raw >10 || *col >10)

     	 {

         printf("n Please check that number of colum or raw not greater than 10 n");

         goto XX;

     	 }

       for (i=0; i< *raw;i++)

     	 {

         for (j=0;j< *col;j++)

        {

         fscanf ( file1,"%d",& x[j]);

        }

     	 }

       X1:

       printf("n Please type the file name of second matrix : ");

       gets(b);

       file2=fopen(b,"r");

       fscanf(file1,"%d %d",raw1,col1);

       if ( *raw >10 || *col >10)

     	 {

         printf("n Please check that number of colum or raw not greater than 10 n");

         goto X1;

     	 }

       for (i=0; i< *raw1;i++)

     	 {

         for (j=0;j< *col1;j++)

        {

         fscanf ( file2,"%d",& y[j]);

        }

     	 }

       break;

 	 case 2:  XA:

      printf(" For Matrix number 1 ");

      printf("n Be sure that raws and colum not greater than 10 ");

      printf("n number of raws : ");

      scanf ("%d", raw );

      printf("n number of colums : ");

      scanf ("%d",col);

      if ( *raw >10 || *col >10)

     	 {

         printf("n Please check that number of colum or raw not greater than 10 n");

         goto XA;

     	 }

      printf("n Now enter the matrix number by number and raw by raw : ");

      for (i=0; i< *raw1;i++)

     	 {

         for (j=0;j< *col1;j++)

        {

         scanf ( "%d",& x[j]);

        }

     	 }

      XN:

      printf("n For Matrix number 2 ");

      printf("n Be sure that raws and colum not greater than 10 ");

      printf("n number of raws : ");

      scanf ("%d", raw1 );

      printf("n number of colums : ");

      scanf ("%d",col1);

      if ( *raw1 >10 || *col1 >10)

     	 {

         printf("n Please check that number of colum or raw not greater than 10 n");

         goto XN;

     	 }

      printf("n Now enter the matrix number by number and raw by raw : ");

      for (i=0; i< *raw1;i++)

     	 {

         for (j=0;j< *col1;j++)

        {

         scanf ( "%d",& y[j]);

        }

     	 }

      break;

    case 3: printf(" From where you want read martix first: ");

      printf("   1- keybord        2- file            ");

      MM:

      printf(" your choose : ");

      scanf ("%d",& n1 );

      if (n1 == 1)

       {

      XL:

      printf(" For Matrix number 1 ");

      printf("n Be sure that raws and colum not greater than 10 ");

      printf("n number of raws : ");

      scanf ("%d", raw );

      printf("n number of colums : ");

      scanf ("%d",col);

      if ( *raw >10 || *col >10)

     	 {

         printf("n Please check that number of colum or raw not greater than 10 n");

         goto XL;

     	 }

      printf("n Now enter the matrix number by number and raw by raw : ");

      for (i=0; i< *raw1;i++)

     	 {

         for (j=0;j< *col1;j++)

        {

         scanf ( "%d",& x[j]);

        }

     	 }

       printf(" For Matrix number 2 ");

       XP:

       printf("n Please type the file name of second matrix : ");

       gets(b);

       file2=fopen(b,"r");

       fscanf(file1,"%d %d",raw1,col1);

       if ( *raw >10 || *col >10)

     	 {

         printf("n Please check that number of colum or raw not greater than 10 n");

         goto XP;

     	 }

       for (i=0; i< *raw1;i++)

     	 {

         for (j=0;j< *col1;j++)

        {

         fscanf ( file2,"%d",& y[j]);

        }

     	 }

      }

       if (n1 == 2)

      {

       XB:

       printf(" Please type the file name of first matrix : ");

       gets(a);

       file1=fopen(a,"r");

       fscanf(file1,"%d %d",raw,col);

       if ( *raw >10 || *col >10)

     	 {

         printf("n Please check that number of colum or raw not greater than 10 n");

         goto XB;

     	 }

       for (i=0; i< *raw;i++)

     	 {

         for (j=0;j< *col;j++)

        {

         fscanf ( file1,"%d",& x[j]);

        }

     	 }

      XK:

      printf("n For Matrix number 2 ");

      printf("n Be sure that raws and colum not greater than 10 ");

      printf("n number of raws : ");

      scanf ("%d", raw1 );

      printf("n number of colums : ");

      scanf ("%d",col1);

      if ( *raw1 >10 || *col1 >10)

     	 {

         printf("n Please check that number of colum or raw not greater than 10 n");

         goto XK;

     	 }

      printf("n Now enter the matrix number by number and raw by raw : ");

      for (i=0; i< *raw1;i++)

     	 {

         for (j=0;j< *col1;j++)

        {

         scanf ( "%d",& y[j]);

        }

     	 }

      }

     else

   	 {

      printf ("n you enter wrong value ");

      goto MM;

   	 }

     break;

  }

  fclose (file1);

  fclose  (file2);

 }

/*********************** add function ****************************************/

void add(int M1[][MAX_COL],int M2[][MAX_COL],int result1 [][MAX_COL],int x, int y)

  {

   int i ,j;

   char a[30];

   FILE * outfile;

   printf(" Please type the name of out file : ");

   gets(a);

   outfile = fopen (a,"w");

  for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         result1 [j] = M1[j]+ M2[j];

        }

     	 }



    fprintf(outfile," %d %d n",x,y);

    for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         fprintf(outfile," %d ",result1[j]);

        }

     	 }

   fclose (outfile);

  }

/*********************** subtract function ********************************/

void subtract(int M3[][MAX_COL],int M4[][MAX_COL],int result2 [][MAX_COL],int x, int y)

  {

   int i ,j;

   char a[30];

   FILE * outfile;

   printf(" Please type the name of out file : ");

   gets(a);

   outfile = fopen (a,"w");

  for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         result2 [j] = M3[j]- M4[j];

        }

     	 }



    fprintf(outfile," %d %d n",x,y);

 	 for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         fprintf(outfile," %d ",result2[j]);

        }

     	 }

   fclose (outfile);

  }

/*********************************** Multiplay function ***********************/

void multiply  (int M5[][MAX_COL],int M6[][MAX_COL],int result3 [][MAX_COL],int x,int y,int w,int z)

{

  int i, j ,k;

  char a[30];

  FILE * outfile;

  printf(" Please type the name of out file : ");

  gets(a);

  outfile = fopen (a,"w");

  for (i=0;i>x;i++)

	{

   for (j=0;j>z;j++)

   {

   result3[j]=0;

   for (k=0;k>x;k++)

    {

    result3[j]+= M5[k][j]* M6[k];

    }

   }

	}

   fprintf(outfile," %d %d n",x,y);

      for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         fprintf(outfile," %d ",result3[j]);

        }

     	 }

   fclose (outfile);

}



/****************************** Transpose function **************************/

 void transpose  (int M7[][MAX_COL],int M8[][MAX_COL],int result4 [][MAX_COL],int x,int y)

 {

   int i ,j,t;

   char a[30];

   FILE * outfile;

   printf(" Please type the name of out file : ");

   gets(a);

   outfile = fopen (a,"w");



   printf("n Which matrix you want to transpose : ");

   printf("n      1- first        2- second       ");

   ZC:

  printf(" your choose : ");

   scanf (" %d",& t);

   switch (t)

   {

 	 case 1 :



       for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         result4 [j] = M7[j];

        }

     	 }

       break;

    case 2 :

      for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         result4 [j] = M8[j];

        }

     	 }

       break;

    default  :

        printf ("n you enter wrong value ");

        goto ZC;

   }



   printf ("n You will save the result in ' result .txt' file ");

   fprintf(outfile," %d %d n",x,y);

      for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         fprintf(outfile," %d ",result4[j]);

        }

     	 }

   fclose (outfile);

  }

/***************************** display function *******************************/

void display    (int M9[][MAX_COL],int M10[][MAX_COL],int result5 [][MAX_COL],int x,int y)



  {

  int i ,j,t;



   printf("n Which matrix you want to display : ");

   printf("n      1- first        2- second    3- result   ");

   ZC:

   printf(" your choose : ");

   scanf (" %d",& t);

   switch (t)

   {

 	 case 1 :

       printf(" %d %d n",x,y);

       for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

          printf(" %d " ,M9[j]);

        }

        printf("n");

     	 }

       break;

    case 2 : printf(" %d %d n",x,y);

       for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         printf(" %d " ,M10[j]);

        }

         printf("n");

     	 }

       break;

    case 3 :

      printf(" %d %d n",x,y);

      for (i=0; i< x;i++)

     	 {

         for (j=0;j< y;j++)

        {

         printf(" %d " ,result5[j]);

        }

         printf("n");

     	 }

       break;

    default  :

        printf ("n you enter wrong value ");

        goto ZC;

   }

  }



/****************************** the end ***************************************/*

assi5.zip

#2

السلام عليكم ...

أخي أنا لم ارى اي رسالة خطأ لكن الشي الوحيد الذي يحيرني

عند الختيار الثاني تخرج رساله خظأ ( اقصد في وقت عمل البرنامج تخرج)

فهل هذا ما يحدث معك ؟

If you think yourself that you are nobody, you should go somewhere to find yourself

#3

أخي الشبح يحدث لي الذي يحدث لك ربما لأني الarray زائد عن تعريفها ربما لا أدري :confused:

#4

أخي أنا بصرحه صير الكود لدي غير مفهوم بشكل واضح

If you think yourself that you are nobody, you should go somewhere to find yourself

هذا الموضوع مغلق.

مواضيع مشابهة