Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Binary Search Array/Struct Update Question
Post Flair (click to view more posts with a particular flair)
Post Body

Is there any way I can convert line 97 from doing a binary search of an astronaut id array to a struct array that hold various astronaut ids? For class I need to do a similar job in a new manner. I have no clue how to convert this over from what is on line 97 to use this:

struct astronautDataType {
int astro_id;
string astro_name;
int astro_test1_tries;
int astro_test1_score;
int astro_test2_tries;
int astro_test2_score;
};
astronautDataType astronaut[SIZE];

Do I make a separate binary search function for a struct type? Can I retain most of the previous binary search code in the "update_score()" function?

Changing

index = BinarySearch(astronaut_id, id, 0, current_size - 1);

to

index = BinarySearch(astronaut.astro_id, id, 0, current_size - 1);

doesn't work and neither does using "astronaut" alone. How can I easily tell it to do a binary search for the IDs specifically in the struct array I made?




Current code without custom struct below:


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

#define SIZE 30

using namespace std;

// Variables
int astronaut_id[SIZE], test1_tries[SIZE], test2_tries[SIZE], current_size;
double score_test1[SIZE], score_test2[SIZE];
string temp;

// Function prototypes
void read_data();
void update_score();
void print_old_data();
string ranking(int, int, double, double);
void print_data();
void print_master();
int BinarySearch(int A[], int target, int low, int high);



/* ****************************************************************** */
/* FUNCTION NAME: Main Function                                       */
/* FUNCTION PURPOSE: Includes inital necessary file input and output  */
/*                                                                    */
/* INPUT PARAMETERS:                                                  */
/*      1. <int>  argc                                                */
/*      1. <char> argv                                                */
/*                                                                    */
/* RETURN VALUE – 0                                                   */
/* ****************************************************************** */


int main(int argc, char** argv) {
    read_data();
    update_score();
    print_data();
    print_master();
    return 0;
}


/* ****************************************************************** */
/* FUNCTION NAME: read_data                                           */
/* FUNCTION PURPOSE: Imports data from welding file                   */
/*                                                                    */
/* INPUT PARAMETERS:                                                  */
/*      1. Empty                                                      */
/*                                                                    */
/* RETURN VALUE – N/A                                                 */
/* ****************************************************************** */

void read_data()
{
    ifstream in("Welding.data");
    int i = 0;
    getline(in, temp);
    while(in)
    {
        in >> astronaut_id[i] >> test1_tries[i] >> score_test1[i] >> test2_tries[i] >> score_test2[i];
        i  ;
    }
    in.close();
    current_size = i;
    print_old_data();
}



/* ****************************************************************** */
/* FUNCTION NAME: update_score                                        */
/* FUNCTION PURPOSE: Updates data from scores file to welding file    */
/*                                                                    */
/* INPUT PARAMETERS:                                                  */
/*      1. Empty                                                      */
/*                                                                    */
/* RETURN VALUE – N/A                                                 */
/* ****************************************************************** */

void update_score()
{
    int id, test, index;
    double score;
    ifstream in("Scores.data");
    ofstream out("Report.out", ios::app);
    int i = 0;
    out << "\n\n" << setw(32) << "Astronaut ID" << setw(10) << "Test" << setw(9) << "Score" << setw(12) << "Ranking" << "\n" << endl;
    getline(in, temp);
    while(in)
    {
        in >> id >> test >> score;
        index = BinarySearch(astronaut_id, id, 0, current_size - 1);
        if(index == -1)
        {
            out << "Candidate with ID = " << id << " Does not exist" << endl;
        }
        else
        {
            if(test1_tries[i] >= 3 || test2_tries[i] >= 3)
            {
                out << "Candidate with ID = " << id << " Cannot take further tests" << endl;
            }
            else
            {
                out << "Old Candidate Data" << setw(9);
                out << id;
                out << setw(14) << test;
                string rank = ranking(test1_tries[index], test2_tries[index], score_test1[index], score_test2[index]);
                if(test == 1)
                {
                    out << setw(10) << score_test1[index];
                    test1_tries[index]  ;
                    score_test1[index] = score;
                }
                else
                {
                    out << setw(10) << score_test2[index];
                    test2_tries[index]  ;
                    score_test2[index] = score;
                }
                out << setw(14) << rank << endl;
                out << "New Candidate Data" << setw(9);
                out << id << setw(14) << test << setw(10) << score;
                out << setw(14) << ranking(test1_tries[index], test2_tries[index], score_test1[index], score_test2[index]) << "\n" << endl;

            }
        }
    }
    in.close();
    out.close();
}


/* ****************************************************************** */
/* FUNCTION NAME: print_old_data                                      */
/* FUNCTION PURPOSE: Prints old candidate data to report              */
/*                                                                    */
/* INPUT PARAMETERS:                                                  */
/*      1. Empty                                                      */
/*                                                                    */
/* RETURN VALUE – N/A                                                 */
/* ****************************************************************** */

void print_old_data()
{
    ofstream out("Report.out");
    out << "Astronaut ID" << setw(28) << "Number of Tries: Test 1" << setw(16) << "Score Test 1" << setw(28) << "Number of Tries: Test 2" << setw(17) << "Score Test 2" << setw(16) << "Ranking" << endl;
    for(int i = 0 ; i < current_size - 1 ; i  )
    {
        out << setw(7) << astronaut_id[i] << setw(21) << test1_tries[i] << setw(24) << score_test1[i] << setw(22) << test2_tries[i] << setw(23) << score_test2[i];
        out << setw(20) << ranking(test1_tries[i], test2_tries[i], score_test1[i], score_test2[i]) << endl;
    }
    out.close();
}


/* ****************************************************************** */
/* FUNCTION NAME: ranking                                             */
/* FUNCTION PURPOSE: Calculates ranking                               */
/*                                                                    */
/* INPUT PARAMETERS:                                                  */
/*      1.<int> test1_tries                                           */
/*      1.<int> test2_tries                                           */
/*      1.<double> score_test1                                        */
/*      1.<double> score_test2                                        */
/*                                                                    */
/* RETURN VALUE – rank                                                */
/* ****************************************************************** */

string ranking(int test1_tries, int test2_tries, double score_test1, double score_test2)
{
    string rank;
    if(test1_tries >= 3 || test2_tries >= 3)
    {
        rank = "Terminated";
    }
    else if(test1_tries == 0 && test2_tries == 0)
    {
        rank = "Beginner";
    }
    else if(score_test1 >= 90.00 && score_test2 >= 95.0)
    {
        rank = "Master";
    }
    else if(score_test1 >= 85.0 && score_test2 >= 90.0)
    {
        rank = "Apprentice";
    }
    else if(score_test1 < 85.0 && score_test2 < 90.0)
    {
        rank = "Novice";
    }
    else
    {
        rank = "Probation";
    }
    return rank;
}


/* ****************************************************************** */
/* FUNCTION NAME: print_data                                          */
/* FUNCTION PURPOSE: outputs data to welding file                     */
/*                                                                    */
/* INPUT PARAMETERS:                                                  */
/*      1. Empty                                                      */
/*                                                                    */
/* RETURN VALUE – N/A                                                 */
/* ****************************************************************** */

void print_data()
{
    ofstream out("Welding.data");
    out << "Astronaut ID" << setw(28) << "Number of Tries: Test 1" << setw(16) << "Score Test 1" << setw(28) << "Number of Tries: Test 2" << setw(17) << "Score Test 2" << endl;
    for(int i = 0 ; i < current_size - 1 ; i  )
    {
        out << setw(7) << astronaut_id[i] << setw(21) << test1_tries[i] << setw(24) << score_test1[i] << setw(22) << test2_tries[i] << setw(23) << score_test2[i] << endl;
    }
    out.close();
}


/* ****************************************************************** */
/* FUNCTION NAME: print_master                                        */
/* FUNCTION PURPOSE: outputs master data to welding file              */
/*                                                                    */
/* INPUT PARAMETERS:                                                  */
/*      1. Empty                                                      */
/*                                                                    */
/* RETURN VALUE – N/A                                                 */
/* ****************************************************************** */

void print_master()
{
    ofstream out("Report.out", ios::app);
    out << "\n" << setw(85) << "Masters of the USA Astronaut Welding Training Program" << "\n" << endl;
    out << "Astronaut ID" << setw(28) << "Number of Tries: Test 1" << setw(16) << "Score Test 1" << setw(28) << "Number of Tries: Test 2" << setw(17) << "Score Test 2" << setw(16) << "Ranking" << endl;
    for(int i = 0 ; i < current_size - 1 ; i  )
    {
        string rank = ranking(test1_tries[i], test2_tries[i], score_test1[i], score_test2[i]);
        if(rank == "Master")
        {
            out << setw(7) << astronaut_id[i] << setw(21) << test1_tries[i] << setw(24) << score_test1[i] << setw(22) << test2_tries[i] << setw(23) << score_test2[i];
            out << setw(20) << rank << endl;
        }
    }
    out.close();
}


/* ******************************************************************************* */
/* FUNCTION NAME: BinarySearch                                                     */
/* FUNCTION PURPOSE: Determines if value is in array                               */
/*                                                                                 */
/* INPUT PARAMETERS:                                                               */
/*      1. A      - address of the integer array                                   */
/*      2. Target - the integer value to be matched (key)                          */
/*      3. Low    - the integer value for the index of the initial array element.  */
/*      4. High   - the integer value of the last index of the array.              */
/*                                                                                 */
/* RETURN VALUE   - If found, index of the item found otherwise a -1               */
/* ******************************************************************************* */


int BinarySearch(int A[], int target, int low, int high) {
    int mid;
    while (low <= high)
    {
        mid = (low   high) / 2;
        if (target == A[mid])
            return mid;
        else if (target < A[mid])
            high = mid - 1;
        else
            low = mid   1;
    }

    return -1;

}

Author
Account Strength
100%
Account Age
11 years
Verified Email
Yes
Verified Flair
No
Total Karma
49,139
Link Karma
9,419
Comment Karma
36,871
Profile updated: 2 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago