Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I have wrote this little program for dynamic memory allocation with some

ID: 3654601 • Letter: H

Question

Hello, I have wrote this little program for dynamic memory allocation with some help. I turned in my code to my professor and he stated: "Hello,


I have wrote this little program for dynamic memory allocation with some help.


I turned in my code to my professor and he stated:


"Set elementReturned to the number of elements in the dynamically allocated array, and return the dynamically allocated array to the caller."


I don't understand what he means Please Help!



Code:


#include<iostream>


#include<string>


using namespace std;



// function declaration


void show( const unsigned a[], unsigned items );


unsigned * evenValues( unsigned * elementsReturned,


const unsigned a[], unsigned elementsPassed );


bool die( const string & msg );



int main(){


unsigned a[] = {1,2,3,4,5,4,3,2,1};


unsigned elementsReturned;


unsigned elementsPassed;


elementsPassed = sizeof(a)/sizeof(*a);


elementsReturned = 0;


unsigned * ptr = evenValues(&elementsReturned,a,elementsPassed);


show( ptr, elementsReturned );


delete[] ptr;


} //main



unsigned * evenValues( unsigned * elementsReturned,


const unsigned a[], unsigned elementsPassed ){


unsigned * ptr = NULL;


for ( unsigned i = 0 ; i < elementsPassed ;i++){


if (a[i]%2 == 0 )


(*elementsReturned)++;


}



try{


ptr = new unsigned[*elementsReturned];


}catch( bad_alloc & ){


die( "allocation error" );


}



for (unsigned i = 0 , j = 0; i < elementsPassed ;i++){


if (a[i]%2 == 0 )


ptr[j++] = a[i];


}


return ptr;


} // evenValues



void show( const unsigned a[], unsigned items ){


cout<<"["<<items<<"]: ";


for ( unsigned i = 0 ; i < items ; i++ ){


cout<<a[i];


i == items -1 ? cout<< " " : cout<<", ";


}


}



bool die( const string & msg ){


cerr <<"Fatal error " <<msg <<endl;


exit( EXIT_FAILURE );


}

Explanation / Answer

#include<iostream>
#include<string>
using namespace std;

// function declaration

void show( const unsigned a[], unsigned items );
unsigned * evenValues( unsigned * elementsReturned,const unsigned a[], unsigned elementsPassed );
bool die( const string & msg );

int main(){

unsigned a[] = {1,2,3,4,5,4,3,2,1};

unsigned elementsReturned;

unsigned elementsPassed;

elementsPassed = sizeof(a)/sizeof(*a);

elementsReturned = 0;

unsigned * ptr = evenValues(&elementsReturned,a,elementsPassed);

show( ptr, elementsReturned );

delete[] ptr;

} //main


unsigned * evenValues( unsigned * elementsReturned,const unsigned a[], unsigned elementsPassed ){

unsigned * ptr = NULL;

for ( unsigned i = 0 ; i < elementsPassed ;i++){

if (a[i]%2 == 0 )

( *elementsReturned)++;

try{

ptr = new unsigned[*elementsReturned];

}

catch( bad_alloc & ){

die( "allocation error" );

}


for (unsigned i = 0 , j = 0; i < elementsPassed ;i++)

{

if (a[i]%2 == 0 )

ptr[j++] = a[i];

}

return ptr;

} // evenValues


void show( const unsigned a[], unsigned items ){

cout<<"[";
for ( unsigned i = 0 ; i < items ; i++ ){

cout<<a[i] ;
i == items -1 ? cout<< " " : cout<<", ";

}

}


bool die( const string & msg ){

cerr <<"Fatal error " <
exit( EXIT_FAILURE );

}

I think u have done the same thing

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote