Question

In: Computer Science

Please answer in c++ 6.Define a function to find a given target value in an array,...

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).

Solutions

Expert Solution

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:


Related Solutions

PLEASE ANSWER IN C++ Given the starting point in a maze, you are to find and...
PLEASE ANSWER IN C++ Given the starting point in a maze, you are to find and mark a path out of the maze, which is represented by a 20x20 array of 1s (representing hedges) and 0s (representing the foot-paths). There is only one exit from the maze (represented by E). You may move vertically or horizontally in any direction that contains a 0; you may not move into a square with a 1. If you move into the square with...
Define the concepts which are given below a) Promotion     b) Target group                 c) Target audience      ...
Define the concepts which are given below a) Promotion     b) Target group                 c) Target audience       d) Public           
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
Write a C++ function to print any given std::array of a given type to the standard...
Write a C++ function to print any given std::array of a given type to the standard output in the form of {element 0, element 1, element 2, ...}. For example given the double array, d_array = {2.1, 3.4, 5.6, 2.9}, you'd print {2.1, 3.4, 5.6, 2.9} to the output upon executing std::cout << d_array; line of code. Your function mus overload << operator.
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
The cost function C and the price-demand function p are given. Assume that the value of...
The cost function C and the price-demand function p are given. Assume that the value of C(x) and p(x) are in dollars. Complete the following. C(x) = x2 100 + 7x + 2000; p(x) = − x 40 + 5 (a) Determine the revenue function R and the profit function P. R(x) = P(x) = (b) Determine the marginal cost function MC and the marginal profit function MP. MC(x) = MP(x) = Here is a picture of the problem: https://gyazo.com/6ce694b737f7dd4cfb20fbb9d1917420
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT