Help in very begining of my c++ knowlege

I need help with entering N amount of numbers which should be sorted in PAIRS of 2 and I should show the pair with biggest total.

Thanks in advice to any single one of you who is willing to help me!
Last edited on
closed account (SECMoG1T)
What do you means bysorted in PAIRS by 2]
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.
closed account (SECMoG1T)
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! :(
Last edited on
closed account (SECMoG1T)
Mybe an example on how you can approach your problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

#include <iostream>

using namespace 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
    }


That will be good to start with modify that
Last edited on
closed account (SECMoG1T)
Lol XD I din't see yr
no arrays involved! :(
hehe
But my example is more than a beginning you can modify that with your datastructure of choice
Thank you so much! You're a lifesaver ! Thanks a lot !
Topic archived. No new replies allowed.