//main.cpp #include <iostream> #include <queue> #include <string> using namespac
ID: 3816597 • Letter: #
Question
//main.cpp
#include <iostream>
#include <queue>
#include <string>
using namespace std;
vector<int> checkSums(vector<int> v){
//write your code here
return sums;
}
int main(){
vector<int> v1;
v1 = {20,32,12,65,0,19,76,15,2,39};
vector<int>answer = checkSums(v1);
cout << "The sum of the sum of the largest two elements is " << answer[0] <<endl;
cout << "The sum of the sum of the smallest two elements is " << answer[1] <<endl;
return 0;
}
The Problem Task: You are given a vector of integers. Implement a priority queue to find the sum of the largest two elements. Similary find the smallest two elements and find their sum. Example output: 3 vec 150,2, 20,5,500, 11 sum of maxes 550 sumDiff of mins Compile and Test A complete Makefile and a main. cpp file containing one simple test has been provided for you. To compile and run, run: make /mainExplanation / Answer
The approach is to sort elements first, and then sum the two biggest numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[10],temp,j,n, sum;
cout<<"Enter the size of vector: ";
cin>>n;
cout<<"Enter elements: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
sum=a[n-1]+a[n-2]
cout<<" sum of the maxes: ";
cout<<sum;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.