LAB 1.pdf

School
American University of Sharjah*
*We are not endorsed by this school
Course
CMP 220L
Pages
5
Upload Date
Feb 10, 2024
Uploaded by MagistrateDog3983 on coursehero.com
American University of Sharjah School of Engineering Computer Engineering Department P. O. Box 26666 Sharjah, UAE Instructor: Dr. Mohammad Al-Zinati Lab Instructor : Eng. Sameer Alawnah Office : EB1-0012C Phone : 971-6-515-4940 e-mail : [email protected] Semester : Fall 2023 CMP 220L - Programming II 1D dynamic array with functions LAB 1 G00097325 Sara Eltawil Notes: Using ChatGPT will be considered a violation of the AUS integrity code. In the questions text and the given code instructions, the variables names are encapsulated between curly braces {}; print {x}, means print the values of the variable x. Question 1: In this question you will create, read and print a dynamic 1D array, complete all the following TO DO steps in the following code so that the program will generate the expected output: #include <iostream> using namespace std; void readArray( int * arr , int n ) { // TO DO 1: Implement the readArray function, this function should read {n} integers into the array {a} from the user. } void printArray( int * arr , int n ) { // TO DO 2: Implement the printArray function, this function should print {n} integers stored in the array {a} in order. } int main() { int * a; int n; // TO DO 3: Ask the user to enter the size of the array, then allocate memory with size n for the array a. // TO DO 4: Call the readArray function to read the array {a} content from the user. // TO DO 5: Print the array {a} content to the user. // TO DO 6: Do any required clean up. } A sample run is given below: Please enter how many elements you want to enter: 4 Please enter 4 integers : 1 2 3 4 You have entered : 1 2 3 4 1
My code: #include <iostream> using namespace std; void readArray( int * arr , int n ) { cout << "Please enter " << n << " integers: " ; for ( int i = 0; i < n ; i++) { cin>> arr [i]; } // TO DO 1: Implement the readArray function, this function should read {n} integers into the array {a} from the user. } void printArray( int * arr , int n ) { cout << "You have entered: " ; for ( int i=0; i < n ; i++) { cout << arr [i] << " " ; } // TO DO 2: Implement the printArray function, this function should print {n} integers stored in the array {a} in order. } int main() { int * a; int n; cout << "Please enter how many elements you want to enter: " ; cin >> n; a = new int [n]; readArray(a,n); printArray(a, n); delete []a; return 0; // TO DO 3: Ask the user to enter the size of the array, then allocate memory with size n for the array a. // TO DO 4: Call the readArray function to read the array {a} content from the user. // TO DO 5: Print the array {a} content to the user. // TO DO 6: Do any required clean up. } 2
Question 2: In this question you will remove all negative integers from a dynamic array and print it back to the user, since we cant chance the array size once created, we will do so by creating a temporary array with the required size, then copy the positive intergers to it, the last step will be deleting the old memory and make the array pointer to point to the new memory, at the end it will look like we deleted the negative numbers. Complete all the following TO DO steps in the following code so that the program will generate the expected output: #include <iostream> using namespace std; void readArray( int * arr , int n ) { // Use the implementation of this function from Question 1 } void printArray( int * arr , int n ) { // Use the implementation of this function from Question 1 } int main() { int * a; int size; // TO DO 1: Ask the user to enter the size of the array, then allocate memory with size n for the array a. // TO DO 2: Call the readArray function to read the array {a} content from the user. // TO DO 3: Print the array {a} content to the user. int neg = 0; // TO DO 4: Count the number of negative values (<0) in array {a} and store it in {neg_ int n_size = size - neg; int * n_arr; // TO DO 5: Allocate {n_size} integers for the array {n_arr} int j = 0; // TO DO 6: Copy the positive values from the array {a} into the array {n_arr}, you may need to use a second index {j} for copying. // TO DO 7: delete the current (old) memory for {a} and make it to point to the new memory located in {n_arr}, also change the {size} to have the value of the new size {n_size} cout << "The positives are : " ; printArray(a, size); delete[] a; } A sample output is given below: Please enter how many elements you want to enter: 6 Please enter 6 integers : 1 -5 4 -6 -4 9 You have entered : 1 -5 4 -6 -4 9 The positives are : 1 4 9 3
Page1of 5
Uploaded by MagistrateDog3983 on coursehero.com