In: Computer Science
#include <iomanip>
#include<iostream>
#include<string.h>
using namespace std;
int size;
//function definition
void readData(string names[], int scores[], int size)
{
cout<<"How many FIFA scores will you enter ? : ";
cin>>size;
::size=size;//assigning local varible value to global
variable
for(int i=0;i<size;i++)
{
cout<<"Enter the name for player #"<<i+1<<" :
";
cin>>names[i];
cout<<"Enter the score for FIFA scorer #"<<i+1<<"
: ";
cin>>scores[i];
}
}
void displayData(const string names[], const int scores[], int
size)
{
cout<<"Top Scores :\n";
for(int i=0;i<size;i++)
{
cout<<left<<setw(25)<<names[i]<<scores[i]<<"\n";
}
}
void sortData(string names[], int scores[], int size)
{
string temp_name=names[0];
int temp_scores=scores[0];
for(int i=0;i<size;i++)//iterations for array sorting
{
for(int j=i;j<size;j++)
{
if(scores[j]<scores[j+1])
{
temp_scores=scores[j+1];
temp_name=names[j+1];
scores[j+1]=scores[j];
names[j+1]=names[j];
names[j+1]=temp_name;
scores[j]=temp_scores;
}
}
}
}
int main()
{
int *scores = new int[10]();//allocation of dynamic memory
string *names=new string[10]();
readData(names,scores,size);//function calls
sortData(names,scores,size);
displayData(names,scores,size);
delete [] scores;//deallocation of dynamic memmory
delete [] names;
return 0;
}
OUTPUT
How many FIFA scores will you enter ? : 5
Enter the name for player #1 : Ronaldo
Enter the score for FIFA scorer #1 : 10400
Enter the name for player #2 : Didier
Enter the score for FIFA scorer #2 : 9800
Enter the name for player #3 : Pele
Enter the score for FIFA scorer #3 : 12300
Enter the name for player #4 : kaka Kaka
Enter the score for FIFA scorer #4 : 8000
Enter the name for player #5 : Cristiano
Enter the score for FIFA scorer #5 : 8400
Top Scores :
Ronaldo 10400
Didier 12300
Pele 9800
Kaka 8400
Cristiano 8000