Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 8, Problem 5P
Write a function named swapFrontBack that takes as input a vector of integers. The function should swap the first element in the vector with the last element in the vector. The function should check if the vector is empty to prevent errors. Test your function with
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Declare a vector of 15 doubles. Using a loop, set all the elements of your vector to
140.041.
Write a function named print Vector that returns nothing and takes by constant
reference a vector of doubles. The function should print every element of the vector,
followed by a newline. Use this function to print out your vector from Assignment 1
above.
Using the at() function, change the value of the first two elements to -1.0. Using the
push_back () function to add two new elements at the end of the vector with values
-10.3 and -20.3. Now use the sort () function on the vector. Using your function from
Assignment 2, print the vector.
Write a program that takes keyboard input from a user and puts what they type into a
string. Print out the number of characters they just typed. Change the first character of
the string to 'x'. Now append "<- you typed this!" to the string. Now print the
modified string with cout.
Caign
Declare a vector of 5 strings. In a loop, read from the keyboard into each…
Write a function that takes a vector of integers as an argument and returns a new vector
with the same elements as the input vector, but with all odd elements replaced by their
square root and all even elements replaced by their square. The function should use a
loop and the modulo operator to determine whether an element is odd or even.
To solve this problem, you will need to use a loop to iterate over the elements of the input
vector, use the modulo operator to determine whether each element is odd or even, and
use an if-else statement to determine what action to take for each element.
This question requires knowledge of loops, the modulo operator, and if-else statements in
R. It is a relatively advanced question and may be challenging for those who are not
familiar with these concepts.
Using R software, create a function that returns the square root of a vector x. However, it uses a tryCatch statement so that if x has negative numbers, then it returns x as it is and issues a warning that x contains negative numbers. Otherwise, it returns the square root of x
Chapter 8 Solutions
Problem Solving with C++ (10th Edition)
Ch. 8.1 - Prob. 1STECh. 8.1 - What C string will be stored in singingString...Ch. 8.1 - What (if anything) is wrong with the following...Ch. 8.1 - Suppose the function strlen (which returns the...Ch. 8.1 - Prob. 5STECh. 8.1 - How many characters are in each of the following...Ch. 8.1 - Prob. 7STECh. 8.1 - Given the following declaration and initialization...Ch. 8.1 - Given the declaration of a C-string variable,...Ch. 8.1 - Write code using a library function to copy the...
Ch. 8.1 - What string will be output when this code is run?...Ch. 8.1 - Prob. 12STECh. 8.1 - Consider the following code (and assume it is...Ch. 8.1 - Consider the following code (and assume it is...Ch. 8.2 - Consider the following code (and assume that it is...Ch. 8.2 - Prob. 16STECh. 8.2 - Consider the following code: string s1, s2...Ch. 8.2 - What is the output produced by the following code?...Ch. 8.3 - Is the following program legal? If so, what is the...Ch. 8.3 - What is the difference between the size and the...Ch. 8 - Create a C-string variable that contains a name,...Ch. 8 - Prob. 2PCh. 8 - Write a program that inputs a first and last name,...Ch. 8 - Write a function named firstLast2 that takes as...Ch. 8 - Write a function named swapFrontBack that takes as...Ch. 8 - Prob. 6PCh. 8 - Write a program that inputs two string variables,...Ch. 8 - Solution to Programming Project 8.1 Write a...Ch. 8 - Write a program that will read in a line of text...Ch. 8 - Give the function definition for the function with...Ch. 8 - Write a program that reads a persons name in the...Ch. 8 - Write a program that reads in a line of text and...Ch. 8 - Write a program that reads in a line of text and...Ch. 8 - Write a program that can be used to train the user...Ch. 8 - Write a sorting function that is similar to...Ch. 8 - Redo Programming Project 6 from Chapter 7, but...Ch. 8 - Redo Programming Project 5 from Chapter 7, but...Ch. 8 - Prob. 11PPCh. 8 - Write a program that inputs a time from the...Ch. 8 - Solution to Programming Project 8.14 Given the...Ch. 8 - Write a function that determines if two strings...Ch. 8 - Write a program that inputs two strings (either...Ch. 8 - Write a program that manages a list of up to 10...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Kilometer Converter Design a modular program that asks the user to enter a distance in kilometers, and then con...
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
// This code has an error. vectorstring strv = {"one", "two", "three"}; vectorstring:: iterator it = strv.cbegi...
Starting Out with C++ from Control Structures to Objects (9th Edition)
Describe four uses of a primary key.
Database Concepts (7th Edition)
What are the four important attributes that all professional software should possess? Suggest four other attrib...
Software Engineering (10th Edition)
What is hardware?
Starting Out with Python (3rd Edition)
This method attempts to convert a value to Integer. a. NumericConvert b. IntegerConvert c. Integer.TryParse d. ...
Starting Out With Visual Basic (8th Edition)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- a. Write a code which prints out a column vector by using the row vector containing the digits of your student number in order with programming conceptimethod.arrow_forwardWrite a function that removes duplicates from a vector. For example, if remove duplicates is called with a vector containing 1 4 9 16 9 7 4 9 11 (use these numbers) then the vector is changed to 1 4 9 16 7 11arrow_forwardThi is matlabarrow_forward
- 50. Create a vector variable vec; it can have any length. Then, write assignment statements that would store the first half of the vector in one variable and the second half in another. Make sure that your assignment statements are general, and work whether vec has an even or odd number of elements (Hint: use a rounding function such as fix).arrow_forwardIn c++ please Write the RemoveEvens() function, which receives a vector of integers as a parameter and returns a new vector of integers containing only the odd numbers from the original vector. The main program outputs values of the returned vector. Hint: If the original vector has even numbers, then the new vector will be smaller in length than the original vector and should have no blank element. Ex: If the vector passed to the RemoveEvens() function is [1, 2, 3, 4, 5, 6, 7, 8, 9], then the function returns and the program output is: [1, 3, 5, 7, 9] Ex: If the vector passed to the RemoveEvens() function is [1, 9, 3], hen the function returns and the program output is: [1, 9, 3] #include <iostream>#include <vector>using namespace std; vector<int> RemoveEvens(vector<int> nums) { /* Type your code here */} int main() { vector<int> input(9); input.at(0) = 1; input.at(1) = 2; input.at(2) = 3; input.at(3) = 4; input.at(4) = 5;…arrow_forwardWrite a function to print all the positive numbers for any given 1D vector. HINT: If vector is [0, -1, -2, 3, 9, -12, 6], your output must be as follows, 0 is positive3 is positive 9 is positive 6 is positivearrow_forward
- Finding the Minimum of a Vector Write a function to return the index of the minimum value in a vector, or -1 if the vector doesn't have any elements. In this problem you will implement the IndexOfMinimumElement function in minimum.cc and then call that function from main.cc. You do not need to edit minimum.h. Index of minimum value with std::vector Complete IndexOfMinimumElement in minimum.cc. This function takes a std::vector containing doubles as input and should return the index of the smallest value in the vector, or -1 if there are no elements. Complete main.cc Your main function asks the user how many elements they want, construct a vector, then prompts the user for each element and fills in the vector with values. Your only task in main is to call the IndexOfMinimumElement function declared in minimum.h, and print out the minimum element's index. Here's an example of how this might look: How many elements? 3 Element 0: 7 Element 1: -4 Element 2: 2 The minimum value in your…arrow_forwardComputer Science IN MATLAB Write a function CalFunc.m that can receive multiple math function as input arguments in the form of function handles. This function has one output argument which is the number of input arguments. This function creates a 1x100 vector with values between 0 and 10 as the x values, plots all function handles in a single plot with a proper legend. If the function is called without any input argument, the function will ask the user to enter an anonymous function for plotting. Your program should work as below:arrow_forwardWhat are values of vector "vec", if QUANTILE function returns following values? > quantile (vec) 0% 25% 50% 75% 100% 0.0 12.5 25.0 37.5 50.0arrow_forward
- Write a function called manualMedian that accepts an vector of numeric values and returns the median of that vector. The function cannot use the built-in median function. If the vector is empty, the function should return the error string "Empty vectors have no median value." Function e C Reset I MATLAB Documentation 1 function [outputArg1, outputArg2] = untitled4(inputArg1, inputArg2) 2 %UNTITLED4 Summary of this function goes here 3 % Detailed explanation goes here 4 outputArg1 = inputArg1; 5 outputArg2 = inputArg2; 6 end Code to call your function e C Reset 1 testVec1=[27 16 26 31 24 26 12 34 18 26 34 11 17 23 18 4 30 18 2 40 2 testVec2=[25.6367 12.1810 33.0248 35.3475 37.8149 15.6318 32.0528 6.2845 25.0066 27.9594 3.4348 21.2472 35 3 testVec3=[17 -8 12 8 -14 8 -14 18 16 1 20 -14 11 14 12 -12 -2 -19 -6 4 testVec4=[1.3015 17.0282 15.9633 1.7933 16.0450 -17.9269 12.3441 -6.6038 -10.8527 12.8961 -6.0706 -13.3812 -18. 5 testVec5=[]; MATLAB Editor to write code to run your solution. To…arrow_forward» Create a vector of random elements and find the following for the that vector: The maximum value in the vector The minimum value in the vector The mean of the vector Learn how to use the built-in functions max, min and mean in Octave.arrow_forwardYou may insert an element into an arbitrary position inside a vector using an iterator. Write a function, insert(vector, value) which inserts value into its sorted position inside the vector. The function returns the vector after it is modified. #include <vector>using namespace std; vector<int>& insert(vector<int>& v, int value){ ................ return v; }arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation; Author: Jenny's lectures CS/IT NET&JRF;https://www.youtube.com/watch?v=AT14lCXuMKI;License: Standard YouTube License, CC-BY
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License