Answer to Question #292654 in C++ for Yaku

Question #292654

#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.


1
Expert's answer
2022-02-01T11:24:01-0500

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.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog