Question

In: Computer Science

Create a function that takes a vector of vectors as an argument. Each inner vector has...

Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number.

Examples:

sum_fractions({{18, 13}, {4, 5}}) ➞ 2

sum_fractions({{36, 4}, {22, 60}}) ➞ 9

sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11

Notes Your result should be a number not string.

Code in C++ language

Solutions

Expert Solution

CODE:

#include <bits/stdc++.h>
using namespace std;

//defining sum_fractions function
int sum_fractions (vector<vector<int>>v)
{
// Initialising sum variable to store the sum of the fractions  
double sum=0.0;

// storing the size of the vector in size
int size = v.size();


// iterating through all the vector pairs
for(int i=0;i<v.size();i++)
{   
        // adding the fraction to the sum, typecasting the numerator to double to get accurate results
        sum = sum + (double)v[i][0]/v[i][1];
}

// returning the rounded value
return round(sum);

}

SAMPLE TEST CODE:

OUTPUT:


Related Solutions

Create a function called merge that takes two int vectors (x and y) as argument and...
Create a function called merge that takes two int vectors (x and y) as argument and returns an int vector (z) that is the result of merging the two vectors x and y. Here is an example to explain how the merge function works: If the vector x has the following values: 1 2 3 4 5, and the vector y has the following: 6 7 8 9 10 11 12, then the merging vector z will have: 1 6...
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
In basic C++ Create a function that takes in a vector, sorts it, then outputs to...
In basic C++ Create a function that takes in a vector, sorts it, then outputs to the console the result.
Add a new function that takes a phrase as an argument and counts each unique word...
Add a new function that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the...
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the area of a regular hexagon given the length of the side. Show that the function works in a worksheet by inserting the formula somewhere.
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT