This is C++ Program, I just want know if I use dynamic array. If not please fix
ID: 3676958 • Letter: T
Question
This is C++ Program, I just want know if I use dynamic array.
If not please fix this as dynamic array.
Also Please explain what is the different of static array and dynamic array. (Please show the example)
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
void fun(int a[], int size){
int k,j;
k=j=0;
for(int i=0; i<size; i++){
if(a[i]==0){
k=k+1;
}
if(a[i]==1){
j=j+1;
}
}
cout<<"number of 0: "<<k<<endl;
cout<<"number of 1: "<<j<<endl;
}
int main(){
srand(time(NULL));
int *a, size;
cout<<"Please type a size: ";
cin>>size;
a=new int [size];
for(int i=0;i<size; i++){
a[i]=rand()%2;
cout<<a[i];
}
cout<<endl;
fun (a,size);
return 0;
}
Explanation / Answer
of course you are using dynamic array by creating a pointer and object by int *a; and a=new int [size];
this type of array notation will allocate the memory at run time i.e dynamic but identifier(name) will be allocated at compile time.
where as static array memory will be allocated at the compile time along with identifier.
to make your code static use int a[10];
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.