Dynamic Matrix
Hi, please help;
I want to put the row, where all the elements are prime numbers, in the end of the matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include<iostream>
#include<cassert>
using namespace std;
bool prime(int *c,int l)
{
int q=0;
for(int i=0;i<l;i++)
{
if(c[i]==1) return false;
for(int j=2;j<=c[i]/2;j++) if(c[i]%j==0) return false;
}
return true;
}
int main()
{
int n,m,**a,i,j,q,*k;
cin>>n>>m;
a=new int*[n]; assert(a);
for(i=0;i<n;i++)
{ a[i]=new int[m]; assert(a[i]); }
for(i=0;i<n;i++) for(j=0;j<m;j++) cin>>a[i][j];
i=0;
while(i<n)
if(prime(a[i],m))
for(q=0;q<n-i-1;q++) { k=a[i+q]; a[i+q]= a[n-1]; a[n-1]=k; }
else i++;
for(i=0;i<n;i++)
{
cout<<endl;
for(j=0;j<m;j++) cout<<a[i][j]<<" ";
}
for(i=0;i<n;i++) delete[] a[i]; delete[] a;
return 0;
}
|
What does your lines 23-27 loop do?
Topic archived. No new replies allowed.