Suppose I have two arrays that look like this:
first array : 8 5 6 1 4 11 7
second array: 1 1 1 1 0 0 0
I want to sort the first array in descending order and the order of the elements in the second array should change in the same way as of first array and remove elements in the first array whose corresponding value in the second array is 0. The elements in the first array whose corresponding value is 0 should go into a different array. At last sum of both arrays should be printed.
so the final array should look like this:
first array : 8 6 5 1
second array: 1 1 1 1
sum= 8+6+5+1=20
new arrays with value:
first array : 11 7 4
second array: 0 0 0
sum = 11+7+4=22
Any ideas how to do this in c++
this is what i have done so far... i tried playing with waytoShort() method:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool wayToSort(int a, int b)
{
return a > b;
}
int main()
{
int n;
int sum;
sum=0;
cout<<"no. of elements in first array: "<<endl;
cin>>n;
//no. of elements in both array should be same.
vector<int> l(n);
vector<int> t(n);
for(int i=0;i<n;i++)
{
cin>>l[i]>>t[i];
}
sort(l.begin(),l.end(),wayToSort);
for(int i=0;i<n;i++)
{
cout<<l[i]<<" "<<t[i]<<endl;
}
for(int j= 0; j<n;j++)
{
sum = sum+l[j];
}
cout<<sum<<endl;
return 0;
}
This only sort the first array.