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

Pointers.h code #ifndef POINTERS_H #define POINTERS_H #include using namespace s

ID: 3704568 • Letter: P

Question

Pointers.h code

#ifndef POINTERS_H
#define POINTERS_H


#include

using namespace std;

class Pointers {
public:
Pointers(){
a = 5;
b = NULL;
c = 10;
}

Pointers(int a, int* b){
this->a = a;
this->b = b;
c = 0;
}

int* getA(){
return &a;
}

int* getB() const{
return b;
}

int getC() const{
return c;
}

void setB(int* b){
this->b = b;
}

void setC(){
c = *b;
}
  
private:
int a;
int* b;
int c;
  
};
#endif

manip.h code

#ifndef MANIP_H
#define MANIP_H

// Author: Fill in your name
// Source File: manip.h
// Description: A set of functions where you should manipulate
// the passed parameters to change the object in a specific way,
// so that Pointers_test.h passes all tests.

#include "Pointers.h"

// A little something to get you going
void manip1(Pointers* p){
*(p->getA()) = 10;
}

void manip2(Pointers* p){
// TODO: Fill me in
*(p->getA()) = 45;
}

void manip3(Pointers* p){
// TODO: Fill me in
  
}

void manip4(Pointers* p, int* other){
// TODO: Fill me in
}

void manip5(Pointers* p, int* other){
// TODO: Fill me in
}

void manip6(Pointers* p){
// TODO: Fill me in
}

void manip7(Pointers* p){
// TODO: Fill me in
}

void manip8(Pointers* p){
// TODO: Fill me in
}

void manip9(Pointers* p, int* other){
// TODO: Fill me in
}

void manip10(Pointers* p){
// TODO: Fill me in
}

#endif

Sandbox file used to test the manip code

#include "Pointers.h"
#include "manip.h"
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
// maninp1
Pointers a;
manip1(&a);
cout << "maninp1:" << endl;
cout << (*(a.getA())) << " == 10 " << endl;
// maninp1 2
int number = 56;
Pointers b(number, &number);
manip2(&b);
cout << "maninp2:" << endl;
cout << (*(b.getB())) << " == 45" << endl;
// maninp1 3
number = rand() % 1000;
int number2 = 4;

Pointers c(number, &number2);
manip3(&c);
cout << "maninp3:" << endl;
cout << (*(c.getB())) << " == " << number << endl;

number2 = 4;
Pointers d;
manip4(&d, &number2);
cout << "maninp4:" << endl;
cout << d.getB() << " == " << &number2 << endl;
cout << (d.getB() == &number2) << " == 1" << endl;

number2 = 4;
Pointers e(0, &number2);
manip5(&e, &number2);
cout << "maninp5:" << endl;
cout << e.getC() << " == " << 45 << endl;
  
int numbers[] = {5,6,7,8};
Pointers f(0, numbers);
manip6(&f);
cout << "maninp6:" << endl;
cout << numbers[2] << " == " << 10 << endl;

int* number3 = new int;
Pointers g(0, number3);
manip7(&g);
cout << "maninp7:" << endl;
cout << *number3 << " == " << 15 << endl;
delete number3;
  
Pointers h;
manip8(&h);
cout << "maninp8:" << endl;
cout << h.getC() << " == " << 199 << endl;

int* number4 = new int;
Pointers* i = new Pointers();
manip9(i, number4);
cout << "maninp9:" << endl;
cout << i->getB() << " == " << number4 << endl;
delete number4;
delete i;

Pointers* j = new Pointers[10];
manip10(j);
cout << "maninp10:" << endl;
cout << j[5].getC() << " == " << 199 << endl;
delete[] j;

return 0;
}

Fill in the manip functions in the manip file to match this output:

maninp1:
10       = 10
maninp2:
45       = 45
maninp3:
383      = 383
maninp4:
0x7fff8072b6fc   = 0x7fff8072b6fc
1       == 1
maninp5:
45       = 45
maninp6:
10       = 10
maninp7:
15       = 15
maninp8:
199      = 199
maninp9:
0xbe9c20         = 0xbe9c20
maninp10:
199      = 199

Explanation / Answer

====================================================================

Edits: Added Last function manip10()

====================================================================

#ifndef MANIP_H

#define MANIP_H

// Author: Fill in your name

// Source File: manip.h

// Description: A set of functions where you should manipulate

// the passed parameters to change the object in a specific way,

// so that Pointers_test.h passes all tests.

#include "Pointers.h"

// A little something to get you going

void manip1(Pointers* p) {

*(p->getA()) = 10; //value of a = 10

}

void manip2(Pointers* p) {

// TODO: Fill me in

// *(p->getA()) = 45; //commented

// location pointed by b should be updated to 45

*(p->getB()) = 45; // Value pointed by B

}

void manip3(Pointers* p) {

// TODO: Fill me in

// Need to copy value of 'a' (number) to location pointed by 'b'

*(p->getB()) = *(p->getA());

}

void manip4(Pointers* p, int* other) {

// TODO: Fill me in

// Should assign value of pointer other(number2) to pointer 'b'

p->setB(other);

}

void manip5(Pointers* p, int* other) {

// TODO: Fill me in

// We should assign 45 to c

*other = 45; // assigning value 45

p->setB(other); // Now b points to value 45

p->setC(); //value of b is assigned here

}

void manip6(Pointers* p) {

// TODO: Fill me in

// We need to assign value 10 to b[2] location

int * ptr = p->getB(); // Getting the pointer b, ptr points to same as 'b'

//ptr++; ptr++;

*(ptr+2) = 10;

}

void manip7(Pointers* p) {

// TODO: Fill me in

// Assign 15 to value pointed by pointer b

*(p->getB()) = 15;

}

void manip8(Pointers* p) {

// TODO: Fill me in

// Assign 199 to c

int * ptr = new int(199); // create int pointer which points to 199

p->setB(ptr); // Now b points to value 199

p->setC(); //value of b is assigned to c

}

void manip9(Pointers* p, int* other) {

// TODO: Fill me in

// Should assign value of pointer other(number4) to pointer 'b'

p->setB(other);

}

void manip10(Pointers* p) {

// TODO: Fill me in

// Assign 199 to c of j[5]

int *ptr = new int(199);

(p+5)->setB(ptr);

(p+5)->setC();

}

#endif