#include <iostream>
int main()
{
double num1 = 0.0;
std::cout << "Enter first number: ";
std::cin >> num1;
double num2 = 0.0;
std::cout << "Enter second number: ";
std::cin >> num2;
std::cout << num1 << " + " << num2 << " = " << (num1 + num2);
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
int main()
{
using namespace std;
vector<int> v { 9, 1, 4, 3, 7, 6, 5, 2, 8 };
cout << "Before sorting:\t";
for (unsigned i=0; i<v.size(); ++i) std::cout << v[i] << '\t';
cout << "\nAfter sorting:\t";
sort(v.begin(), v.end());
for (unsigned i=0; i<v.size(); ++i) std::cout << v[i] << '\t';
cout << endl;
}
Constructors cannot return values so the only paractical way of achieving this would be to supply an output parameter to the constructor. But what exectly are you constructing and for what reason? You can find the sum of the digits for any value with a simple function.
unsigned sum_of_digits(unsigned num)
{
unsigned sum = 0;
do
{
sum += num % 10;
} while (num/=10);
return sum;
}
To achieve the same thing via a constructor you'd need the following:
class sum_of_digits
{
public:
sum_of_digits(unsigned num, unsigned& sum)
{
sum = 0;
do
{
sum += num % 10;
} while (num/=10);
return sum;
}
};
This class does absolutely nothing useful. Even though the class is empty (no member data), you must still instantiate an object of the class in order to find the sum. And an empty object consumes memory like any other object.
int main()
{
unsigned sum;
sum_of_digits f (42, sum);
// Now you've got two objects occupying memory (sum and sum_of_digits).
sum = sum_of_digits (42);
// With the function call you'd have just one object: the sum itself.
}
Use the following two template functions to find the sum and average of any array. The elements in the array must support the compound plus/assignment operator (+=) for both functions, while the average function requires the elements also support the division operator (/). All integral types support these operators by default, thus to sum or average twenty numbers, place all the numbers in an array of the appropriate type and call the appropriate template function, passing the array (by reference) and its size. Note that the average function makes use of the sum function.
template<typename T>
T sum(T A[], const size_t size)
{
T total=0;
for(size_t index=0; index<size; ++index)
total+=A[index];
return(total);
}
template<typename T>
T average(T A[], const size_t size)
{
return(sum(A,size)/size);
}
#include<iostream>
int main()
{
int x=0, y=1;
std::cout<<x<<" ";
std::cout<<y<<" ";
while( y<1000000 )
{
std::cout<<(y+=x)<<" ";
x=y-x;
}
std::cout<<std::endl;
return(0);
}
#include
#include
how to write a program that counts automorphic number from 1 to 999
#include<iostream> class foo{ int m_data; }; int main() { foo* p=new foo; delete( foo), foo=NULL; return(0); }
#includeint main(){int i;for(i=2;i
Sure.
C++ already provides a string class in the C++ standard template library. #include<iostream> #include<string> int main() { using namespace std; string s {"Hello world!"}; cout << s << endl; }
i dn't know. haha
how to write a program that counts automorphic number from 1 to 999
By learning how to program on C+.
#include<iostream> class foo{ int m_data; }; int main() { foo* p=new foo; delete( foo), foo=NULL; return(0); }
#includeint main(){int i;for(i=2;i
Yes
R = (A > B && A > C) ? A : (B > C) ? B : C; // parentheses not necessary - for clarity only
#include using std::cout;using std::endl;int main(viod){cout
Sure.
Instantiation of a class literally means creating an instance of a class. This is the process of allocating memory for an object that you can use in your program.
time in hours second minute
Input a variable.