اذهب إلى المحتوى
  • 0

ايجاد اكبر عنصر في المصفوفة و اكبر مجموع صفوف في مصفوفة ثنائية في جافا

Hoida Ahamed

السؤال

package Java;

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
  System.out.println(" ENTER size:");
  Scanner input=new Scanner(System.in);

  int rows=input.nextInt();
  int colums=input.nextInt();
 
  System.out.println(" ENTER Elemnt:");
  int arr[][]=new int[rows][colums];
 
  for(int i=0;i<rows;i++){
    for (int j=0;j<colums;j++){
      arr[i][j]=input.nextInt();
    }
  }

  printArray(arr);
    System.out.println();
    int max;
    max=arr[0][0];
    if(arr[i][j]>max)
    max=arr[i][j];
      for(int i=0;i<rows;i++){
    for (int j=0;j<colums;j++){
    System.out.println(max);
    }
    }
 
   int R,C,SR,SC;
  R=arr.length;
  C=arr[0].length;
 
  for(int i=0;i<R;i++){
    SR=0;
    for(int j=0;j<C;j++){
      SR+=arr[i][j];
    }
   System.out.println("sum of row :"+(i+1)+":="+SR);
  }
  for(int i=0;i<C;i++){
    SC=0;
    for(int j=0;j<R;j++){
      SC+=arr[j][i];
    }
    System.out.println("sum of colum :"+(i+1)+":="+SC);
  }
  
  }

static void printArray(int a [] []){
  System.out.println(" Data ");
  for(int []x:a){
    for(int y:x){
     System.out.print(y+" ");
    }
    System.out.println();
  }
  }
  }

١/ايجاد اكبر عنصر في المصفوفة

٢/ايجاد اكبر مجموع صفوف

تم التعديل في بواسطة Wael Aljamal
توضيح السؤال
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

بالنسبة لأكبر عنصر، يجب أن نضع الشرط داخل حلقتي for حيث أن i و j غير معرفتين!

ونطبع الناتج بعد الانتهاء من اختبار جميع العناصر (يفضل اختيار اسم متغير غير max لأن هذا اسم لدالة محجوزة)

int max;
max=arr[0][0];

for(int i=0;i<rows;i++){
  for (int j=0;j<colums;j++){
    
    if(arr[i][j]>max)
      max=arr[i][j];
  }
}
System.out.println(max);

أكبر مجموع سطر:

int R,C,SR,SC;
R=arr.length;
C=arr[0].length;

int MaxSumRow = 0;

for(int i=0;i<R;i++){
  SR=0;
  for(int j=0;j<C;j++){
    SR+=arr[i][j];
  }
  if (SR > MaxSumRow) {
  	MaxSumRow = SR;
  }
  System.out.println("sum of row :"+(i+1)+":="+SR);
}

System.out.println("MAx Sum Row :" + MaxSumRow );

قمت هنا بتعريف متغير إضافي ليقوم بتخزين أعلى مجموع.

حاولي تطبيق نفس الفكرة على أكبر مجموع عمود للكود الآخر

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...