answersLogoWhite

0

📱

C++ Programming

Questions related to the C++ Computer Programming Language. This ranges all the way from K&R C to the most recent ANSI incarnations of C++, including advanced topics such as Object Oriented Design and Programming, Standard Template Library, and Exceptions. C++ has become one of the most popular languages today, and has been used to write all sort of things for nearly all of the modern operating systems and applications." It it a good compromise between speed, advanced power, and complexity.

2,546 Questions

What are the advantages and disadvantages of the linked implementation of a queue relative to the contiguous implementation?

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 structure?

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.

What is class in DBMS?

class in dbms is nothing but a collection of attributes....

class in java is defined as collection of objects....:D

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.

Write a c plus plus program to use plus operator to display the sum of 2 numbers and concatenation of two strings by using operator overloading?

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);

}

What is profiling in c?

It is the process of analyzing the run-timebehaviorof the program. It includes analyzing which function takes the longest time, how many times a particular function is called, etc. Some of the profilers are valgrind, etc.

What is the difference between structure and structure using typedef with example?

Consider the following structure:

  • struct data_t { /* ... */ };

The name of this type is struct data_t. This means that we must include the struct keyword whenever we declare any instances of the type or declare a pointer to the type:

  • struct data_t object;
  • struct data_t* ptr;

To remove the verbosity of the otherwise redundant structkeyword, we can use an alias:

  • typedef struct data_t data;
  • data object;
  • data* ptr;

To simplify things further, we can combine the alias with the structure's definition:

  • typedef struct data_t { /* ... */ } data;

Note that the _t suffix is conventionally used to denote a user-defined type as opposed to an alias. However, its usage is not consistent. For instance, the wchar_t type is not a type, it is implemented as an alias in the C standard library header:

  • typedef unsigned short wchar_t;

In C++, the typedef keyword is not required; it is retained purely for backward compatibility with C. Aliases are introduced with the using keyword, never with typedef. We only use typedef when we are explicitly writing code intended for compilation under both C and C++.

Note also that wchar_t (amongst others) is a built-in type in C++, so we don't need to include the C standard library to make use of it, unless we are writing code for C.

How do you reverse the order of element on stack by using one additional stack and some additional non array variables?

You pop elements off of one stack and push them onto the other. This reverses the order of the elements.

while ((element = pop(stack1)) != NULL) push(stack2, element);

How do you remove recursion using stacks?

Using stacks won't remove recursion, they can only re-implement those recursions. In some cases we don't actually need a stack to implement a recursive algorithm, in which case an iterative implementation will typically perform better with little to no cost in additional memory. But if we require a stack in order to implement recursions iteratively, then we pay the cost in terms of additional memory consumption (the "built-in" call stack is fixed-size and exists whether we use it or not). In addition, there may be a performance cost if we cannot determine how much additional memory we need.

As an example, consider the recursive quicksort algorithm:

template<typename T>using iter = std::vector<T>::iterator; template<typename T>void quicksort (iter begin, iter end) {

if (begin<end) {

size_t pivot = partition (begin, end);

quicksort (begin, pivot - 1);

quicksort (pivot + 1, end);

} // end if

}

Note that the partition algorithm is not shown for the sake of brevity. However, it is best implemented as a separate function as its local variables play no part in the recursion.

Being a divide-and-conquer algorithm, this algorithm requires a stack for back-tracking. Here is the iterative equivalent using a stack:

template<typename T>using iter = std::vector<T>::iterator;

template<typename T>void quicksort (iter begin, iter end) {

if (begin<end) {

std::stack<std::pair<iter, iter>> s {};

s.push ({begin, end});

while (s.empty() == false) {

begin = s.top().first();

end = s.top().second();

s.pop();

size_t pivot = partition (begin, end);

if (pivot + 1<end) s.push ({pivot + 1, end});

if (begin<pivot - 1) s.push ({begin, pivot - 1});

} // end while

} // end if

}

Note that the order we push the pairs on at the end of the while loop is the reverse order we wish them to be processed. The order doesn't actually matter, but it ensures both algorithms operate in a consistent manner, with depth-first traversal from left to right.

This implementation is naive because each push allocates new memory for each pair object we push onto the stack, releasing the same memory with each pop. Allocating and releasing system memory on a per-element basis like this is highly inefficient, so it's highly unlikely that this version will perform any better than the recursive algorithm.

However, the quicksort algorithm guarantees that there can never be more elements on the stack than there are elements in the initial range, so we can improve performance significantly by reserving sufficient memory in advance:

template<typename T>using iter = std::vector<T>::iterator;

template<typename T>void quicksort (iter begin, iter end) {

if (begin<end) {

std::vector<std::pair<iter, iter>> v {};

v.reserve (end - begin);

v.emplace_back (begin, end);

while (v.empty() == false) {

begin = v.back().first();

end = v.back().second();

v.pop_back();

size_t pivot = partition (begin, end);

if (begin < pivot - 1) v.emplace_back (begin, pivot - 1);

if (pivot + 1 < end) v.emplace_back (pivot + 1, end);

} // end while

} // end if

}

Note that in this implementation we use a vector rather than a stack, however all pops and pushes (implemented as emplace_back operations) occur at the back of the vector where the unused elements are, and that's precisely how an efficient stack should be implemented. As a result, this version will perform significantly better than the previous version and should perform at least as well as the recursive implementation if not better. The only significant cost is the cost of reserving memory in the vector.

What is the inverse of a conditional statement?

The statement formed when you negate the hypothesis and conclusion of a conditional statement.

For Example: If you had enough sleep, then you did well on the test.

The inverse will be: If you didn't have enough sleep, then you didn't do well on the test.

Where is STL?

STL is an abbreviation for many different things. Most commonly, when one uses STL, they are referring to St. Louis, Missouri.

What is the Main purpose of complex.h header file in c plus plus?

The complex header implements complex numbers. Complex numbers are represented by the expression a + bi where a and b are real numbers and i is an imaginary unit that satisfies the constant expression i squared = -1.

Complex numbers allow imaginary solutions to expressions that have no actual solution. For instance, (x + 1) squared = -9 has no real solution, but does have two imaginary solutions when x = (a + bi), where a is -1 and b can be 3 or -3.

Note that library headers with an h extension are pre-standard headers. After standardisation, all library headers dropped the h extension including the complex header. Older compilers may still provide both versions of a library header, but in this day and age you should always use the standardised headers (no extension).

#include<complex.h> // non-standard

#include<complex> // standardised

What is a user defined manipulator in c plus plus?

A user-defined manipulator is a function which can be passed as an argument to the stream insertion or extraction operator overloads. For example, the output stream insertion operator has the following overload:

std::ostream& operator<< (std::ostream& st, std::ostream& (*func) (std::ostream&));

The second argument is the function pointer, with the following signature:

std::ostream& (*func) (std::ostream&)

Any function that matches this signature can be used as a manipulator. For instance, the following user-defined manipulator does exactly the same job as the std::endl manipulator:

std::ostream& my_manipulator (std::ostream& os)

{

return os << '\n' << std::flush;

}

Example usage:

std::cout << "Hello world!" << my_manipulator;

You can, of course, provide your own implementations to perform any type of manipulation. For example, suppose you want a manipulator that inserts an elipses into an output stream:

std::ostream& elipses (std::ostream& os)

{

return os << "...";

}

Example usage:

std::cout << "Hello" << elipses << "world!" << my_manipulator;

Output:

Hello...world!

How do you make a UIPickerView that plays sounds?

The UIPickerView doesn't actually play the sounds; you do that with a hidden media control. When you click the control button, you examine the selected element in the list and load the associated sound file into the media control, which then plays the file. There are various ways of doing it, but one of the simplest methods is to use two parallel arrays, one containing the titles (which you load into the list), the other containing the paths to the sound files. Both arrays being of type std::string, of course. Thus element 9 in the titles array maps to element 9 in the sound files array. Alternatively, use std::pair objects (where each element in the pair is a std::string) to associate each title with its sound file.

What is callback function in c?

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a function defined in a higher-level layer. Usually, the higher-level code starts by calling a function within the lower-level code passing to it a pointer or handle to another function. While the lower-level function executes, it may call the passed-in function any number of times to perform some subtask. In another scenario, the lower-level function registers the passed-in function as a handler that is to be called asynchronously by the lower-level at a later time in reaction to something. A callback can be used as a simpler alternative to polymorphism and generic programming, in that the exact behavior of a function can be dynamically determined by passing different (yet compatible) function pointers or handles to the lower-level function. This can be a very powerful technique for code reuse. Callback functions separate the caller from the callee, the caller doesn't care who the callee is For complete understanding we need to know about Function pointers in c. check the link below

C plus plus program to insert an element using binary tree?

The algorithm for inserting into a binary tree is fairly straightforward. Start by passing the element to the root node of the tree. If there is no root, the new element becomes the root and you're done. Whenever a node receives a new element, it compares the element's value with its own value. If the new element is lower in value, pass the element to the left node otherwise pass it to the right node. If there is no node in that position, the new element becomes that node. In this way, the node's themselves decide where new elements are placed.

In order to achieve this you need a node that encapsulates some data. Typically the data is a template parameter but for the sake of simplicity let us assume the data is an unsigned integer. We'll also need pointers to the left and right nodes. We'll use the default constructor to initialise the data and pointers. we'll also need an insertion method. Thus our node class looks like this:

class node

{

private:

node* left;

node* right;

unsigned data;

public:

node(unsigned value): left(NULL), right(NULL), data(value) {}

void insert(unsigned value);

};

The insert method will be implemented as follows:

void node::insert(unsigned value)

{

if (value<data)

{

if (left) left->insert(value); else left = new node(value);

}

else

{

if (right) right->insert(value); else right = new node(value);

}

}

That's all there is to it. All you need now is a pointer to the root which will initially be NULL. When you come to insert a value and the root is NULL, create a new node and assign it to the root pointer, otherwise call root->insert(value); and let the tree sort itself out.

What is TCDEFexe?

TCDEF.exe probably relates to Borland Turbo C (TC). I can't find any info on the file itself, other than that some people using TC seem to have problems locating the file during compilation. That's usually a sign of a bad install or (more likely) the presence of malware. Given the lack of information I suspect TCDEV.exe is not a legitimate part of TC.