I was wondering whats the simplest way to count and sort the elements of each array when using a set like

char list[40][30];

theyre being brought in from a text file that would simply look something like this

apples
bannanas
oranges
grapes

i would need to count each element, sort them into a new array and determine the largest and smallest whats the easiest way to do this without vectors or anything like that? maybe using strncpy or would i need a paraell array?
thanks for your help!

P.S. ive already managed to stream them in from the file fine and that array matches the input file

It's a shame you have those restrictions, because you can do it with std::map really easily:

std::map< std::string, unsigned > fruits;
while(inputFile.fail() != true){
    std::string fruitName;
    inputFile >> fruitName;
    fruits[fruitName]++;
}

std::cout << "Found the following fruits:" << std::endl;
std::map< std::string, unsigned >::iterator it = fruits.begin();
while(it != fruits.end())
    std::cout << (*it).second << " x " << (*it++).first << std::endl;

As a bonus, the fruits will all be sorted into alphabetical order :) Nice.

Do you mean sort the characters of each string, or sort the array of strings?

std::sort() http://www.cplusplus.com/reference/algorithm/sort/

sort the array of strings, like if it were:

pinapple
bannana
pie
blueberry

it would sort it into:
pie
bannana
pineapple
blueberrry

we havnt been taught vectors yet so im not certain i can use that site but at this point id be willing to take the penalty

It's a shame you have those restrictions, because you can do it with std::map really easily:

std::map< std::string, unsigned > fruits;
while(inputFile.fail() != true){
    std::string fruitName;
    inputFile >> fruitName;
    fruits[fruitName]++;
}

std::cout << "Found the following fruits:" << std::endl;
std::map< std::string, unsigned >::iterator it = fruits.begin();
while(it != fruits.end())
    std::cout << (*it).second << " x " << (*it++).first << std::endl;

As a bonus, the fruits will all be sorted into alphabetical order :) Nice.

haha thanks anyways i might use this if i cant figure out another way, like i said im willing to take a small penalty and might even get bonus marks for it i dunno

update: i got the counting working and got the largest and smallest working the only problem is i dont know where to insert a sort and would it be best to copy that into a new array? thanks!

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

//--------Function definitions----------



int main()
{
	
	//--------Constant declarations--------
	
	
	//--------Variable declarations---------
	ifstream fin; //input and output variables
	ofstream fout("sorted");
    int i; //counters
    int j;
    int totalstrings;
    char list[40][30]; //input array
	char sorted[40][30]; //sorted array
	char filename[100]; 
	int smallest;
	int largest;
	//------Main program body-----------
i=0;
j=0;
smallest=40;
largest=0;
count=0;
totalstrings=0;
	cout<<"**** File sorting program*****"<<endl<<endl; //Title
	
	//--------Main Program Body----------
    cout<<"Please enter the name of the file you wish to sort"<<endl;  //prompts user
	cin>>filename;
	fin.open(filename);
	cin.ignore();
	while(!fin.is_open()){  //check for error and re prompt user
		cout<<"Cannot open file, Please try again: ";
		cin>>filename;
		cin.ignore();
		fin.open(filename);}

for(i=0;i<=40;i++){   //get each string
    fin.getline(list[i],30);
	if(!fin){break;}
	totalstrings=i;
}

for(i=0;i<=totalstrings;i++){
string str (list[i]);
if (str.size()>largest){largest=str.size();} 
else if (str.size()<smallest){smallest=str.size();}
                 }


cout<<"totalstrings "<<totalstrings<<endl;
cout <<"smallest " << smallest<<endl;
cout <<"largest " << largest<<endl;
cout<< "Writing to file......";
for(i=0;i<=40;i++){
   fout<<sorted[i];
}
	fin.close(); //close files
	fout.close();
	
	cout << endl << endl;
       system("PAUSE");     // Pause the output window for reading
       return 0;
       }

What are you trying to sort? The characters in each string, or the strings themselves?

I'm guessing the strings, right? The example you posted doesn't really make it obvious to me how you want the strings sorted.

You may wish to use a lexicographical less-than: http://www.cplusplus.com/reference/algorithm/lexicographical_compare/
combined with some character functions from <cctype> like toupper() or tolower()
http://www.cplusplus.com/reference/clibrary/cctype/

Be a part of the DaniWeb community

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