I have this problem to solve for preparing for the olympiad and i'm not quite fond of the functions so that i want to do it classically but i find it harder.Here it goes:
"During a desert operation a group of soldiers were lost from different squads.After the storm passed the soldiers need to regroup to their squads, so they have some metallic tags on which their identification number is written on.So, the soldiers of the same squad have the ident. number formed by the same digits but arranged in a different way. For example, ident numbers 78003433, 83043073,33347008 tells us that the three soldiers are from the same squad.
REQUEST:
Having given the n numbers from the ident tags, you have to regroup the soldiers into squads indicating the number of squads found(a squad must be formed by minimum 1 soldier), the number of soldiers from the most numerous squad, the number of squads that have this number and also the components of this kind of squad(with the max number of regrouped soldiers).
ENTRY INFO:
The file of input "plutoni" will contain on the first line the number of the n soldiers found, and on each of the next n lines a number of identification of the n soldiers.
OUTPUT info:
The file of output "plutono" will contain on the first line the number of squads that are reassembled. On the second line it will contain the number of soldiers from the most numerous squad that is reassembled.On the third line it will contain the number of squads that have the maximum number of reunited soldiers.On the fourth line the formation of this kind of squad will be shown, the soldiers identification number being written each after another separated by a space(_).

EXAMPLE:if the input file contains the following:
10
1223
123
666
321
7890
2213
312
655
1000
1322
the output file will look like this:
6
3
2
321 312 123.
That's quite it..so here's what i've produced so far:

#include <iostream>
#include <fstream.h>
using namespace std;
int nr;
int nrsort;

long nrp,n,maxim,nrmax;
long sortare(long x) {//means sorting
long aux,c,y;
aux=x;
y=0;
for(c=9;c>=0;c--) {
while(x) {
if(x%10==c) {
y=y*10+x%10;}
x=x/10;}
x=aux;}
return y;}
void main() {
long i,j,p,pmax;
long aux;
fstream f1("plutoni.txt", ios::in);
f1>>n;
for(i=1;i<=n;i++) {
f1>>nr[i];
nrsort[i]=sortare(nr[i]);
f1.close();}
i=1;
nrp=nrmax=maxim=0;
while(i<=n) {
p=i;
j=i+1;
while(j<=n){
if(nrsort[i]==nrsort[j]) {
i++;
aux=nr[i];
nr[i]=nr[j];
nr[j]=aux;
aux=nrsort[i];
nrsort[i]=nrsort[j];
nrsort[j]=aux;}
j++;}
i++;
nrp++;
if(i-p>maxim) {
maxim=i-p;
nrmax=1;
pmax=p;}
else if(i-p==maxim) {
nrmax++;}}
fstream f2("plutono.txt", ios::out);
f2<<nrp<<endl<<maxim<<endl<<nrmax<<endl;
for(i=pmax;i<pmax+maxim;i++) {
f2<<nr[i]<<" ";
f2.close();}
}

Answered by siddhant3s 1,429 in a post from

>>I'm currently using the new borland so this is a correct syntax after all it's
>>working on my computer, as for the variables i am on writing description for
>>everyone of them.
This is a wakeup bell for all those who are using Old compilers ( like the OP is …

Answered by Salem 5,138 in a post from

> I'm currently using the new borland so this is a correct syntax
> after all it's working on my computer,
You have no idea how much grief you're going to get if you keep up with the "it works on this compiler" approach.

If for no other reason, …

All 6 Replies

WOW!!.. u wrote all that.. that's cool man loool

i can hardly write while and do while and with lots of mistakes..
good luck with that.. as i didn't get any of it hehe :D

It's <fstream> not <fstream.h>, main() must return() a value, and what do your variable names mean?

It's <fstream> not <fstream.h>, main() must return() a value, and what do your variable names mean?

I'm currently using the new borland so this is a correct syntax after all it's working on my computer, as for the variables i am on writing description for everyone of them.

#include <iostream>
#include <fstream.h>
using namespace std;
int nr;
int nrsort;

long n;//number of soldiers
long max;//maximum number of soldiers from a regrouped squad
long nrmax;//number of squads regrouped with the maximum number of soldiers
long sortare(long x) {//means sorting and returns the number obtained
long aux,c,y; //by arranging in decreasing order of the   digits
aux=x; //of x:for example sortare(2810831)=8832110->from the example
y=0; //in aux we save the initial value of x
for(c=9;c>=0;c--) {
while(x) {
if(x%10==c) {
y=y*10+x%10;}
x=x/10;}
x=aux;}
return y;}
void main() {
long i,j,p,pmax;
long aux;
fstream f1("plutoni.txt", ios::in);
f1>>n;
for(i=1;i<=n;i++) {
f1>>nr[i];
nrsort[i]=sortare(nr[i]);
f1.close();}
i=1;
nrp=nrmax=maxim=0;
while(i<=n) {
p=i;
j=i+1;
while(j<=n){
if(nrsort[i]==nrsort[j]) {
i++;
aux=nr[i];
nr[i]=nr[j];
nr[j]=aux;
aux=nrsort[i];
nrsort[i]=nrsort[j];
nrsort[j]=aux;}
j++;}
i++;
nrp++;
if(i-p>maxim) {
maxim=i-p;
nrmax=1;
pmax=p;}
else if(i-p==maxim) {
nrmax++;}}
fstream f2("plutono.txt", ios::out);
f2<<nrp<<endl<<maxim<<endl<<nrmax<<endl;
for(i=pmax;i<pmax+maxim;i++) {
f2<<nr[i]<<" ";
f2.close();}
}

hope it's clear now

>>I'm currently using the new borland so this is a correct syntax after all it's
>>working on my computer, as for the variables i am on writing description for
>>everyone of them.
This is a wakeup bell for all those who are using Old compilers ( like the OP is using Borland's) that C++ has been standardized 13years ago. So there is no concept as "It is working on my implementation so it ought to be right".
This is what cooks me up boiling. Ruthless.
and http://cppdb.blogspot.com/2009/02/should-i-use-void-main-or-int-main-or.html

These are some link is from Bjarne Stroustup's homepage:
* A Very simple ISO standard program http://www.research.att.com/~bs/bs_faq2.html#simple-program
and
* This ones about naming variables http://www.research.att.com/~bs/bs_faq2.html#Hungarian

(I am really surprised by the guts of OP. A good man (Mosiac) is giving him advice and he is just too much a snob to accept it)

commented: Fantastic!!!!!! +30

> I'm currently using the new borland so this is a correct syntax
> after all it's working on my computer,
You have no idea how much grief you're going to get if you keep up with the "it works on this compiler" approach.

If for no other reason, when you change to another compiler, all the crap you've learnt for the old compiler will no longer work on the new one.

If you're going to the trouble of learning C++, then try to learn standard C++ (which will work everywhere), and not "dialect-C++" which only works in one place.

> as for the variables i am on writing description for everyone of them.
But if you wrote more meaningful names to begin with, you wouldn't need to write a description. int ns; // number of students vs. int numberOfStudents; Which by the way would be obvious in all the places it was USED as well, not just where it was declared.
When your 'descriptive' comment scrolls off the top of the page, 'ns' once again becomes meaningless.

> hope it's clear now
Actually, I couldn't give a toss. It was posted without code tags so I just ignored it.

commented: Agreed :) +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.