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

How do you write a program that asks a user to enter a list of integers placing positive and negative integers into separate arrays and output both arrays?

#include

#include

#include

#include

using namespace std;

string ask(string prompt)

{

string input;

cout<

getline(cin, input);

return(input);

}

int ask_for_int()

{

int input;

string response = ask("Enter a number: ");

stringstream s(response);

if(s>>input)

return(input);

return(0);

}

bool ask_yn(std::string prompt)

{

while(1)

{

string input = ask(prompt);

if( input.size() && input.size()<4 )

{

if((input[0]=='y' input[0]=='Y')

&& (input.size()==1

(input.size()==3

&& (input[1]=='e' input[1]=='E')

&& (input[2]=='s' input[2]=='S'))))

return(true);

if(( input[0]=='n' input[0]=='N')

&& (input.size()==1

(input.size()==2

&& (input[1]=='o' input[1]=='O'))))

return(false);

}

cout<<"Please answer YES or NO\n"<

}

}

void print_array(string title, vector arr)

{

cout<

if(!arr.size())

cout<<"None"<

else for(size_t i=0; i

cout<

cout<

}

int main()

{

vector positive;

vector negative;

while(1)

{

int i = ask_for_int();

if( !i && !ask_yn("Invalid number. Continue entering numbers? "))

break;

if( i>0 )

positive.push_back(i);

else

negative.push_back(i);

}

cout<

print_array("Positive integers",positive);

print_array("Negative integers",negative);

}

Example Output

Enter a number: 42

Enter a number: -12

Enter a number: -1

Enter a number: 2112

Enter a number: 6

Enter a number: -23

Enter a number: +1

Enter a number: -990

Enter a number: 112

Enter a number: 0

Invalid number. Continue entering numbers? no

Positive integers

42

2112

6

1

112

Negative integers

-12

-1

-23

-990

What are the advantages and disadvantages of using borland c plus plus version 5?

The only advantage is that it is free. But it is disadvantaged by the fact it only supports Windows 95, 98, NT and 2000. Thus it is not compliant with the current C++ standard, and is only useful for legacy development upon these Windows platforms.

C plus plus array-based lists?

If you mean an array where each element is a list, then the STL is your friend. To create an array of lists of any type T, use the following declaration:

std::vector<std::list<T>> my_array_of_lists;

How is the performance of Intel atom n450 processors and can it be used for c plus plus progamming?

All Intel processors can be used for C++ programming. All you need is a suitable compiler/linker.

With regards performance, the N450 does not perform well compared to modern netbook processors. Given its age this is hardly surprising.

How does having a widely adopted c plus plus standard help game programmers?

A standardised language means that code becomes completely portable between any compiler, so long as the compiler supports the standard and the code itself does not contain compiler-specifics. In terms of game development, this makes it much easier to port games between platforms.

Of course, C++ code is usually far from standard because the standard does not include platform-specifics (in terms of hardware and operating system). Thus each platform requires it own set of libraries. Generic libraries are available to make things easier (such as OpenGL and OpenAL for graphics and audio), but Windows API calls vary greatly from Linux API calls, and each needs its own set of libraries. However, with prudent use of preprocessor definitions (macros) and helper functions, it is possible to write generic cross-platform code.

What is the declaration of the function to overload output operator inside class in c plus plus?

You can't overload the insertion operator (<<) from inside a class because the l-value must be an ostream object, but operator overloads implemented within classes always implicate the class instance itself as being the l-value.

You must overload the insertion operator from outside of the class, like so:

ostream& operator<<(ostream& lhs, const MyObject& rhs)

{

lhs << rhs.get_data();

return( lhs );

}

Passing an array name to a pointer assigns the first memory location of the array to the pointer variable?

Yes, passing an array name to a pointer assigns the first memory location of the array to the pointer variable. An array name is the same as a pointer to the first location of the array, with the exception that an array name is a r-value, while a pointer is an l-value.

What are the arguments for and against making identifiers case sensitive in programming language?

The simple argument for case-sensitivity is that character 'A' (#65) and character 'a' (#97) are not the same character. To us humans they are clearly the same letter, but computers are less forgiving. Back in the good old days when RAM and CPU time were expensive, it simply wasn't worth the effort or expense to have the computer treat them as being the same. In those days, the onus was largely upon the programmer to ensure they identified variables correctly. Programmers were far more concerned with speed of compilation and, having been brought up within the case-sensitive world of Unix, they were intelligent enough to know that MAX and max are not the same identifier as far as the computer was concerned.

As the cost of computing fell and more and more people wanted to program them, the unforgiving nature of case-sensitive programming became a greater concern, thus languages such as BASIC were developed to address the problem and make life easier for newcomers. The same can be said of DOS, from which MS-DOS evolved. But while these languages and operating systems are more forgiving, they lack the flexibility and speed of a case-sensitive environment.

In C, for instance, the inherent case-sensitivity makes it possible to use programming conventions such as all uppercase for macro identifiers and lowercase for C identifiers. Macros are not actually part of the C language -- they are used by the preprocessor to prepare the C source code for compilation -- but it is helpful to be able to differentiate a macro from an actual identifier in your code, even when they have the exact same name.

MAX and max are not the same, but then neither is Max or mAx. Normally you wouldn't use mixed-case like this in C, as it makes your code hard to read, but the language is flexible enough to permit it should you wish to do so. And that is the main advantage: flexibility. The main disadvantage is the lack of forgiveness. But if you want forgiveness, you wouldn't be programming in C to begin with, you'd use something far more abstract instead, like Visual Basic.

Flowchart of find the difference sum product and average of A and B Print the result?

See if flowchart is made then good reason is drawn for it. So, flow chart is essential for our cultural and social life.hence ,flowchart is important.

Why an operation to check queue overflow is not implemented on linked queue?

In linked queue we're dynamically allocating the memory and there's no fixed memory limit in Linked Queue. That's why there's no operation for overflow.

I guess It's the correct reason

What is the codes for 4 numbers that sort from highest to lowest turbo c plus plus?

Use an associative container such as std::set and enumerate in reverse order, or supply a predicate that sorts in descending order such as std::greater<T> (default is std::less<T>).

What is a basic shell c plus plus?

A basic shell program should essentially emulate a command prompt with a very limited set of commands, such as exit to close the shell and possibly cd to change the current directory. Shell's are typically used to spawn other processes, but a basic shell will typically limit which processes may be allowed to run.

What are the three required sets of statements for every function that a programmer writes in C plus plus?

There is no requirement for any statement in a C++ function, let alone three sets of statements. For instance, the following is a perfectly valid function:

void foo(){}

Clearly this does nothing as it has no statements in the function body, but it is nevertheless a valid function.

Perhaps you mean something else by "statements". The only requirement of a function is that it have a return type, a valid name, an argument list and a function body. The return type may be void, of course, and the argument list may be empty, but it must include the ellipses. The function declaration need not include the function body, and the argument list need only specify the type of argument (the argument names are optional and need not match those declared in the actual definition).

The function name and the arguments define the function signature (the prototype), thus the three required "components" of a function are the return type, the signature and the function body.

What is an alias name given to a variable in opp with c plus plus?

An alias is a reference, an alternate name for a variable or constant. You can assign the address of any variable or constant to a reference of the same type. A reference is a bit like a constant pointer to the type but, unlike a pointer, a reference has no address of its own thus you cannot store references. More importantly, references can never be NULL. They are simply an alternative name by which you can refer to an existing variable or constant. When you assign a value to an existing reference to a variable, you are assigning the value to the variable itself. When you pass a reference to a function, you are passing the address of the value being referred to, and that address is assigned to the function's reference argument and is local to the function. This is not unlike passing a pointer, but pointers may be NULL, references are guaranteed to be non-NULL (a NULL reference invalidates your program).

Note that C++ references are not the same as C reference variables or constants. In C, a reference variable is simply a non-const pointer, while a reference constant is a constant pointer. Hence pointers can be dereferenced (both in C and C++). But in C++, a reference is neither a variable nor a pointer, but is constant (it always refers to the same object and cannot be reassigned once assigned).

What are the advantages of sorting c plus plus?

It's always much quicker (on average) to search a sorted array than it is to search an unsorted array. This is because you can start in the middle of the array. If the value you seek is there then you're done. If it is not there, but the value you seek is less than the value you found, then you immediately know it must be in the left half of the array, otherwise it must be in the right half. In other words you've reduced the number of items to be searched by 50%. If you repeat the process with this smaller subset you'll reduce the search to 25% of the original array, then 12.5%, and so on. Eventually you'll find the value you're looking for. The worst case for any search is when the value does not exist. But you'll know that as soon as the reduced subset has no elements.

We often use Big-O notation to determine how long we expect an operation to take. We know that we can access any element in an array in constant time, which is denoted O(1). We also know that we can compare values in constant time. So we're really only interested in the cumulative time it takes to search the array. For an array of n elements, the worst case involves inspecting every element, which is denoted O(n), which means O(1)*n, or n constant-time operations. For a large array of say, 1,000,000 elements, that's a lot of constant-time operations.

If we imagine an unsorted array, it will always take O(n) time to determine that a value does not exist. But if we sort the array and inspect each element in turn, it will only take O(n/2) time on average because we can stop searching as soon as we find a value that is greater than the one being sought. However, the worst case is still O(n) if the value being sought happens to be greater than any in the array. But if we start in the middle of a sorted array, the average and worst cases both drop to just O(log n).

Note that there will be some overhead in recalculating the subset boundaries, which is itself a constant-time operation, but this is only of concern when dealing with small arrays. It would, in fact, be much quicker to search small arrays one item at a time. For larger arrays, reducing the problem by 50% on each iteration means the overhead quickly becomes irrelevant, but it only works when the array is sorted. Hence new programmers tend to spend an inordinate amount of time studying sorting algorithms to ensure their programs operate as efficiently as possible.

Some algorithms are better than others, but there is no single algorithm that is suitable in every situation. For instance, insert sort is the ideal sorting algorithm for small subsets, but when dealing with a subset of more than a few hundred items it is woefully inadequate. And while quicksort performs extremely well when dealing with large amounts of data, it is unstable (equal items may not be in the same order they were input) and it doesn't work at all when the amount of data is so enormous that it simply will not fit into working memory. For that you need merge sort which uses multiple tape drives or separately controlled disks to sort the data. With slow-to-access media such as this, efficiency is far more important than raw speed alone.

What is the technical name for calling a base class constructor using derived class constructor?

You cannot actually call any constructor, you can only invoke construction, either by instantiating a static instance of a class or by dynamically creating one with the new operator. In the case of base class construction via a derived class, the base class constructor is invoked by the derived class' initialisation list.

Every class constructor has an initialisation list whether you define one or not (the compiler will generate one automatically if you don't). When you derive one class from another, the derived class initialisation list invokes a call to the base class default constructor. The only exception is the compiler-generated copy constructor which automatically calls the base class copy constructor.

If you define your own initialisation list, then you can explicitly invoke any base class constructor overload, thus making your construction code all the more efficient. However, copy constructors should always invoke the base class copy constructor, so if you define a copy constructor, you must explicitly invoke the base class copy constructor -- the compiler will not invoke it implicitly from a user-defined copy constructor.

While many programmer's use the constructor's body to initialise a class, this is highly inefficient. Even if you don't specify an initialisation list, one is created for you, resulting in every base class and every member variable being initialised twice, which can quickly add up to a substantial cost in performance.

The constructor's body should only really be used for initialisation when it would be difficult or impossible to do so from the initialisation list. Remember that your object doesn't physically exist until initialisation is complete, so you may not have access to some members, particularly base class members, at certain points in the initialisation process.

Initialisation must be done from the ground up, starting with the base classes and ending with the actual class members, and all in the order they were declared. Note that only direct base classes (or virtual base classes) should be invoked from the initialisation list. The base classes themselves should invoke their own base class constructors, if they have any. Thus no matter which derivative you construct, the least-derived class is always constructed first.

Is it possible to use a for loop in place of a while loop?

Yes.

while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below:

for(;condition;)

eg:

for(;i<=n;)

Write a program in C plus plus to store 100 element within array and find the particular element if exist?

#include<iostream>

#include<iomanip>

#include<time.h>

template<typename T>

size_t find(T& data, T a[], size_t size)

{

size_t index=0;

do {

if(a[index]==data)

break;

} while(++index<size);

return(index);

}

template<typename T>

void print(T a[], size_t size)

{

using std::cout;

using std::endl;

using std::setw;

size_t index=0;

do{

if(index&&index%20==0)

cout<<endl;

cout<<setw(3)<<a[index];

}while(++index<size);

cout<<endl;

}

int main()

{

srand((unsigned)time(NULL));

const size_t size=100;

unsigned int a[size];

size_t index=0;

do{

unsigned int data=rand()%100;

do{

data=rand()%100;

} while(find(data,a,index)<index);

a[index]=data;

} while(++index<size);

print(a,size);

}

What is the difference between the structure tag and structure variable?

The structure tag is a type. The structure variable is an instance of that type.

How to Write a c plus plus program to insert a value n after the node in the linked list?

To insert a value after a node in a linked list, first allocate and initialize the node, then add it to the current node...

newnode* = new node (n);

newnode->next = current.next;

if (current != head) current->next = newnode; else head = newnode;