The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.

To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

Problem Solving with C++

Problem Solving With C++

Walter Savitch ,julia Lobur
Self-test Exercises
Introduction To Computers And C++ Programming



What are the five main components of a computer?


The five main components of a computer are as follows:

  1. Processor (CPU)
  2. Main memory
  3. Secondary memory
  4. Input devices
  5. Output devices
4 0


issac dunbar

Written in C++ Please

In an ancient land, the beautiful princess Eve had many suitors. She decided on the following procedure to determine which suitor she would marry. First, all of the suitors would be lined up one after the other and assigned numbers (first suitor is number 1, second is number 2, etc.) Starting at the first suitor, she would then count three suitors down the line and the third suitor would be eliminated from winning her hand and removed from the line. Eve would then continue, counting three more suitors, and eliminating every third suitor. When she reaches the end of the line and the count is not yet 3, she would continue counting from the beginning (in a round-robin approach.)Create a struct named Suitor with two member variables, as follows:

-- Name - a string to contain the name of the suitor

-- Number - an integer to show the original ordinality (position) in the list of suitors. This number will not change throughout a program run except if assigned a value by the setOrdinality function below.

Create a class named Princess containing the following:

-- A private variable of vector type called SuitorList, which contains elements of type Suitor.

-- A private variable of integer type called NumOfSuitors, which contains the number of suitors in the SuitorList.

-- A private variable of integer type called WinningSuitor, which contains the Number of the predicted winning Suitor. See setWinningSuitor function below for additional information on how this variable is used.

-- A default constructor that initializes NumOfSuitors and WinningSuitor to zero.

-- A constructor that takes an argument of type Suitor and adds it to the SuitorList, incrementing NumOfSuitors. The Suitor will only contain data for the Name field; initialize the Number and WinningSuitor fields to zero.

-- A function named getNumOfSuitors that returns NumOfSuitors.

-- A function named addSuitor that takes a Suitor as an input and adds it to the end of SuitorList. The Suitor will only contain data for the Name field; initialize the Number field to zero. Do not add a suitor if the name already exists in SuitorList (use the overloaded equality operator described below to check if the suitor is in the list already.)

-- A function named deleteSuitor that takes an integer argument as the position of the suitor to be removed from the list. Keep in mind that positions start from 1, not 0. Make sure that the argument passed to this function is valid, i.e. it is within range. If it’s outside of range, output a message stating this fact.

-- A function named deleteSuitor that takes a string argument as the name of the Suitor and removes it from the list. Use the getSuitor function described below to search the entire list and check if the name is in the list. Stop searching and delete the entry if the name is found. If the name is not in the list, output an error message.

-- A function named getSuitor that takes an argument for the position of the suitor and returns the name of the suitor at that position.

-- An overloaded equality operator (==) to compare if two Suitor entries are the same

-- A function named setOrdinality that assigns position numbers to each suitor’s Number field in the SuitorList. Position numbers start at 1 and are assigned in the order they were added to the SuitorList.

-- A function named addCheatingSuitor that takes a Suitor and an integer position as inputs and adds the suitor to the given position in SuitorList, shifting the positions of all other suitors at indices larger than position down by one (Hint: This needs to be done before inserting the cheating suitor’s name in the vector.) Do NOT use the insert() function from the vector class, you need to create your own. The Suitor will only contain data for the Name field; initialize the Number field to zero. Run setOrdinality to assign new numbers for each of the suitors after the cheating suitor has been added to the vector. Do not add a suitor if the name already exists in SuitorList (use the overloaded equality operator described above to check if the suitor is in the list already.)

-- A function named setWinningSuitor that takes NumOfSuitors as an argument and predicts the suitor who will win Eve’s heart, storing the Number of the suitor in WinningSuitor. This function acts as an algorithm to cheat in the game and insert a new name at a given position using the addCheatingSuitor function above (this allows the malicious suitor insert himself in the vector at the winning location). When determining the position at which the cheating suitor should be added, include the cheating suitor in the calculation as an additional suitor, i.e. use NumOfSuitors+ 1 as the total number of suitors. Run through several values for the total number of suitors in order to determine the winning position for any given number of suitors and create a generalized formula such that it is valid for any number of suitors (it is important that you generalize this formula, do not hardcode all values). Assume there will be at least 2 suitors total in this game and pay attention to special cases.

The following functions may be useful for this problem. It is used as shown below:

vectorName.erase(positionNumber) //erases entry at positionNumber and position

numbers start at 0.

vectorName.push back(value) //value is a variable that is added to the end of the vector

struct Suitor



class Princess



Post the discussion to improve the above solution.