Exercise 2: decalre a pointer variable structPointer and initalize it to point t
ID: 674878 • Letter: E
Question
Exercise 2: decalre a pointer variable structPointer and initalize it to point to a struct variable called newPhone, of typePhone, that has three int fields called country, area, and number. Also write the declaration of the Phone type. Then write assignment statements to indirectly store the values 1, 888, and 5551212, respectively, into these fields.
Exercise 3: Write a Boolean value-returning function called ShallowCompare that takes two variables of type structPointer, as defined in Excerise 2, and returns true if the structs that they point to the same struct and false otherwise.
Exercise 4: Write a Boolean value-returning function called DeepCompare that takes two variables of type structPointer, as defined in Exercise 2, and returns true if the structs that they point to have identical values in their corresponding fields.
Explanation / Answer
Exercise 2:
struct Phone
{
int country;
int area;
int number;
};
int main()
{
Phone newPhone;
Phone *structPointer = &newPhone;
structPointer->country = 1;
structPointer->area = 888;
structPointer->number = 5551212;
//...
return 0;
}
Exercise 3 & 4:
#include "Phone.h"
using namespace std;
bool ShallowCompare (Phone*, Phone*);
bool DeepCompare (Phone*, Phone*);
int Greatest (int[], int);
int GetGreatestInput();
int main()
{
int static max;
int someInt;
int* intPointer;
intPointer = &someInt;
someInt = 451;
if(someInt == 451)
cout << "Passed PWU Exercise 1." << endl;
else
cout << "Failed PWU Exercise 1." << endl;
someInt = 0;
*intPointer = 451;
if(someInt == 451)
cout << "Passed PWU Exercise 1.n" << endl;
else
cout << "Failed PWU Exercise 1.n" << endl;
char* charArrPointer;
char initials[4];
charArrPointer = initials;
*charArrPointer = 'A';
*(charArrPointer + 1) = 'E';
*(charArrPointer + 2) = 'W';
if (*charArrPointer == 'A')
cout << "Passed PWU Exercise 2.n" << endl;
else
cout << "Failed PWU Exercise 2.n" << endl;
Phone newPhone;
Phone* structPointer = &newPhone;
structPointer->country = 1;
structPointer->area = 888;
structPointer->number = 5551212;
if ((structPointer->country == 1) && (structPointer->area == 888) && (structPointer-> number == 5551212))
{
cout << "Passed PWU Exercise 3.n" << endl;
}
else
{
cout << "Failed PWU Exercise 3.n" << endl;
}
Phone& structReference = newPhone;
structReference.country = 1;
structReference.area = 888;
structReference.number = 5551212;
if ((structReference.country == 1) && (structReference.area == 888) && (structReference.number == 5551212))
{
cout << "Passed PWU Exercise 4.n" << endl;
}
else
{
cout << "Failed PWU Exercise 4.n" << endl;
}
Phone* structPointer1 = &newPhone;
Phone* structPointer2 = &newPhone;
if (ShallowCompare(structPointer1,structPointer2))
{
cout << "Passed PWU Exercise 5.n" << endl;
}
else
{
cout << "Failed PWU Exercise 5.n" << endl;
}
Phone* structPointer3 = &newPhone;
Phone* structPointer4 = &newPhone;
if (DeepCompare(structPointer3, structPointer4))
{
cout << "Passed PWU Exercise 6.n" << endl;
}
else
{
cout << "Failed PWU Exercise 6.n" << endl;
}
int exercise7Array[100];
int* dataPtr = exercise7Array;
for (int count = 0; count < 100; count++)
{
*(dataPtr + count) = count;
}
for (int count = 0; count < 100; count++)
{
int temporary;
temporary = *(dataPtr + count);
if (count == 0)
{
temporary = max;
}
else
{
if (max < temporary)
{
max = temporary;
}
}
}
if (max == 99)
cout << "Passed PWU Exercise 7.n" << endl;
else
cout << "Failed PWU Exercise 7.n" << endl;
delete dataPtr;
int exercise8Array[100];
int* dataPtr2 = exercise8Array;
for (int count = 0; count < 100; count++)
{
*(dataPtr2 + count) = count;
}
max = Greatest(exercise8Array, 100);
if (max == 99)
cout << "Passed PWU Exercise 8.n" << endl;
else
cout << "Failed PWU Exercise 8.n" << endl;
int largestValue9 = GetGreatestInput();
if (largestValue9 == 99)
cout << "Passed PWU Exercise 9.n" << endl;
else
cout << "Failed PWU Exercise 9.n" << endl;
int* oldValue;
int* newValue;
int exercise10Int1 = 40;
int exercise10Int2 = 50;
oldValue = &exercise10Int1;
if (*oldValue != NULL)
{
newValue = oldValue;
}
else
{
newValue = &exercise10Int2;
}
if (*newValue == 50)
{
cout << "Passed PWU Exercise 11.n" << endl;
}
else
{
cout << "Failed PWU Exercise 11.n" << endl;
}
if (*oldValue == *newValue)
{
delete oldValue;
}
if (*oldValue != NULL)
{
cout << "Passed PWU Exercise 11.n" << endl;
}
else
{
cout << "Failed PWU Exercise 11.n" << endl;
}
system("Pause");
return 0;
}
bool ShallowCompare (Phone* ptr1, Phone* ptr2)
{
if(ptr1 == ptr2)
return true;
else
return false;
}
bool DeepCompare (Phone* ptr1, Phone* ptr2)
{
if ((ptr1->country == ptr2->country) && (ptr1->area == ptr2->area) && (ptr1->number == ptr2->number))
return true;
else
return false;
}
int Greatest (int array1[], int size)
{
int maximum;
int* temporaryPtr = array1;
for (int count = 0; count < size; count++)
{
int temporary;
temporary = *(temporaryPtr + count);
if (count == 0)
{
maximum = temporary;
}
else
{
if (maximum < temporary)
{
maximum = temporary;
}
}
}
delete temporaryPtr;
return maximum;
}
int GetGreatestInput()
{
int size;
cout << "Input the number of values to be input: " << endl;
cin >> size;
cin.ignore(100, 'n');
int temporaryArray[1000];
for (int count = 0; count < size; count++)
{
int temporaryValue;
cout << "Input an integer value to be put into the array: " << endl;
cin >> temporaryValue;
cin.ignore(100, 'n');
temporaryArray[count] = temporaryValue;
}
int largestValue = Greatest(temporaryArray, size);
return largestValue;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.