Home > other >  How to connect sections of an array or organize an array the same as another?
How to connect sections of an array or organize an array the same as another?

Time:09-27

I am not sure how to connect a part of an array or if it is even possible. My code is as follows:

#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
string name;
string date[3];
double height[3];
double enter;

cout << "Enter name of a pole vaulter: ";
cin >> name;
cout << "Enter date of first vault: ";
cin >> date[0];
cout << "Enter height of first vault: ";
cin >> enter;
if (enter >= 2.0)
{
    if (enter <= 5.0) 
    {
        height[0] = enter;
    }
    else
    {
        cout << "Incorrect Value";
        abort();
    }
}
else
{
    cout << "Incorrect Value";
    abort();
}
cout << "Enter date of second vault: ";
cin >> date[1];
cout << "Enter height of second vault: ";
cin >> enter;
if (enter >= 2.0)
{
    if (enter <= 5.0)
    {
        height[1] = enter;
    }
    else
    {
        cout << "Incorrect Value";
        abort();
    }
}
else
{
    cout << "Incorrect Value";
    abort();
}
cout << "Enter date of third vault: ";
cin >> date[2];
cout << "Enter height of third vault: ";
cin >> enter;
if (enter >= 2.0)
{
    if (enter <= 5.0)
    {
        height[2] = enter;
    }
    else
    {
        cout << "Incorrect Value";
        abort();
    }
}
else
{
    cout << "Incorrect Value";
    abort();
}

int len = sizeof(height) / sizeof(height[0]);
sort(height, height   len, greater<int>());
cout << "Stats for " << name << ":" << endl;
for (int i = 0; i < len; i  ) {
    cout << height[i] << " ";
}
cout << height[0];
}

I am trying to enter dates and a double value, and then organize the double values in descending order and keep the dates with the corresponding value. I am not sure if this is possible, any alternative way of completing this would be helpful.
Thank you

CodePudding user response:

Group of data, data sorting, multiple data points that should be aligned/connected to their respective other data points. I think the best solution here would be the use of a struct or class with vectors:

  1. Let's say you want a variable that contains both your date and number. We can construct a class or structure for that:
#include <iostream>
using namespace std;

struct str1
{
    string date;
    double number;
};

class cls1
{
public:
    string date;
    double number;
};

int main()
{
    str1 ob1;
    cls1 ob2;
    
    ob1.date = "somedate";
    ob1.number = 12345;
    cin >> ob1.date;
    
    cout << ob1.date << " " << ob1.number << endl;
    
    
    ob2.date = "somedate2";
    ob2.number = 54321;
    cin >> ob2.number;
    
    cout << ob2.date << " " << ob2.number << endl;
    
    return 0;
}

Having a class or struct enables you to use objects (variables made from those structs or classes). Every object created has their own place in memory for storing both date and number. You can use, find, search any of these variables and have access to both values this way.

  1. Grouping them up so there's a list of them can be done in vectors.

Vectors are like better arrays. They not only have a dynamical size (meaning its size can change and doesnt stay static like in arrays), but they also have quite a bit ready made functions for you to use:

bool sortingFunction(int &a, int &b)
{
    if (a > b) return true;
    else return false;
}

int main2()
{
    vector<int> numbers;
    
    //to add
    numbers.emplace_back(5); //5 is the number to add
    
    //to remove
    numbers.erase(numbers.begin()   2); //2 is the index of the variable to delete
    
    //to sort
    sort(numbers.begin(), numbers.end(), sortingFunction);
    
    return 0;
}

Vectors need the #include <vector> header. Sort is a function that sorts. Needs #include <algorithm> header. Sort function is neat because you can define the logic behind how you want to sort the vector or array with a seperate function that returns either true or false.

  1. For your example you could do something like this in the end:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct myType
{
    string date;
    double number;
};

bool sortByDate(myType &a, myType &b)
{
    if (a.date > b.date) return true;
    else return false;
}

bool sortByNumber(myType &a, myType &b)
{
    if (a.number > b.number) return true;
    else return false;
}

int main()
{
    vector<myType> variables;
    
    int num;
    cout << "how many do you want to add" << endl;
    cin >> num;
    
    for(int i = 0; i < num; i  )
    {
        myType tmp;
        cout << "Enter date of var" << i 1 << ": ";
        cin >> tmp.date;
        cout << "Enter number of var" << i 1 << ": ";
        cin >> tmp.number;
        variables.emplace_back(tmp);
    }
    
    //after that you can use the vector as you want...
    //sort
    sort(variables.begin(), variables.end(), sortByDate);
    sort(variables.begin(), variables.end(), sortByNumber);
    //delete
    variables.erase(variables.begin() 5);
    //or clear the entire thing
    variables.clear();
    //Either way each item in the vector consists of both number and date thus even
    //if you sort the vector the values are still connected at the same position
    
    return 0;
}
  • Related