خرید بک لینک

Vote count: 0

This is a working program that I've been trying to lea C++ from that inputs from a file strings and doubles and puts them into their perspective arrays. What I'm stuck on is while sorting the strings I want to sort the array of doubles in ascending order and keep the scores with the name they are associated with. I'm trying to lea it without using vectors before I go on to lea how to manipulate them, that's the reason for not using vectors before anyone asks. Is it best to sort the columns of the 2d array and than sort them with the 1d array or just to have a statement that does it all? Also what would be the best sorting algorithm for this application? My attempts have all failed so far so I tu to the community here for help. The logic is most likely some simple concept that I have yet to grasp. We all have to start somewhere. Thank you in advance for your help.

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;  

void ArraySort (string x[], double y[][3], int length);

int main() 
{
    ifstream inFile;

    inFile.open("bowlers2.txt");

    const int SIZE = 10;

    int i,j;
    double scores[10][3];
    string names[SIZE];
    string mystring;

    if (!inFile)
    {
        cout << "Can not open the input file"
             << " This program will end."<< "n";
        retu 1;

    }

    for(i = 0; i < SIZE; i++)
    {
        getline(inFile, names[i]);
        for(j = 0; j < 3; j++)
        {
            getline(inFile, mystring);
            scores[i][j] = atoi(mystring.c_str());
        }
    }

    for(int i=0;i<SIZE;i++)
    {
        cout << names[i] << "n";
        for(j = 0; j < 3; j++)
            cout << scores[i][j] << "n";
    }

    inFile.close();

    ArraySort (names, scores, SIZE);

    retu 0;
}

void ArraySort (string x[], double y[][3], int LENGTH)
{
    int i,j;
    string sValue;
    double dValue;

    for(i = 1; i < LENGTH; i++)
    {
        sValue = x[i];
        for(j = i - 1; j >= 0 && x[j] > sValue; j--)
        {
            x[j + 1] = x[j]; 
        }
        x[j + 1] = sValue;
    }

    cout << "n";
    for(int i=0;i<LENGTH;i++)
    {
        cout << x[i] << "n";
        for(j = 0; j < 3; j++)
            cout << y[i][j] << "n";
    }

}

file read by program:

Linus too good
100
23
210
Charlie brown
1
2
12
Snoopy
300
300
100
Peperment Patty
223
300
221
Pig Pen
234
123
212
Red Headed Girl
123
222
111
Marcey
1
2
3
keith hallmark
222
300
180
aa hallmark
222
111
211
roxie hallmark
100
100
2

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 30 فروردين 1395 ساعت: 7:36

صفحه بندی