C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 25th, 2012, 12:19 PM
Rost013 Rost013 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 Rost013 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

}
}
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - Sort array


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap