This program illustrates dynamic variables
#include <iostream>
using namespace std;
int main ()
{
int *p1;
p1 = new int; // Variables created using the new operator are called dynamic variables
cout << "Enter an integer \n";
cin >> *p1;
*p1 = *p1 + 7;
cout << << "Your input + 7 = " <<
*p1 << endl;
delete p1; // Delete the dynamic variable p1 and return the memory occupied by p1 to the freestore to be reused.
return 0;
}
Problem is:
Define a class Area that has two private variable
members; units of type string and area_value of
type float. Modify the program to create a dynamic variable of type
Area.
1) Input from the keyboard the
area_value and its units. Compute one-half and one-quarter
of the area and display the results
2) Destroy the dynamic variable at the end
#include <iostream>
#include <cstring>
using namespace std;
class Area{
private:
string unit;
float area_value;
public:
Area(){
}
Area(string s, float f){
unit = s;
area_value = f;
}
string getUnit(){
return unit;
}
float getValue(){
return area_value;
}
};
int main() {
string s;
float f;
cout<<"Enter unit: ";
cin>>s;
cout<<"Enter area value: ";
cin>>f;
Area *a = new Area(s,f);
cout<<"One half of area value is: "<<0.5*a->getValue()<<" "<<a->getUnit()<<endl;
cout<<"One quater of area value is: "<<0.25*a->getValue()<<" "<<a->getUnit()<<endl;
delete a;
}
==========================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.
This program illustrates dynamic variables #include <iostream> using namespace std; int main () { int...
#include <iostream> using namespace std; int main(void) { int SIZE; cout<<"Enter the size of the array"<<endl; cin>>SIZE; int *numlist = new int[SIZE]; // Read SIZE integers from the keyboard for (int i = 0; i<SIZE; i++ ) { cout << "Enter value #" << i+1 << ": "; cin >> numlist[i]; } // Display the numbers in a reverse order for (int i = SIZE; i > 0; i--...
Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length, int width = 0); int main() { int length, width; // for rectangle use both arguments cout << "Enter length and width of a rectangle" << endl; cin >> length >> width; cout << "The area is " << area(length, width) << endl; // for square, only need first argument cout << "Enter side of a square" << endl; cin >> length; cout <<...
Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
#include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...
C++
#include <iostream>
using namespace std;
bool checkinventoryid(int id)
{
return id > 0;
}
bool checkinventoryprice(float price)
{
return price > 0;
}
void endProgram()
{
system("pause");
exit(0);
}
void errorID(string str)
{
cout << "Invalid Id!!! " << str
<< " should be greater than 0" << endl;
}
void errorPrice(string str)
{
cout << "Invalid Price!!!" << str
<< " should be greater than 0" << endl;
}
int inputId()
{...
Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int n){ int a = 0,b = 1,c; if(n == 0 || n == 1){ return n; } while(n>2){ c = b + a; a = b; b = c; n--; } return c; } int main(){ int n; cout<<"Enter n: "; cin>>n; int result = fib(n); cout<<"Fib("<<n<<") = "<<result<<endl; return 0; }
Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: "; cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2; return 0; }
#include <iostream> using namespace std; int main() { int sumOdd = 0; // For accumulating odd numbers, init to 0 int sumEven = 0; // For accumulating even numbers, init to 0 int upperbound; // Sum from 1 to this upperbound // Prompt user for an upperbound cout << "Enter the upperbound: "; cin >> upperbound; // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound int number =...
Watermelon Problem: The answer should not include any whitespace. #include<iostream> using namespace std; int main() { double distance, gravity =9.8, height; int time, t=0; cout<<"Please input the time of fall in seconds:"<<endl; [ A ] // Get the user input of the time of fall in seconds (Use cin>> method) cout<<endl; cout<<"Please input the height of the bridge in meters:"<<endl; [ B ] // Get the user input of the height of the bridge in meters (Use cin>>...