In: Computer Science
Please answer in c++
6.Define a function to find a given target value in an array, but use pointer notation rather than array notation whenever possible.
7.Write a swap function, that swaps the values of two variables in main, but use pointers instead of reference parameters.
8.Write a function that takes an array of ints and its size as arguments. It should create a new array that is the same size as the argument. It should set the values in the new array by adding 10 to each element in the original array. The function should return a pointer to the new array. (Make sure you use dynamic memory allocation and try to get the syntax right).
6.
The required function is given below:
//#function to find the target value
int findTarget(int arr[], int size, int target)
{
for(int i=0; i<size; i++)
{
if(target == *(arr+i))
return i;
}
return -1;
}
The screenshot of the above function is given below:
7.
The required function is given below:
//function to swap two values
void swap(int *a, int *b)
{
//temp variable declaration
int temp;
//swap the values
temp = *a;
*a = *b;
*b = temp;
}
The screenshot of the above function is given below:
8.
The required function is given below:
//function to add 10 to the each element of the array
int * findTarget(int arr[], int size)
{
int *ptr = (int *) malloc(size * sizeof(int));
for(int i=0; i<size; i++)
{
*(ptr+i) = *(arr+i) + 10;
}
return ptr;
}
The screenshot of the above function is given below: