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

Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 a

ID: 3763724 • Letter: P

Question

Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions)

Part 1 - Using Pointers

int largeArray (const int [], int);
int largePointer(const int * , int);
void fillArray (int * , int howMany);
void printArray (const char *,ostream &, const int *, int howMany);

const int low = 50; const int high = 90;

void main()
{
const int MAX_SIZE = 40;
int info[MAX_SIZE];
int used;
cout << "How many elements in the array? ";
cin >> used;
while ( used <= 0 || used > MAX_SIZE )
{ cout << "Enter a value between 1 and " << MAX_SIZE << "How many elements in the array? ";
cin >> used;
}
fillArray (info,used);
printArray("Array of data",cout,info,used);

cout << "The largest element using subscripts is " << largeArray (info,used) << endl;
cout << "The largest element using pointers is " << largePointer(info,used) << endl; }

//ENDMAIN

void printArray(const char * m,ostream & Out,const int * p, int hm)
{
Out << m << endl;
for(int i = 0; i < hm; i++)
{
Out << p[i] << endl;
}
}
void fillArray(int * p , int howMany) // as parameters, pointers and arrays are one and the same
{ int range = high - low + 1;
srand(time(0));
for( int i = 0; i < howMany; i++)
{
p[i] = rand() % range + low;
}
}
int largeArray(const int data[], int howMany) // use subscripts
{
int largest = 0;
// 1 - WRITE THE CODE TO FIND THE LARGEST NUMBER - USE SUBSCRIPTS TO ACCESS THE ELEMENTS
return largest;
}
int largePointer(const int * data, int howMany) // use pointers
{
int largest = 0;
// 2 - WRITE THE CODE TO FIND THE LARGEST NUMBER - USE POINTERS TO ACCESS THE ELEMENTS
return largest;
}

Part 2 - Using Dynamic Memory Allocation--------------------------------------------------------------------------------------------


void printArray(const char *,ostream &, const int *, int howMany);

void main()
{
int * info;
int used;
cout << "How many elements in the array? ";
cin >> used;
while ( used <= 0 )
{
cout << "Enter a value of 1 or more" << endl;
cin >> used;
}
// 3 - WRITE THE CODE TO ACQUIRE THE MEMORY FOR THE ARRAY FROM THE HEAP

// 4 - USING SUBSCRIPTS WRITE THE CODE TO FILL THE ARRAY WITH MULTIPLES OF 3 -
// - i.e. cell 0 holds 0, cell 1 holds 3, cell 2 holds 6, etc.

printArray("Array of data",cout,info,used);

// 5 - WRITE THE CODE TO RETURN THE MEMORY TO THE HEAP

}//ENDMAIN

void printArray(const char * m,ostream & Out,const int * p, int hm)
{
Out << m << endl;
for(int i = 0; i < hm; i++)
{
Out << p[i] << endl;
}
}

Explanation / Answer

1)

int largeArray(const int data[], int howMany) // use subscripts
{
int largest = 0;

for(int i=0;i<howMany;i++)
{
if(data[i]>largest)
{
   largest=data[i];//Assiging value greater than largest variable value
}

}

return largest;
}

2)

int largePointer(const int * data, int howMany) // use pointers
{
int largest = 0;

for(int i=0;i<howMany;i++)
{
if(*(data+i)>largest)
{
   largest=*(data+i);//Assiging value greater than largest variable value
}
}
return largest;
}

3&4)

void main()
{
int *info;
int used;
cout << "How many elements in the array? ";
cin >> used;
while ( used <= 0 )
{
cout << "Enter a value of 1 or more" << endl;
cin >> used;
}

info=(int*)malloc(6*sizeof(int));//Allocating Memory for 6 integers

for(int i=0;i<6;i++)
{
info[i]=3*i;//assiging multiples of 3
}

printArray("Array of data",cout,info,used);

free(info);//returning memory to heap;


}