Docsity

Programming: Deleting Arrays, Adding to Arrays, Study notes of Computer Science

Partial preview of the text

Download Programming: Deleting Arrays, Adding to Arrays and more Study notes Computer Science in PDF only on Docsity!

CMP 220 _ HW _ 1

Fall 2018

STUDENT: Rawan Suwwan

ID: g

Professor: T. Shanableh

Date submitted: 16/09/

SOLUTION:

(see end of document for original question)

#include // Rawan Suwwan ID : 00075916 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; cout << "Enter the size of the array: " << endl; // asks user for size 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];^ //filling array

cout << "The array you entered is: " << endl; printArr(arr, size); //calling function of print

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

cout << "The added array is: " << endl; arr = addValue(arr, size, newValue); //calling function of addvalue and assigning it to arr printArr(arr, size); cout << endl; cout << "Enter the new value that you want to add: " << endl; cin >> newValue;

cout << "The added array is: " << endl; arr = addValue(arr, size, newValue); //the last arr has the new &size and so on printArr(arr, size);

cout << endl;

cout << "Enter the value that you want to delete: " << endl; cin >> deleteValue; cout << "The deleted array is: " << endl; arr = del(arr, size, deleteValue); //calling delete and assigning arr = arr3 from that function printArr(arr, size);

cout << endl;

if (arr[i] != deleteValue) //copies all numbers except for the deletevalue { arr3[j] = arr[i]; j++; } }

delete[] arr; //delete arr so that we can assign it to arr

return arr3;

void printArr(double* arr, int size) { for (int x = 0; x < size; x++) { cout << arr[x] << "^ "; }

cout << endl;

Original problem: