double[][] ret = new double[matrix.length][matrix[0].length];
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
ret[i][j] = matrix[order[i]][j];
}
}
return ret;
}
private static void fill(double[][] matrix)
{
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
matrix[i][j] = Math.random()*10;
}
}
}
private static int[] getOrder(double[][] matrix)
{
double[] sums = new double[matrix.length];
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
sums[i]+=matrix[i][j];
}
}
int[] order = new int[sums.length];
for (int i=0;i<sums.length;i++ )
{
order[i] = i;
}
for (int i=0;i<sums.length;i++ )
{
for (int j=i+1;j<sums.length;j++ )
{
if(sums[i]<sums[j])
{
double tmp1 = sums[i];
int tmp2 = order[i];
sums[i] = sums[j];
order[i] = order[j];
sums[j] = tmp1;
order[j] = tmp2;
}
}
}
return order;
}
private static void print(double[][] matrix )
{
for (int i=0;i<matrix.length;i++ )
{
double sum = 0.0;
for (int j=0;j<matrix[i].length;j++ )
{
System.out.print(matrix[i][j]+" ");
sum+=matrix[i][j];
}
System.out.println(" sum: "+sum);
}
System.out.println();
}
}
gri-7410
07.07.2020
// PascalABC.NET 3.0, сборка 1073 const m=5; n=4; a:array[1..m] of real=(2,-7.1,3.2,-5.4,6.9); b:array[1..n] of real=(8.1,3.2,-6.4,-3.5); k=3; var i,j:integer; sk:real; s:array[1..m,1..n] of real; begin Writeln('Массив S'); for i:=1 to m do begin for j:=1 to n do begin s[i,j]:=a[i]*b[j]; Write(s[i,j]:7:2) end; Writeln end; sk:=0; for i:=1 to m do sk:=sk+s[i,k]; Writeln('Сумма элементов столбца ',k,' равна ',sk) end.
Результат выполнения программы: Массив S 16.20 6.40 -12.80 -7.00 -57.51 -22.72 45.44 24.85 25.92 10.24 -20.48 -11.20 -43.74 -17.28 34.56 18.90 55.89 22.08 -44.16 -24.15 Сумма элементов столбца 3 равна 2.56
smakarov76
07.07.2020
#include <iostream> #include <ctime> #include <cstdlib> using namespace std;
int main(){ srand(time(0)); int n,m; cin>>n; cin>>m; int **a=new int *[m]; for(int i=0;i<m;i++) a[i]=new int[n]; cout<<"Matrix A:"; for(int i=0;i<m;i++){ cout<<"\n"; for(int j=0;j<n;j++){ a[i][j]=rand()%17-7; cout<<a[i][j]<<" "; } } int *b = new int[m]; cout<<"\n Array B:"; for(int j=0;j<n;j++){ b[j]=0; for(int i=0;i<m;i++){ b[j]=b[j]+a[i][j]; }cout<<b[j]<<" "; } cin.get(); cin.get(); return 0; }
Ответить на вопрос
Поделитесь своими знаниями, ответьте на вопрос:
[java] дана вещественная матрица размером 10х40. упорядочить ее строки по убыванию суммы их элементов
public class MyClass {
public static void main(String args[]) {
int dimention1 = 10;
int dimention2 = 5;//40
double[][] matrix = new double[dimention1][dimention2];
fill(matrix);
print(matrix);
matrix = sort(matrix, getOrder(matrix));
print(matrix);
}
private static double[][] sort(double[][]matrix, int[] order)
{
double[][] ret = new double[matrix.length][matrix[0].length];
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
ret[i][j] = matrix[order[i]][j];
}
}
return ret;
}
private static void fill(double[][] matrix)
{
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
matrix[i][j] = Math.random()*10;
}
}
}
private static int[] getOrder(double[][] matrix)
{
double[] sums = new double[matrix.length];
for (int i=0;i<matrix.length;i++ )
{
for (int j=0;j<matrix[i].length;j++ )
{
sums[i]+=matrix[i][j];
}
}
int[] order = new int[sums.length];
for (int i=0;i<sums.length;i++ )
{
order[i] = i;
}
for (int i=0;i<sums.length;i++ )
{
for (int j=i+1;j<sums.length;j++ )
{
if(sums[i]<sums[j])
{
double tmp1 = sums[i];
int tmp2 = order[i];
sums[i] = sums[j];
order[i] = order[j];
sums[j] = tmp1;
order[j] = tmp2;
}
}
}
return order;
}
private static void print(double[][] matrix )
{
for (int i=0;i<matrix.length;i++ )
{
double sum = 0.0;
for (int j=0;j<matrix[i].length;j++ )
{
System.out.print(matrix[i][j]+" ");
sum+=matrix[i][j];
}
System.out.println(" sum: "+sum);
}
System.out.println();
}
}