Question

In: Computer Science

Problem: Write a C++ program that will implement and test the five functions described below that...

Problem:

Write a C++ program that will implement and test the five functions described below

that use pointers and dynamic memory allocation.

The Functions:

You will write the five functions described below. Then you will call them from the main

function, to demonstrate their correctness.

1. minimum: takes an int array and the array's size as arguments. It should

return the minimum value of the array elements. Do not use square brackets

anywhere in the function, not even the parameter list (use pointers

instead).

2. swapTimesTen: The following function uses reference parameters. Rewrite the

function so it uses pointers instead of reference variables. When you test this

function from the main program, demonstrate that it changes the values of the

variables passed into it.

int swapTimesTen (int &x, int &y)

{

int temp = x;

x = y * 10;

y = temp * 10;

return x + y;

}

3. doubleArray: takes an int array and the array's size as arguments. It should

create a new array that is twice the size of the argument array. The function

should copy the contents of the argument array to the first half of the new array,

and the contents of the argument array each multiplied by 2 to the second half

of the new array. The function should return a pointer to the new array.

!

4. subArray: takes an int array, a start index and a length as arguments. It

creates a new array that is a copy of the elements from the original array

starting at the start index, and has length equal to the length argument. For

example, subArray(aa,5,4) would return a new array containing only the

elements aa[5], aa[6], aa[7], and aa[8]. The function should return a pointer to

the new array. Do not call any other functions from this function. Hint: modify

the code from duplicateArray.

5. subArray2: takes an int array, a start index and a length as arguments. It

behaves exactly like subArray, however, you must define subArray2 as follows:

Add the code for the definition of the duplicateArray function from the lecture

slides for Unit 3 (or from the textbook) to your program. Add the code for the

subArray2 function given below to your program. Fill in the blanks with

expressions so that the function subArray2 behaves as the first subArray.

int *subArray2 (int *array, int start, int length) {

int *result = duplicateArray(__________, ___________);

return result;

}

DO NOT alter duplicateArray, DO NOT alter subArray2 as defined above.

Solutions

Expert Solution

int minimum(int *arr, int size) {
    int i = 0;
    int min = *arr;

    arr++;
    i++:
    while(i < size) {
        if(*arr < min) {
            min = *arr;
        }
        arr++;
        i++;
    }
    return min;
}

int swapTimesTen (int *x, int *y) {
    int temp = *x;
    *x = (*y) * 10;
    *y = temp * 10;
    return *x + *y;
}

int* doubleArray(int *arr, int size) {
    int *result = new int[2*size];

    for(int i=0; i<size; i++) {
        result[i] = arr[i];
    }
    for(int i=0; i<size; i++) {
        result[size + i] = 2*arr[i];
    }
    return result;
}

int* subArray(int *arr, int start, int length) {
    int *result = new int[length];

    for(int i=0; i<length; i++) {
        result[i] = arr[i + start];
    }
    return result;
}


int* subArray2(int *arr, int start, int length) {
    int *result = duplicateArray(arr + start, length);
    return result;
}

Related Solutions

Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
For this program you will implement the following utility functions to test mastery of C strings....
For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src); Please read the description of these functions carefully. In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
Write a C program with call to functions to produce the output given below. // the...
Write a C program with call to functions to produce the output given below. // the requirements are that there should be 5 files; intList.h, intList.c, hw3.h, hw3.c, and main.c. please only C and use Linked List. thank you. For the 5 different files, he wants it this way: 1) main.c This file just consists of the main() function, which only consists of the displayClassInfo() function call, and the runMenuHw3() function call. 2) intList.h This file would have the IntNode...
C++ Program: Create a 3x3 matrix of int values. Implement five functions, each expecting the matrix...
C++ Program: Create a 3x3 matrix of int values. Implement five functions, each expecting the matrix as parameter input. The first function must use a nested loop to prompt a user to enter each value. Show the indices when prompting for input and populate the matrix with these values. The second function outputs the matrix and formats the output to align all columns and rows to accommodate values up to 1000. The third function finds and returns the minimum value...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT