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

خطأ في الكود!

مغلق
بدأه طالب برمجة في 15 مايو 2004 · 3 رد · 511 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

عندي حل لمسأله ولكن فيه عده اخطأء بالاضافه ان output ليست بالشكل المطلوب في السؤال

اتمنى المساعد

package assig_2;
import javax.swing.JOptionPane;
import java.util.*;

public class matrix {
public static void main(String[] args) {
 String N = JOptionPane.showInputDialog("what is the size of your matrix?(2-10)");
 int n = Integer.parseInt(N);

 int[][] A = new int[n][n];
 int[][] B = new int[n][n];
 int[][]C;

}
public static void fill(int a[][]) {
 int random = 0;
 int randomN = 0;
 for(int i = 0; i < a.length; i++) {
  for(int j = 0; j < a[0].length; j++) {
   random = (int)(Math.random()*6);
   randomN = (int)(Math.random()*10);

   if(randomN % 3 == 0){
    random = random * -1;
   }
   a[j] = random;

   System.out.print(random + "\t");
   if(j == 2)
    System.out.println("\n");
  }
 }
}
 public static void fillB(int b[][])
 {
   int randomb=0;
   int randomB= 0;
   for(int k=0; k<b.length; k++){
     for(int c = 0; c < b[0].length; c++) {
     randomb = (int)(Math.random()*6);
     randomB = (int)(Math.random()*10);

     if(randomB % 3 == 0){
      randomb = randomb * -1;
     }
     b[k][c] = randomb;

     System.out.print(randomb + "\t");
     if(c == 2)
      System.out.println("\n");
    }
   }
 }// end fillB method
 public static void multiply (int c[][])
{
  int  t = 0;
 int [][] c =  new int [b.length][a[0].length]; //Matrix C = A*B
// Output Matrix C
 for( int i = 0;i<b.length;i++)
 {
    for( int k  = 0;k<a[0].length;k++)
  {
       c[k] = 0;
      for( int j = 0;j<b[0].length;j++)
        {
             c[k] +=b[j]*a[j][k];

         }
System.out.print(c[k]);
System.out.print(" ");
}
  t = t+1;
if( t == 4);
           System.out.println();
}
}
}


    public static void print (){
  System.out.print("Matrix A:")
       fill(A);
  System.out.print("Matrix B:")
      fillB(B);
  System.out.print("Matrix C = A*B:")
       multiply(C);


    }

    }//end class
;
#2

هذا السؤال:

اقتباس
1. Read the size of a matrix n from the keyboard.

2. Create three integer matrices A[n][n], B[n][n] and C[n][n].

3. Use a method to fill up the two arrays A and B by positive and/or negative integer random numbers between 0 and 6 (Hint: to obtain negative numbers, just select randomly any positive integer number and convert it to negative).

4. Use a method to multiply the elements of the two matrices A and B and store the result in matrix C as shown in the example below.

5. Use a method to print the elements of matrix A in a two digit decimal format.

6. Use the same print method to print matrices B and C to the screen in the format shown in the example below.

The Equation and Pseudo Codes for Matrix Multiplication are shown below.

 

  Cij =   Aik * Bkj  (for k = 1 to k = n)

For i = 1 to n

For j = 1 to n do

  Cij = 0

  For k = 1 to n

    Cij = Cij + Aik * Bkj

Running Example of Matrix Multiplication:

What is the size of your Matrix (2-10)? 4

Matrix A:

  -2 5 0 1

  4 1 6 -3

  1 2 -4 5

  -6 0 2 1

 

Matrix B:

  5 -6 1 -4

  0 3 -4 3

  3 1 2 0

  4 -5 1 3

 

Matrix C = A*B:

  -6 -2 -21 10

            26 0 9 -22

  13 -29 -10 17

  -20 -39 -1 27

#3

i don't know ??

but i think i fix the bugs ...

see and tell me

import javax.swing.JOptionPane;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class matrix extends JFrame{

   int[][] A;
   int[][] B;
   int[][]C;


  public matrix()
  {
    String N = JOptionPane.showInputDialog("what is the size of your matrix?(2-10)");
    int n = Integer.parseInt(N);
    A = new int[n][n];
    B = new int[n][n];
    C = new int[n][n];;

    System.out.print("\nMatrix A:\n");
    fillA();
    System.out.print("\nMatrix B:\n");
    fillB();
    System.out.print("\nMatrix C = A*B:\n");
    multiply();

  }// end constructor
public static void main(String[] args) {

matrix m = new matrix();
}

public void fillA() {
int random = 0;
int randomN = 0;
for(int i = 0; i < A.length; i++) {
 for(int j = 0; j < A[0].length; j++)
 {
  random = (int)(Math.random()*6);
  randomN = (int)(Math.random()*10);

  if(randomN % 3 == 0)
     {   random = random * -1;  }

     A[j] = random;

  System.out.print(random + "\t");

  }//end for2
     System.out.println();
}//end for1
}// end method fill

public void fillB()
{
  int randomb=0;
  int randomB= 0;
  for(int k=0; k<B.length; k++){
    for(int c = 0; c < B[0].length; c++) {
    randomb = (int)(Math.random()*6);
    randomB = (int)(Math.random()*10);

    if(randomB % 3 == 0){
     randomb = randomb * -1;
    }
    B[k][c] = randomb;

    System.out.print(randomb + "\t");
   }
   System.out.println();
  }
}// end fillB method

public void multiply ()
{
 int  t = 0;
 C =  new int [B.length][A[0].length]; //Matrix C = A*B
// Output Matrix C
for( int i = 0;i<B.length;i++)
{
   for( int k  = 0;k<A[0].length;k++)
 {
      C[k] = 0;
     for( int j = 0;j<B[0].length;j++)
       {
            C[k] +=B[j]*A[j][k];

        }
System.out.print(C[k]);
System.out.print(" ");
}
 t = t+1;
if( t == 4);
          System.out.println();
}
}
}


  // public static void print(){
  // }

  // }//end class
#4

شكرا اخوي فعلا الكود ضبط

بس المطلوب في السؤال print method

على حسب فهمي مفروض يكون منفصل؟

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

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