sorted in pairs OF 2(my bad) . I understand it that way:
I should enter even amount of numbers and they should be sorted 2 by 2 in pairs,then I should check which pair of all has the biggest total and i should show it out.
Well its okay now I get you , are you aware of datastructures such as array, containers such as vectors
this is exactly what you need though you can also use simple arrays, structs. Have you attempted to solve your problem? or are you stuck at the beginning
I'm stuck at the begining...Searching for any possible way to solve it! I know how to find the biggest total and how to show it out but i dont seem to know how to enter these numbers in pairs of 2 :(
Note: Though of arrays but they told me - no arrays involved! :(
#include <iostream>
usingnamespace std;
struct pairs/// i'll use structure to store pairs in an array
{
int one;
int two;
}
void sort_here (pairs& p_obj)/// helping function takes a pairs object and sorts its variable in ascding
{ /// order
int temp;
if (p_obj.one> p_obj.second)
{
temp=p_obj.one;
p_obj.one=p_obj.two;
p_obj.two=temp;
}
}
void mysort (pairs *ptr, int size)///takes a pointer to an array and the size of array then sorts each pair
{
for (size_t i =0; i <size; i++)
sort_here (ptr [i]);
}
int find_great (pairs *ptr, int size)/// returns index of greatest pair
{
int sum=ptr [0]-> one+ptr [0]-> two, index=0;
for (int i=0; i <size; i++)
{
if ((ptr [i]-> one+ptr [i]-> two)> sum)
{
sum=ptr [i]-> one+ptr [i]-> two;
index=i;
}
}
return index;
}
int main()
{
int n;// no of integers pairs
int index_greatest; //will contain the index with the greatest pair
cout <<" how many pairs of numbers do you want to store "; cin>> n;
pairs *arr_ptr=new pairs[n]; //this is a dynamic array of the size specified by user; however this are no
///better than containers such as vectors
for (int count=0;count <n; count++)
{
cout <<" enter pair "<<count+1<<" ";
cin>> arr_ptr [count]->one>> arr_ptr [count]->two;
}
mysort(arr_ptr, n);
index_greatest=find_great (arr_ptr, n);
///print the pair
cout <<"the greatest pair in the collection is\n";
cout <<arr_ptr[index_greatest]-> one <<" "<<arr_ptr [index_greatest]-> two;
delete arr_ptr;///always good to return memory to the owner
}