Let me try to explain a problem I have.I have built array of structures that contains int(num of days) and string(Name).So basicy user should enter name of a friend and days they last talked,and then make a list sorted by the names of the friends of by how recently it was since they talked to each friend.So my idea is to take num of days,and sort them, but then I dont know how to connect that again with Names so I can print it out. am not looking for a homework solution, just some guidance. thanks

The solution or hints of a solution depend on which data structures you plan to use. Could you post what you have so far?

You can make use of the `std::sort()` (Click Here) function which is found in the `<algorithm>` header.

It require two random access iterators from a sequence, the first iterator should point to the beginning of the sequence, and the second to the end of the sequence.
It also requires a binary function to compare the items:

Here's a small example:

``````struct Person {
int days;
string name;
};

bool compare_persons( const Person& first, const Person& second){
return first.days < second.days;
}

int main(){
vector<Person> persons; //create a vector in which to store all the persons
// ... populate the vector

std::sort(
persons.begin(),   // iterator at the beginning of the sequence
persons.end(),     // iterator at the end of the sequence
compare_persons);  // binary function that compares 2 Person structs

//that's all, your vector with the persons is now sort accordingly to your
//binary function given as a comparator
return 0;
}
``````

Don't get me wrong, but am working trough this book and trying to do these practise problems only with knowlege that book has given me so far, and data structures are my next chapter, so am not familiar with vectors. Any way this is what I got :

``````#include <iostream>
#include<string>
using namespace std;

Friend *grow_array(Friend* p_array,int size);

struct Friend
{
int days=0;
string Name="  ";

};

int main()
{
int size = 5;
int x = 0;
string decision = "  ";
Friend *p_friend = new Friend[size];

while(true) {

cout << "Enter a friend : ";
cin >> p_friend.Name[x];

cout << "Num of days you didn't talk : ";
cin >> p_friend.days[x];

cout << "Do you want to add more friends(yes/no)? : ";
cin >> decision;

if(decision == "no")
break;

if(size == x+1)
{
for(int i = 0;i<size;i++){
p_friend[i] = grow_array(p_friend,size);}

cout << "Do you want to see the list(yes/no)? : ";
cin >> decision;

if(decision == "yes")
the_list(); // should pass a p_friend.days in, sort it, and based on the sorted numbers,
//its should print out Names that are under same index

}

}
}

Friend *grow_array(Friend* p_array,int cur_size)
{
Friend *p_new_friend = new Friend[cur_size*2];

for(int i = 0;i<cur_size;i++)
{
p_new_friend[i] = p_array[i];

}

delete p_array;
return p_new_friend;
}

void sort_array(int day_par)    // This is also where I got a problem. How can I pass array of days
// that is dynamicly allocated to a function
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.