In C write a program to dynamically allocate a couple of arr
In C++, write a program to dynamically allocate a couple of arrays using and and see what they contain:
a.Allocate an array of integers 5 elements long using .
a.Remember that takes 2 parameters, the number of elements and the size (in bytes) for each element
b.You may need to the call to if your compiler doesn’t like assigning the address returned by to an pointer.
b.Set the 3rd element of the array to 259. (That’s 256 + 3…what is it in binary?)
c.Print the contents of the array to verify that they have been initialized to zeros (except for the 3rd element, which you just changed).
d.Allocate an array of doubles 10 elements long using .
a.Be sure to allocate enough bytes! Remember that you tell how many bytes you need, not how many elements.
e.Set the 3rd element of the array to
f.Set the 4th element of the array to
g.Print out the contents of the array, to see if they have been initialized or not
a.(You may still get zeros…unused memory often contains zero’s, but there’s no guarantee with )
h.return the memory for both arrays to free-store using …)
Solution
Please find the code below....
#include<bits/stdc++.h>
#define endl \'\ \'
using namespace std;
int main()
{
int *a=new int[5];
a[2]=259;
for(int i=0;i<5;i++)
cout<<a[i]<<\" \";
cout<<endl;
float *f=(float *)malloc(10*sizeof(float));
f[3]=12.34;
f[4]=13.45;
for(int i=0;i<5;i++)
cout<<f[i]<<\" \";
free(a);
free(f);
return 0;
}
