Задание :
Дана матрица А размером MxN, состоящая из натуральных чисел, больших 1. Выполнить следующие действия:
1) Строки матрицы А упорядочить по невозрастанию произведения элементов строк.
2) Составить одномерный массив В, в котором элемент Вj, равен номеру столбца, содержащего только числа, кратные 5, в противном случае Вj = 0.
--------------------------
Нужно перевести задачу с Паскаля на С
Подскажите где я ошибся ?
Вот С :
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main()
{
const int m=5;
const int n=4;
int a[m][n]= {
{3,5,4,15},
{2,5,3,5},
{5,10,3,5},
{4,5,3,10},
{9,5,2,5}
};
int i,j,tmp;
int tta[n];
int psa[n];
int count;
int B[100];
for(i=1;i<m;i++)
{
psa[i]=1;
for(j=1;j<n;j++)
psa[i]=psa[i]*a[i][j];
}
for(i=1;i<m;i++)
for(j=1;j<m;j++)
{
if (psa[i]>psa[j])
{
tmp=psa[i]; psa[i]=psa[j]; psa[j]=tmp;
for (tmp=1;tmp<n;tmp++)
{ tta[tmp]=a[i][tmp]; }
for (tmp=1;tmp<n;tmp++)
{ a[i][tmp]=a[j][tmp]; }
for (tmp=1;tmp<n;tmp++)
{ a[j][tmp]=tta[tmp]; }
}
}
cout<<"Matritsa uporyadoch. po nevozrataniu proizvedniy elementov strok " << endl;
for(i=1;i<m;i++)
{
for(j=1;j<n;j++)
cout<<a[i][j];
}
for(j=1;j<n;j++)
{
count=0;
for(i=1;i<m;i++)
if (a[i][j]%5=0)
count=count+1;
if (count=5)
{
B[j]=j;
}
else B[j]=0;
}
cout<<"Poluchenniy massiv B="<<endl;
for(i=1;i<m;i++)
for(j=1;j<n;j++)
cout<<B[j];
getche()
return 0;
}
const m=5; n=4;
a: array[1..m,1..n] of integer=
(
(3,5,4,15),
(2,5,3,5),
(5,10,3,5),
(4,5,3,10),
(9,5,2,5)
);
var
i,j,tmp:integer;
tta,psa:array[1..n] of integer;
count: integer;
B: array[1..100] of integer;
begin
for i:=1 to m do
begin
psa[i]:=1;
for j:=1 to n do psa[i]:=psa[i]*a[i,j];
end;
for i:=1 to m do
for j:=1 to m do
begin
if psa[i]>psa[j] then
begin
tmp:=psa[i]; psa[i]:=psa[j]; psa[j]:=tmp;
for tmp:=1 to n do tta[tmp]:=a[i,tmp];
for tmp:=1 to n do a[i,tmp]:=a[j,tmp];
for tmp:=1 to n do a[j,tmp]:=tta[tmp];
end;
end;
writeln('Matritsa uporyadoch. po nevozrataniu proizvedniy elementov strok');
writeln;
for i:=1 to m do
begin
for j:=1 to n do write(a[i,j],' ');
writeln;
end;
for j := 1 to n do begin
count := 0;
for i := 1 to m do
if (a[i, j] mod 5) = 0 then count := count + 1;
if count = m then B[j] := j else B[j]:=0;
end;
writeln('Poluchenniy massiv B=');
for i:=1 to m do
for j:=1 to n do
write( B[j]);
readln;
end.
Для того, чтобы НЕ ошибаться, нужно не переводить программу с одного языка на другой, а начинать писАть ее сразу на нужном тебе языке.
У тебя постоянно одна и та же ошибка - массивы в С индексируются в 0 до N - 1, а не с 1 до N
for(i=1;i<m;i++)
if (a[i][j]%5=0)
count=count+1;
if (count=5)
for(i = 0; i < m; i++)
if( !(a[i][j] % 5) ) count++;
if(count = m) ...
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main()
{
const int m=5;
const int n=4;
int a[m][n]= {
{1,1,1,1},
{3,3,3,3},
{2,2,2,2},
{5,5,5,5},
{4,4,4,4}
};
int i,j,tmp;
int tta[n];
int psa[n];
int count;
int B[n];
for(i=0;i<m;i++)
{
psa[i]=1;
for(j=0;j<n;j++)
psa[i]=psa[i]*a[i][j];
}
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
if (psa[i]>psa[j])
{
tmp=psa[i]; psa[i]=psa[j]; psa[j]=tmp;
for (tmp=0;tmp<n;tmp++)
{ tta[tmp]=a[i][tmp]; }
for (tmp=0;tmp<n;tmp++)
{ a[i][tmp]=a[j][tmp]; }
for (tmp=0;tmp<n;tmp++)
{ a[j][tmp]=tta[tmp]; }
}
}
cout<<"Упоряд. матрица " << endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
for(j=0;j<n;j++)
{
count=0;
for(i = 0; i < m; i++)
if(!(a[i][j] % 5) ) count++;
if(count==5)
{
B[j]=j;
}
else B[j]=0;
}
cout<<"Полученный массив B="<<endl;
for(j=0;j<n;j++)
cout<<B[j];
getche();
}
Во-первых, почему
int psa[n]; // <--- Здесь не m должно быть, случайно ?
for(i=0;i<m;i++)
for(j=i+1;j<m;j++) { // <--- Здесь
if(psa[i]<psa[j]) {
int T = psa[i]; psa[i] = psa[j]; psa[j] = T;
for(int k = 0; k < n; k++) {
T = a[i][k]; a[i][k] = a[j][k]; a[j][k] = T;
}
}
}
Спасибо за помощь