Question

Assume you have a necklace of stones. Some of the stones have positive value and some have negative value. You have the oppor

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <vector>
using namespace std;
int find_value_of_necklace(vector<int> temp)//function to find new value of necklace
{
    int ret = 0;//return value
    bool negative = 1;//initialy we assume all values are negative
    for (auto it = temp.begin(); it != temp.end(); it++)
    {
        if (*it >= 0)//if any value is positive
            negative = 0;//negative=false
        ret += *it;
    }
    if (negative)//if all values are negative
        return 0;
    return ret;
}
void should_snip(vector<int> temp)//function to calculate if i should snip necklace or not
{
    bool negative = 1, positive = 1;//initially we
    for (auto it = temp.begin(); it != temp.end(); it++)
    {
        if (*it >= 0)//if any value is positive
            negative = 0;
        if (*it < 0)//if any value is negative
            positive = 0;
    }
    if (negative)//if all values are neagtive
    {
        cout << "Throw the necklace away\n";
        return;
    }
    if (positive)//if all values are positive
    {
        cout << "Do not snip\n";
        return;
    }
    cout << "Snip\n";
}
int main()
{
    vector<int>necklace;
    int size,temp;
    cout<<"Enter number of stones in necklace\t";
    cin>>size;
    cout<<"Enter space saparated value of stones\n";
    for(int i=0;i<size;i++)
    {
        cin>>temp;
        necklace.push_back(temp);
    }
    cout<<"New value of necklace is\t"<<find_value_of_necklace(necklace)<<endl;
    should_snip(necklace);
}

Enter number of stones in necklace Enter space saparated value of stones New value of necklace is Do not snip PS Efixer> a. e

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Assume you have a necklace of stones. Some of the stones have positive value and some have negative value. You have the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • You are interested in analyzing some hard-to-obtain data from two separate databases. Each database contains n...

    You are interested in analyzing some hard-to-obtain data from two separate databases. Each database contains n numerical values—so there are 2n values total—and you may assume that no two values are the same. You’d like to determine the median of this set of 2n values, which we will define here to be the nth smallest value. However, the only way you can access these values is through queries to the databases. In a single query, you can specify a value...

  • THIS QUESTION MUST BE ANWSERD WITH CLEAR ENGLISH PSEUDOCODE! You have a set of N coins...

    THIS QUESTION MUST BE ANWSERD WITH CLEAR ENGLISH PSEUDOCODE! You have a set of N coins in a bag, each having a value between 1 and M, where M ? N. Some coins may have the same value. You pick two coins (without replacement) and record the sum of their values. Determine(algorithm ) what possible sums can be achieved, in O(M log M ) time. For example, if there are N = 3 coins in the bag with values 1,...

  • Specifically answer this prompt: Have you ever experienced a negative or positive externality? Specifically identify the...

    Specifically answer this prompt: Have you ever experienced a negative or positive externality? Specifically identify the "consumers" and the "producers" in the market, and how you were affected by that market. Specifically explain what the positive or negative externality is, and if possible give a dollar value of that positive or negative externality. Use either government regulation or the Coase Theorem in your response and be specific on the type of regulation (e.g. taxes, subsidies, command & control). One example...

  • List some of the common perceptions, both positive and negative, that physicians have about managed care....

    List some of the common perceptions, both positive and negative, that physicians have about managed care. What steps can MCOs take to optimize their relationship with provider in light of these perceptions? (You may want to respond two ways, “positive perceptions,” and “negative perceptions”)

  • Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algo...

    need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...

  • For your second co-op you are developing the next state-of-the- art flight search engine. You have...

    For your second co-op you are developing the next state-of-the- art flight search engine. You have the list of all n daily flights in the world. 1. For each flight you have starting city, destination city, and price (a positive number). Give an efficient algorithm to compute the cheapest trip from start city to end city t . You can use any number of flights in your trip. 2. Same as part 1, but if multiple trips with the same...

  • There is very deep and strong river and the only way one person can cross the...

    There is very deep and strong river and the only way one person can cross the river is to jump on the stones. There are N-l stones on river from one side to another, placed in equal distances. If we give numbers to stones, we have 1, 2, 3,...,N-1 and the other side of the river can be numbered as N. A person can only jump to stone infront of him, or to the next one or to the next...

  • Assume that you have two Possible outcomes: 50% chance of a positive cash flow of $2,000 50% chan...

    Assume that you have two Possible outcomes: 50% chance of a positive cash flow of $2,000 50% chance of a positive cash flow of $1,000 What is your Expected Cash Flow? Then, how often will you actually get that value? That is, what is the probability, the percentage of the time, that you will actually get the cash flow that you expected? Assume that you have two Possible outcomes: 50% chance of a positive cash flow of $2,000 50% chance...

  • - Give one example of a negative externality that you have encountered in your life/worklife, describe...

    - Give one example of a negative externality that you have encountered in your life/worklife, describe how that affects you, and give suggestion on what you think should have been done with it. - Give one example of a positive externality that you have encountered in your life/worklife, describe how that affects you, and give suggestion on what you think need to be done to maintain it.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT