Question

In: Computer Science

C++ Write a toVector function that takes in a C-style pointer of any type and a...

C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me

Solutions

Expert Solution

Code:

#include<iostream>
#include<vector>
using namespace std;

//function toVector definition.
//Since the array is int array the return type should be vector<int>
vector<int> toVector( int *arr, int n)
{
    //declare a new int vector xvec
    vector<int> xvec;
    //using for loop from 0 to n
    for(int i=0; i<n; i++)
    {
        //add elements of array to xvec vector using push_back()
        xvec.push_back(*arr);
        //increment the var1++ so that it goes to next address of elements
        arr++;
    }
    //return xvec
    return(xvec);
}

int main()
{
    //initialize an array
     int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
     int n = sizeof(arr)/sizeof(arr[0]);
     //store the returned vector from function in another vector res
     vector<int> res= toVector(arr, n);
     //using for loop display the vector elements
     cout << "The elements of vector are:\n";
     for(int i = 0; i< n;i++)
     cout << res[i] <<"\n";
     return 0;
}

O/P:

The elements of vector are:
10
20
30
40
50
60
70
80
90
100

Code screenshot:

O/P screenshot:


Related Solutions

C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array using a template. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules. Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string. The fields should be...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Please write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
Language C++ Thank You! -Write a function getMeASentence() that takes zero arguments, and its return type...
Language C++ Thank You! -Write a function getMeASentence() that takes zero arguments, and its return type is string. This function will return a randomly generated sentence that abides by certain rules. -The rules of this sentence are that there are 5 randomly generated lowercase letters, a space, a randomly generated inequality symbol, a space, and a randomly generated 1 digit number. -In this context, an inequality symbol is understood to mean the "<", ">", or "=" symbol. -Write a function...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a pointer to an int variable. It computes the length of the string and updates the int variable with the length. stringCopy() - Copies one string onto another using pointers #include<stdio.h> #define MAX_STR_LEN 2048 void stringLength(char *str, int *len) { // This function computes the length of the string // at the address in the pointer *str. Once the length // has been determined, it...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT