Requirement: create one project for each problem; add comments to your code. Sub
ID: 3549099 • Letter: R
Question
Requirement: create one project for each problem; add comments to your code.
Submission: after finishing the all the problems, paste the code and snapshot the output in
one file, submit it to the dropbox.
===================================================
Problem 1:
Consider the following statement:
int *num;
a) Write the C++ statement that dynamically creates an array of 10 components of
type int and num contains the base address of the array
b) Write the C++ code that inputs data into the array num from the standard input
device
c) Write the C++ statement that deallocates the memory space of array to which
num points
Problem 2: Write the copy-array procedure by a function
int main()
{
int *a, *b;
int lena, lenb;
cout << "Enter the length of a: ";
cin >> lena;
a = new int[lena];
// Assume some values are loaded in a.
b = CopyArray(a, lena, lenb); // WRITE THIS FUNCTION!!
// After the function, 'b' is a copy of 'a' and
// 'blen' represents the length of 'b'
}
===================================================
Problem 3 ():
Write the function: void shuffle(int ar[], int size);
This function "shuffles" the elements in the array pointed by 'ar' (and whose length is
'size'). To do so, you randomly choose TWO indices (NOT ONE) and swap the elements
Explanation / Answer
Problem 1:
Consider the following statement:
int *num;
a) Write the C++ statement that dynamically creates an array of 10 components of
type int and num contains the base address of the array
num = new int[10];
b) Write the C++ code that inputs data into the array num from the standard input
device
for(int i=0; i<10; i++)
{
cout <<"Enter element " << (i+1) << " : ";
cin >> num[i];
cout << endl;
}
c) Write the C++ statement that deallocates the memory space of array to which
num points
delete[] num;
Problem 2: Write the copy-array procedure by a function
#include<iostream>
using namespace std;
int* CopyArray(int a[],int lena,int& lenb)
{
int *pointer_to_array = new int[lena];
lenb = lena;
for(int i=0; i<lena; i++)
pointer_to_array[i] = a[i];
return pointer_to_array;
}
int main()
{
int *a, *b;
int lena, lenb;
cout << "Enter the length of a: ";
cin >> lena;
a = new int[lena];
// Assume some values are loaded in a.
b = CopyArray(a, lena, lenb); // WRITE THIS FUNCTION!!
// After the function, 'b' is a copy of 'a' and
// 'blen' represents the length of 'b'
}
===================================================
Problem 3 ():
This function "shuffles" the elements in the array pointed by 'ar' (and whose length is
'size'). To do so, you randomly choose TWO indices (NOT ONE) and swap the elements
Write the function:
void shuffle(int ar[], int size)
{
for(int i=0; i<size; i++)
{
int rand1 = rand()%size;
int rand2 = rand()%size;
int temp = ar[rand1];
ar[rand1] = ar[rand2];
ar[rand2] = temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.