Docsity

CMP220 - Programming II: Homework 1 - Pointers and Dynamic Arrays, Exercises of Computer Science

Toggle sidebar
bg1
College of Engineering
Department of Computer Science and Engineering
CMP220 – Programming II
Fall - 2018
Homework 1: Pointers and dynamic arrays
Date: 10/Sept/2018
Due: 16/Sept/2018
Write a C++ program that creates a dynamic 1D array. Prompt the use for
the size of the array and fill it in with values.
You are also asked to implement the following 3 functions:
double* addValue (double* arr_in, int& size, double newValue);
This functions creates a new array with size+1, copies old array values and
append the new value entered by the user. The new array is then returned
to the main function.
double* deleteValue(double* arr_in, int& size, double deleteValue);
This function creates a new array with size-1, copies old array values except
for the value to be deleted (deleteValue). You can assume that the array
contains unique value. Also note that if “deleteValue” does not exist in the
original array then the function will simply return the original array as is.
void printArr(double* arr_in, int size);
This function prints out the value of an array.
Here is an example main function (incomplete):
void main()
{
int size;
double* arr_in;
//read size and create array
printArr(arr_in, size);
arr_in = addEntry(arr_in, size, 9.1);
printArr(arr_in, size);
arr_in = addEntry(arr_in, size, 9.2);
printArr(arr_in, size);
arr_in = deleteEntry(arr_in, size, 9.1);
printArr(arr_in, size);
arr_in = deleteEntry(arr_in, size, -100);
printArr(arr_in, size);
pf3
pf4
pf5

Partial preview of the text

Download CMP220 - Programming II: Homework 1 - Pointers and Dynamic Arrays and more Exercises Computer Science in PDF only on Docsity!

College of Engineering

Department of Computer Science and Engineering

CMP220 – Programming II

Fall - 2018

Homework 1: Pointers and dynamic arrays

Date: 10/Sept/

Due: 16/Sept/

Write a C++ program that creates a dynamic 1D array. Prompt the use for

the size of the array and fill it in with values.

You are also asked to implement the following 3 functions:

double* addValue (double* arr_in, int& size, double newValue);

This functions creates a new array with size+1, copies old array values and

append the new value entered by the user. The new array is then returned

to the main function.

double* deleteValue(double* arr_in, int& size, double deleteValue);

This function creates a new array with size-1, copies old array values except

for the value to be deleted (deleteValue). You can assume that the array

contains unique value. Also note that if “deleteValue” does not exist in the

original array then the function will simply return the original array as is.

void printArr(double* arr_in, int size);

This function prints out the value of an array.

Here is an example main function (incomplete):

void main() { int size; double* arr_in;

//read size and create array printArr(arr_in, size); arr_in = addEntry(arr_in, size, 9.1); printArr(arr_in, size); arr_in = addEntry(arr_in, size, 9.2); printArr(arr_in, size); arr_in = deleteEntry(arr_in, size, 9.1); printArr(arr_in, size);

arr_in = deleteEntry(arr_in, size, -100); printArr(arr_in, size);

SOLUTION:

#include

using namespace std; double* del(double* arr, int& size, double deleteValue); double* addValue(double* arr, int& size, double newValue); void printArr(double* arr, int size);

void main() { int size, i = 0; cout << "Enter the size of the array: " << endl; cin >> size; double * arr; arr = new double[size]; double newValue; double deleteValue;

cout << "Enter the values you want: " << endl; for (i = 0; i < size; i++) { cin >> arr[i]; }

cout << "The current array is: " << endl; printArr(arr, size);

cout << "Enter the new value that you want to add: " << endl; cin >> newValue;

cout << "The added array is: " << endl; addValue(arr, size, newValue);

cout << "Enter the value that you want to delete: " << endl; cin >> deleteValue; cout << "The deleted array is: " << endl; del(arr, size, deleteValue);

return 0; }

void printArr(double* arr, int size)

{ for (int x = 0; x < size; x++) { cout << arr[x] << endl; } //is this supposed to print for all? how if diff. sizes?

#include

using namespace std; double* deleteValue(double* arr, int& size, double deleteValue); double* addValue(double* arr, int& size, double newValue); void printArr(double* arr, int &size);

void main() { int size; cout << "Enter the size of the array: " << endl; cin >> size; double * arr; arr = new double[size];

double newValue;

int i = 0;

cout << "Enter the values you want: " << endl; for (i = 0; i < size; i++) { cin >> arr[i];

cout << "The current array is: " << endl; printArr(arr, size);

cout << "Enter the new value that you want to add: " << endl; cin >> newValue;

cout << "The added array is: " << endl; arr = addValue(arr, size, newValue); printArr(arr, size);

cout << "Enter the new value that you want to add: " << endl; cin >> newValue;

cout << "The added array is: " << endl; arr = addValue(arr, size, newValue); printArr(arr, size);

double* addValue(double* arr, int& size, double newValue) { double *arr2; size++; arr2 = new double[size];

for (int j = 0; j < (size - 1); j++) { arr2[j] = arr[j]; }