Question

In: Computer Science

What is the function for printVector that takes a vector of ints, and prints the values...

What is the function for printVector that takes a vector of ints, and prints the values one after the other to screen, with commas in between the values.

For example,

vector<int> numbers{0,3,9};
printVector(numbers);

...should print out:

0,3,9

Solutions

Expert Solution


#include <iostream>
#include <vector>

using namespace std;

void printVector(vector<int> numbers)
{
int i;
cout << "The elements are : ";
for(i = 0; i < numbers.size(); i++)
{
if(i != numbers.size() - 1)
cout << numbers[i] << ", "; // printing elements in the vector
else
break;
}
cout << numbers[i];
}

int main()
{
vector<int> numbers;
int n, x;
cout << "Enter the no of elements in the vector : ";
cin >> n; // read the size of the elements
  
cout << "enter the elements : ";
for(int i = 0; i < n; i++)
{
cin >> x; // read the elements
numbers.push_back(x); // and push them into the vector
}
printVector(numbers); // printVector function
return 0;
}
OUTPUT :


Related Solutions

C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
This function takes in a string as a parameter and prints the average number of characters...
This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below). Assume a sentence always ends with a period (.) or when the string ends. Assume there is always a blank space character (" ") between each word. Do not count the blank spaces between words or the periods...
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++...
In C++, create two vectors, one a vector of ints and one a vector of strings....
In C++, create two vectors, one a vector of ints and one a vector of strings. The user will fill each one with data, and then your program will display the values. Copy the template file /home/cs165new/check10a.cpp to your working directory. For this assignment, in order to focus our practice on the syntax of the vectors, you can put all of your code in main. Create and display a vector of ints as follows: Create a vector of ints. Prompt...
"Write a function named "firstLast2" that takes as input a vector of integers. The function should...
"Write a function named "firstLast2" that takes as input a vector of integers. The function should return true if the vector starts and ends with the digit 2. Otherwise, it should return false. Test your function with vectors of different length and with the digit 2 at the beginning of the vector, end of the vector, middle of the vector, and missing from the vector." Additional Requirements: >Must use a loop allowing the user to continue until he/she quits. >Read...
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.
(10 pts) Write a function called isInBetween that takes 3 ints, say n, low and high,...
(10 pts) Write a function called isInBetween that takes 3 ints, say n, low and high, and returns a boolean; the function returns true if the first parameter (n) is between the second and third parameters (low and high, INCLUSIVE), and false otherwise. Can safely assume that low is lower than high.
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints a message with information about the total amount owed and how much the tip was. As a reminder, the tip amounts are 10%, 15% and 20% for stingy, regular, and generous customers. And the tax amount should be 7%. The total amount is calculated as the sum of two amounts: check_amount = base_cost*1.07 tip_amount = tip_percentage*check_amount To receive full credit, you must use string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT