#include <iostream>
using namespace std;
int *my_array;
int array_length;
void FunA(int pos, int input){
my_array[pos] = input;
}
int FunB(){
int n;
cout<<"enter lenth of the array: ";
cin>>n;
my_array = new int[n] ;
return n;
}
int main (){
array_length = FunB();
FunA(array_length-1,20);
delete [] my_array;
return 0;
}
a) Explain the code snippet above in details.
The beginning of the program is main functions. Also we have 2 global variables here
int* my_array and int array_length that means we can access them from any part of this file.
We can see that in main first we call function FunB(), which asks the user to insert the length of the array, after this function allocates memory for this entered length to the poiner from global scope and returns the length. We assign it to array_length global variable. Then we call FunA with two arguments, in FunA we just assign value to oisition in an array. After that we release memory, allocated at the beginning of the program.
Comments
Leave a comment