Search
 
SCRIPT & CODE EXAMPLE
 

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;

}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to refresh multiple command lines in C++ stream 
Cpp :: add two constant char pointers c++ 
Cpp :: fast scan in c++ 
Cpp :: c++ first index 0 or 1 
Cpp :: 1/2(-3-3)(-3+4) 
Cpp :: find number of 1s in a binary cv::mat image 
Cpp :: 136. Single Number leetcode solution in c++ 
Cpp :: Write C++ program that will ask to choose from three cases. 
Cpp :: c+ 
Cpp :: template design pattern 
Cpp :: all usefull stls in cpp imports 
Cpp :: c++ How can I make a std::vector of function pointers 
Cpp :: interactive problem 
Cpp :: c++ ignore_line 
Cpp :: Define and show the implementation of the functions of an arrayList. 
Cpp :: why do men drink liquor 
Cpp :: the partition function of a system is given by z= 1/(1-e^-bEi), calculate the total energy of the system 
Cpp :: add comment in c/c++ 
Cpp :: hola mundo c++ 
Cpp :: C++ concept simple requirements 
Cpp :: rotateArray 
Cpp :: Consider a pair of integers, (a,b). The following operations can be performed on (a,b) in any order, zero or more times: - (a,b) - ( a+b, b ) - (a,b) - ( a, a+b ) 
Cpp :: c++ server service ros 
Cpp :: how to analyse a poem 
Cpp :: print number with leading zeros 
Cpp :: conditions in c++ 
Cpp :: what do we use c++ vectors for 
Cpp :: print all even number using for loop c++ 
C :: _CRT_SECURE_NO_WARNINGS 
C :: how to download file in powershell 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =