How do you make a program like 5142332415 using looping?
There is insufficient information in the question to answer it. Please rephrase the question.
Write a function that creates a vector of user-given size M using new operator?
template<typename T> std::vector<T>* create_new_vector (const size_t M)
{
std::vector<T>* p = nullptr;
try
{
if (p = new std::vector<T>)
{
p->resize (M);
p->shrink_to_fit ();
}
}
catch (std::exception& e)
{
throw e;
}
return p;
}
What is the relationship between an array and a pointer?
All variable names are an alias for the value stored at the memory address allocated to them. To get the memory address itself, you must use the address of operator (&). The value returned from this can then be stored in a pointer variable.
Arrays are different. The array name is an alias for the start address of the array, thus you do not need the address ofoperator to obtain the memory address (although you can if you want to). This means that when you pass an array name to a function, you pass the memory address of the array rather than passing the array itself (which would require the entire array to be copied, which is a highly inefficient way to pass an array). In essence, the array is passed by reference rather than by value.
Consider the following code. This shows how a primitive variable name differs from the name of an array of primitive variables. The final portion shows how a pointer can be used to achieve the same results you got by accessing the array elements directly from the array name itself. This is in fact how the compiler implements arrays, using pointers, but there's no need to do this in your code. Accessing array elements directly by their index is a programming convenience.
#include
using namespace std;
int main()
{
int i = 10;
cout << "&i = address of i:\t0x" << &i << endl;
cout << "i = value of i:\t" << i << endl;
cout << endl;
int Array[10] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
cout << "&Array = address of Array:\t0x" << &Array << endl; // same as next line.
cout << "Array = address of Array:\t0x" << Array << endl; // same as previous line.
cout << "*Array = value of Array[0]:\t" << *Array << endl; // returns value of first element.
cout << endl;
int offset=0;
for( offset=0; offset<10; ++offset )
{
cout << "&Array[" << offset << "] = address of element " << offset << ":\t0x" << &Array[offset] << endl;
cout << "Array[" << offset << "] = value of element " << offset << ":\t" << Array[offset] << endl;
}
cout << endl;
int * pointer = Array; // point to the array address.
cout << "&pointer = address of pointer:\t\t0x" << &pointer << "\t(never changes)" << endl;
cout << endl;
for( offset=0; offset<10; ++offset )
{
cout << "pointer = address stored in 0x" << &pointer << ":\t0x" << pointer << "\t(same as &Array[" << offset << "]" << endl;
cout << "*pointer = value stored in 0x" << pointer << ":\t" << *pointer << "\t\t(same as Array[" << offset << "]" << endl;
++pointer;
}
cout << endl;
return( 0 );
}
What is the difference between the direct and indirect base class?
Direct base classes are those that are specified in a derived class' inheritance declaration. Indirect base classes are those that are inherited via the direct base classes.
class A{};
class B : public A{};
class C : public B{};
With respect to C, B is direct while A is indirect. With respect to B, A is direct.
What are the different types of overloading allowed in c plus plus?
There are only two types: user-defined and compiler-generated. User-defined overloads are the ones you specifically write. Compiler-generated overloads are created by the compiler based upon a template function that you define. The only real difference is that with a template function you only need to write one version of the function, and the compiler will generate the actual overloads on an as-required basis. With user-defined overloads you must write each overload in full.
Template functions save a lot of time and maintenance, however they are only practical when the only difference between the overloads is the type of argument. That is, if the implementation is exactly the same regardless of type, then template functions are clearly a better option than writing out each overload by hand. Moreover, if you ever need to alter the implementation, there's only one function to modify.
If you know what BNF is:
Identifier -> Start Cont
Start -> letter | underline
Cont -> empty | (letter | underline | digit) Cont
#include <iostream>
using namespace std;
int main()
{
int i1, i2, i3;
char o;
cout << "Enter two integers sparated by an operator: ";
cin >> i1 >> o >> i2;
if ( o '/' && i2 != 0 )
{
i3 = i1 / i2;
cout << i1 << " / " << i2 << " = " << i3 << endl;
}
else
cout << "Input Error!" << endl;
return 0;
}
Can you write a program in c plus plus for finding practical numbers?
#include<iostream>
#include<vector>
#include<sstream>
#include<cassert>
using set_type = std::vector<size_t>;
using set_const_iterator = set_type::const_iterator;
using subset_type = std::vector<set_type::const_iterator>;
using subset_const_iterator = subset_type::const_iterator;
using subset_reverse_iterator = subset_type::reverse_iterator;
// Returns the first subset of the given set with the given count.
subset_type first_subset (const set_type& set, size_t count)
{
assert (0U < count);
assert (count <= set.size());
subset_type subset;
for (set_const_iterator it=set.begin(); count--; ++it)
subset.push_back (it);
return subset;
}
// Returns the next subset after the given subset of the given set with the given count.
subset_type next_subset (set_type& set, size_t count, subset_type subset)
{
assert (subset.size() == count);
if (count)
{
subset_reverse_iterator sub_it = subset.rbegin();
set_const_iterator set_it = *sub_it;
subset.pop_back();
if (++set_it != set.end())
subset.push_back (set_it);
else if (1U != count)
{
const size_t value = set.back();
set.pop_back();
subset = next_subset(set, --count, subset);
set.push_back (value);
if (subset.size() == count++)
{
sub_it = subset.rbegin();
set_it = *sub_it;
if (++set_it != set.end())
subset.push_back (set_it);
}
}
}
return subset;
}
// Returns all the divisors of the given number.
set_type get_divisors (const size_t num)
{
set_type divs;
size_t factor = 0;
while (factor++<=num/2)
{
if (num%factor==0)
divs.push_back (factor);
}
return divs;
}
// Returns the sum of the given subset.
size_t sum (const subset_type& subset)
{
size_t sum = 0;
for (subset_const_iterator it=subset.begin(); it!=subset.end(); sum += **it, ++it);
return sum;
}
// Returns true if the sum of any combination of distinct divisors totals the given number
bool has_compound (const size_t num, set_type divisors)
{
bool found = false;
for (size_t count=1; !found && count<=divisors.size(); ++count)
for (subset_type subset = first_subset (divisors, count); !found && subset.size(); subset = next_subset (divisors, count, subset))
found = sum (subset) == num;
return found;
}
// Returns true if the given number is a practical number.
bool is_practical (const size_t num)
{
if (!num) return false;
if (num<3) return true;
if (num%2) return false;
set_type divs = get_divisors (num);
for (size_t val=1; val<num; ++val)
if (!has_compound (val, divs))
return false;
return true;
}
int main()
{
std::cout << "Practical numbers in the range 1 to 240\n\n";
std::stringstream ss;
for (size_t num=1; num<=240; ++num)
{
if (is_practical(num))
{
if (ss.str().size())
ss << ", ";
ss << num;
std::cout << num << std::endl;
}
}
// Assert that the numbers match those in sequence A005153
// of the Online Encyclopedia of Integer Sequences (OEIS).
std::string verify = "1, 2, 4, 6, 8, 12, 16, 18, 20, 24, "
"28, 30, 32, 36, 40, 42, 48, 54, 56, 60, 64, 66, 72, 78, "
"80, 84, 88, 90, 96, 100, 104, 108, 112, 120, 126, 128, "
"132, 140, 144, 150, 156, 160, 162, 168, 176, 180, 192, "
"196, 198, 200, 204, 208, 210, 216, 220, 224, 228, 234, 240";
std::cout << verify << std::endl;
std::cout << ss.str() << std::endl;
assert (ss.str() == verify);
}
CAN Overloaded functions can return default values?
I'm not sure I understand you as it wouldn't make sense for a function to return a default value.
Do you actually mean can a function return an argument that has a default value? If so, then yes. Any argument passed to a function, whether defaulted or not, can be returned by the same function. If the argument is passed by value then you must return it by value. If passed by reference (which cannot be defaulted) then you can either return by reference or by value. However, if you pass by non-constant reference then you can just use the reference as an output argument, and use the actual return value for some other purpose, such as reporting any error condition(s) created by the function.
Overloaded functions are no different to ordinary functions, the only criteria is that each overload has an unique signature. The return value does not form any part of the signature, thus signatures cannot differ by return type alone.
How do you print hello world 100 times in c plus plus using while statment?
In C plus plus all false relational expressions have a mathematical value of?
In C++ all false relational expressions have a mathematical value of 0.
In C++ you can use the standard library sort algorithm to sort any data sequence provided the elements support the appropriate comparison operator (std::less<T> for any given type T is used by default).
For example:
std::vector<int> v {5, 9, 7, 1, 3};
std::sort (v.begin(), v.end()); // v = {1, 3, 5, 7, 9}
std::sort (v.begin(), v.end(), std::greater<int>); // v = {9, 7, 5, 3, 1}
What do you mean by public private protected and friendly?
An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:
1. Public
2. Protected
3. Default and
4. Private
Java does not have a friendly access modifier.
Public Members:
If a variable or method is declared public, it means that it can be accessed by anyone or any other class (irrespective of which package they are in).
Private Members:
Members marked private can't be accessed by code in any class other than the class in which the private member was declared
Protected and Default Members:
The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package
Find vowel from given string in C plus plus language?
std::string test ("The cat sat on the mat");
std::string vowels ("aeiouAEIOU");
size_t pos = test.find_first_of (vowels);
if (pos != test.npos)
std::cout << "Vowel found at position " << pos << std::endl;
Contiguous implementation (e.g., using an array), has a major disadvantage in that you have to allocate enough space in the array to hold all the elements in the queue. When there is insufficient space, you have to re-allocate, which may occasionally require the entire array be copied to new memory. In addition, with each extraction, you are left with unused memory at the start of the array. Thus before any re-allocation, you must first check if there are unused elements and then shunt all elements forward, thus creating space at the end of the array.
If we ignore the shunting and re-allocations necessary with an array, insertions and extractions will occur in constant time for both contiguous and linked implementations. However, once you take shunting and re-allocation into account, we find that contiguous implementations occasionally falter during an insertion.
Contiguous implementations also require more memory than linked implementations. Although a linked node is larger than an array element by one pointer (32 bits on a 32-bit system), contiguous implementations must allocate far more memory than is actually required in order to minimise the number of re-allocations. Thus an array must always have unused elements. Moreover, when all elements are extracted from the queue, the current allocation remains in memory. With linked implementations, the only memory allocated is the memory actually in use at any given moment, with the minimum memory requirement being just one pointer, to the tail node.
An optimal queue implementation will use a circular linked list, where only the tail node need be maintained by the list object. The tail's next node always points to the head node, so there is no need to maintain this separately.
The only real disadvantage of a linked implementation is the need to allocate and deallocate memory for each insertion and extraction respectively. However, this is a constant time operation. With contiguous implementation, only extraction is guaranteed to be a constant time operation. Most of the time, insertions will be constant time, but will occasionally take variable time due to the need to shunt or re-allocate.
How can i find last node of a cicular list whose size i don't know and the last node points to any other node except first node of the link list through c plus plus language of data stucture?
Is there a way how to generate a random number in between -1000 and 1000 in c?
#include <stdlib.h>
int myrand() {
double d = rand() / RAND_MAX; /* (0,1] */
return d * 2001 - 1000 + 0.5; /* [-1000,+1000] */
}
If you write an app using KDE4 with kdelibs will it be cross-platform?
Yes. KDE4 applications will run on Unix and Unix-like platforms (including Mac OS X) and Windows platforms.
class in dbms is nothing but a collection of attributes....
class in java is defined as collection of objects....:D
Depends on your code. Post it, along with your variable declarations.
What is the structure of 3-methyl-3-butanol?
There is no such thing. It would be named 2-methyl-2-butanol (see link below).
"Butan" is a continuous chain of 4 carbons. Starting from one side, the 3rd carbon will have both a methyl group (-CH3) as well as a hydroxyl group (-OH), hence "butanOL". Since the rules of naming require that the lowest numbers be used, and the methyl and hydroxyl groups are on the second carbon, the correct name is in fact: 2-methyl-2-butanol.
There is no need to overload the plus operator to achieve this. It is already overloaded with this functionality.
#include<iostream>
#include<string>
int main()
{
int x=40;
int y=2;
int z=x+y;
std::cout<<x<<" + "<<y<<" = "<<z<<std::endl;
std::string s1="Hello ";
std::string s2="world!";
std::string s3=s1+s2;
std::cout<<"""<<s1.c_str()<<"" + ""<<s2.c_str()<<"" = ""<<s3.c_str()<<"""<<std::endl;
return(0);
}