array int and digit sorting
Sep 7, 2011 at 4:51pm UTC
help me with this problem please. i cannot sort the character inside my array.
Input
First line of input contains the number of teams T in the competition and the number of rounds R they
have competed. The succeeding lines contain the teams with their corresponding scores for the rounds.
Assume that each round is worth 100 points.
Output
You are to return the name of the teams with their corresponding scores arranged from first to fifth
place. Assume that no teams will have the same general average.
Sample Input:
6 4
A 10 20 10 20
P 20 20 20 80
T 100 80 90 100
H 80 80 80 80
K 90 90 90 90
G 100 0 0 100
Sample Output
1: T 92.5
2: K 90
3: H 80
4: G 50
5: P 35
here is my code atm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(){
int x,y,loop1=0;
double arraysum=0;
char ch;
ifstream indata;
cout<<fixed<<setprecision(2);
indata.open("top.in" );
indata >> x >> y;
int array_int[y];
char array_char[x];
double array_avg_holder[x];
while (loop1 < x){
for (int i=0; i<y; i++){
if (i==0){
indata >> array_char[loop1];
//cout << array_char[loop1]<<" ";
}
else {
indata >> array_int[i];
//cout << array_int[i]<<" ";
arraysum = arraysum + array_int[i];
}
}
arraysum = arraysum / (y-1);
array_avg_holder[loop1] = arraysum;
//cout << array_avg_holder[loop1]<< endl;
arraysum=0;
loop1++;
}
for (int next=1; next<x; next++){
double insert = array_avg_holder[next];
char insert_char = array_char[next]; <- this part is my problem
int moveitem = next;
while (moveitem>0 && array_avg_holder[moveitem-1] > insert)
{
array_avg_holder[moveitem] = array_avg_holder[moveitem-1];
array_char[moveitem] = array_char[moveitem-1];
moveitem--;
}
array_avg_holder[moveitem] = insert;
}
for (int i=x-1; i>-1; i--){
cout<<array_char[i]<<" " <<array_avg_holder[i]<<endl;
}
system("pause" );
}
Oct 12, 2011 at 8:20pm UTC
use this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(){
int x,y,loop1=0;
double arraysum=0;
char ch;
ifstream indata;
cout<<fixed<<setprecision(2);
indata.open("C:\\Users\\bhupender kumar\\Desktop\\in.txt" );
indata >> x >> y;
int *array_int = new int [y];
char *array_char = new char [x];
double *array_avg_holder = new double [x];
while (loop1 < x)
{
for (int i=0; i<=y; i++)
{
if (i==0)
{
indata >> array_char[loop1];
}
else
{
indata >> array_int[i];
arraysum = arraysum + array_int[i];
}
}
array_avg_holder[loop1] = arraysum/y;
arraysum=0;
loop1++;
}
double insert = 0;
char insert_char = ' ' ;
for (int next=0; next<(x-1); next++)
{
int moveitem = next+1;
while (moveitem<x)
{
if (array_avg_holder[moveitem] > array_avg_holder[next])
{
insert = array_avg_holder[moveitem];
array_avg_holder[moveitem] = array_avg_holder[next];
array_avg_holder[next] = insert;
insert_char = array_char[moveitem];
array_char[moveitem] = array_char[next];
array_char[next] = insert_char;
}
moveitem++;
}
}
for (int i=0; i<5; i++)
{
cout<<array_char[i]<<" " <<array_avg_holder[i]<<endl;
}
system("pause" );
}
Topic archived. No new replies allowed.