
November 25th, 2012, 12:19 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 19 m 50 sec
Reputation Power: 0
|
|
|
Memory & arrays - Sort array
Need sort array
from
4 4 5 5 5
3 4 5 4 4
5 5 5 5 5
5 3 4 4 4
5 4 4 5 4
to
5 5 5 5 5
4 4 5 5 5
5 4 4 5 4
i have done some, but cant delete row where there is "3"
#include <iostream>
#include <fstream>
using namespace std;
void print(int **a, int nrow, int ncol, double *sum);
void sort(int **a, int nrow, int ncol, double *sum);
int main(void)
{ int nrow, ncol;
int i,j; int **a;
ifstream f;
f.open("D:\\student.txt");
if (f)
{ cout << "Read file ! \n";
f >> nrow; f >> ncol;
a = new int *[nrow];
for(i = 0;i < nrow;i++)
a[i]= new int [ncol];
for(i = 0;i < nrow;i++)
for(j = 0;j < ncol;j++)
f >> a[i][j];
cout << "\n";
f.close();
double *sum = new double [nrow];
for (i = 0;i < nrow;i++)
{sum[i] = 0;
for (j = 0;j < ncol; j++)
sum[i] += a[i][j];
}
cout<<"\n Array\n";
print(a, nrow, ncol, sum);
cout << "\n";
//output sort array
sort(a, nrow, ncol, sum);
cout << "\Sort";
print(a, nrow, ncol, sum);
cout << "\n";
}
else cout << "File not found \n";
}
void print(int **a, int nrow, int ncol, double *sum)
{ for (int i = 0;i < nrow; i++)
{ for (int j = 0;j < ncol; j++)
cout << " " << a[i][j]<< " ";
cout << "| " << sum[i]/ncol << "\n";
}
}
void sort(int **a, int nrow, int ncol, double *sum)
{ long buf_sum;
int nmin, buf_a;
for(int i = 0;i < nrow-1; i++)
{ nmin = i;
for(int j = i+1;j < ncol; j++)
if (sum[j] > sum[nmin])
nmin = j;
buf_sum = sum[i];
sum[i] = sum[nmin];
sum[nmin] = buf_sum;
for(int j = 0;j < ncol;j++)
{ buf_a = a[i][j];
a[i][j] = a[nmin][j];
a[nmin][j] = buf_a;
}
}
}
|