Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

copying a file to an array and sorting

#include<bits/stdc++.h>
using namespace std;



string winner_arr[108 ]; // completes 108 competitions in between 1903 and 2012 remove (1904 and 1994)

void readNames(string arr[])
{
        ifstream myfile ("Teams.txt");

        int indx=0;
        string line;
        if(myfile.is_open())
         {

        while (! myfile.eof() ) 
        {
            getline (myfile,line); 
            arr[indx] = line;
            indx++;
        }
        myfile.close(); 
    }
    else cout << "can't open the file"; 

}

int winnerCount(string tname)
{

        int cnt=0;
        for(int i=0;i<108;i++)
                if(winner_arr[i]==tname) cnt++;  // max 108 cups

        return cnt;
}


void teamCounts(string tnames[],int  n)
{

        int cnt_arr[n];  // To count the number of winning of team
        int indx_arr[n];  // store the index of the team when we sorting the teams according to wins initially same value

        for(int i=0;i<n;i++){
                cnt_arr[i]=winnerCount(tnames[i]);
                indx_arr[i]=i;
        }

        // we use simple bubble sort algorithm to sort the data 
        for(int i=0;i<n-1;i++)
        {
                for(int j=0;j<n-i-1;j++)
                {
                        if(cnt_arr[j]>cnt_arr[j+1])
                        {
                                swap(cnt_arr[j],cnt_arr[j+1]);
                                swap(indx_arr[j],indx_arr[j+1]); // index of the present team updated 
                        }
                }
        }


        cout<<"Team name "<<"    Wins"<<endl; 
        for(int i=0;i<n;i++)
        {
                cout<<tnames[indx_arr[i]]<<"             "<<cnt_arr[i]<<endl;
        }

}


void readWinnerTeams()
{

        ifstream myfile ("WinnerTeams.txt");

        int indx=0;
        string line;
        if(myfile.is_open())
         {

        while (! myfile.eof() ) 
        {
            getline (myfile,line); 
            winner_arr[indx] = line;
            indx++;
        }
        myfile.close(); 
    }
    else cout << "can't open the file"; 
}



int main()
{
        string arr[100]; // maximum hundred teams
        readNames(arr);
        readWinnerTeams();
        teamCounts(arr,5);


        return 0;

}
Source by www.chegg.com #
 
PREVIOUS NEXT
Tagged: #copying #file #array #sorting
ADD COMMENT
Topic
Name
9+5 =