In: Computer Science
Fill in the code only using pointer variables
#include
using namespace
std; int main() {
int longside; // holds longside (length)
int wideside; // holds wideside(width)
int total; // holds total (area)
int *longsidePointer = nullpointer; // int pointer which will be set to point to length
int *widthPointer = nullpointer; // int pointer which will be set to point to width
cout << "Please input the longside of the rectangle" << endl;
cin >> longside;
cout << "Please input the wideside of the rectangle" << endl;
cin >> wideside;
// Fill in code to make longsidePointer point to longside(length) (hold its address)
// Fill in code to make widesidePointer point to wideside(width) (hold its address)
total = // Fill in code to find the total(area) by using only the pointer variables
cout << "The total is " << total << endl;
if (// Fill in the condition longside(length) > wideside(width) by using only the pointer variables)
cout << "The longside is greater than the wideside" << endl;
else if (// Fill in the condition of wideside(width) > longside(length) by using only the pointer variables)
cout << "The wideside is greater than the longside" << endl;
else
cout << "The wideside and longside are the same" << endl;
return 0;
}
}
Solution
#include <iostream>
using namespace std;
int main()
{
int longside;
int wideside;
int total;
int *longsidePointer = NULL;
int *widthPointer = NULL;
cout << "Please input the longside of the rectangle" << endl;
cin >> longside;
cout << "Please input the wideside of the rectangle" << endl;
cin >> wideside;
// Fill in code to make longsidePointer point to longside(length) (hold its address)
longsidePointer=&longside;
widthPointer=&wideside;
total = *longsidePointer * *widthPointer ;
cout << "The total is " << total << endl;
if (longsidePointer > widthPointer)
cout << "The longside is greater than the wideside" << endl;
else if (longsidePointer < widthPointer)
cout << "The wideside is greater than the longside" << endl;
else
cout << "The wideside and longside are the same" << endl;
return 0;
}