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 can a program that outputs the days of the week using switch statement be created?

#include<stdio.h>

#include<conio.h>

void main()

{

int n;

printf("enter the value of n");

scanf("%d",&n);

switch(n)

{

case1:printf("monday");

break;

case2:printf("tueday");

break;

case3:printf("wednesday");

break;

case4:printf("thursday");

break;

case5:printf("friday");

break;

case6:printf("saturday");

break;

case7:printf("sunday");

break;

default:printf("invalid number");

}

}

How constructor is different from normal member function?

A constructor differs from a normal member function in three ways:

  1. A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void."

    The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).

  2. A constructor is never called explicitly except with the new operator.
  3. Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements.

    The common design practise is that at least the default constructor is declared public.

How operator overloading differ from function overloading?

Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed.

Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.

Can you use c plus plus instead of c sharp in aspnet?

No, not directly. But maybe yes indirectly

The code behind languages of a web page in asp.net are either VB.NET or C#.

No, you cannot develop / write codes in C++ in these code-behind

You still can call out to some components written in C++ from these code-behind pages via C# or VB.NET codes

What is bubble sort?

Bubble sort is a sorting algorithm that compares 2 adjacent items at a time, starting from the beginning of a list, and swapping them if they are out of sequence. Each comparison gradually moves the largest item to the end of the list (likened to a bubble making its way to the surface of water). After n*n passes, all the items will be sorted. The big O for a standard bubble sort is therefore O(n*n).

The algorithm can be improved somewhat. Since it is clear that the last item is sorted on each pass, the unsorted set can be reduced by 1 element on each pass. Moreover, since the final swap on each pass indicates that everything from that point on is already sorted, the unsorted set can often be reduced by more than 1 element on each pass. For an already sorted list, the worst case is reduced to O(n), constant time.

For small sets of data, perhaps 10 to 20 items, the bubble sort is reasonably efficient, especially on partially sorted lists. However the insert sort algorithm offers similar or better performance on average. With larger sets, the quick sort algorithm is hard to beat, but is let down by inefficiencies when dealing with partially sorted lists. Hybrid sorts can improve things a little, however, there is no efficient way to check the state of a list to determine the most efficient algorithm to use at any given point.

Explain virtual functions in C plus plus?

A virtual function in C++ is a function that can have multiple definitions.

For example:

If you have a class which contains a virtual function:

class Virtual

{

virtual void makesomething();

};

That function can be implemented when you inherit that class an implement the function. So:

class Inherit : public Virtual

{

//this is the same function, but can be implemented to do something different

void makesomething() { //do something else }

};

C program to swap two variables without using third variable?

you can do it in the following manner

Supposing your two variables are x and y:

int x=3;

int y=5;

x=x+y; [x becomes 8]

y=x-y; [y assumes the original value of x i.e. 3]

x=x-y; [x assumes the original value of y i.e. 5]

or... you can use the unary XOR (exclusive or) operator, '^=' .

Same values, int x=3, y=5

x ^= y; // x becomes 6

y ^= x; // y becomes 5

x ^= y; // x becomes 3.

The second method has more advantages :

- Its assembler operations never use the processor's ALU carry.

- Without use of the aforementioned carry, no overflow will ever occur.

- Performing this with 32-bits values 8-bits processors will be more efficient, in terms of program space AND execution speed.

- You can even use this in Visual Basic an other languages which implements boundaries on values, while the first method is guaranteed to fail when overflows occurs.

In both case, do not EVER try to factorize these 3 lines into two, as the operations order in multiple-operators lines depends on the compiler's way to parse your code.

Thus , typing

x = x ^ y ^x;

y = y ^ x;

Will surely give you garbage.

Where can you practice c plus plus is there a website that can be useful?

C Plus Plus, or C++ is an intermediate-level computer programming language. It was developed by Bjarne Stroustrup starting in 1979. You can practice C Plus Plus at a site such as Cprogramming.

Write c plus plus program to find the factors of a given number?

It isn't actually possible to generate factorials for negative and/or non-integer numbers.

However, a function for non-negative integers is:

unsigned int factorial(unsigned int n)

{

if(n==0)

{

return 1;

}

return n * factorial(n-1);

}

What is actually passed to a function when an object is passed to a function through a pointer?

When an object is passed to a function by pointer, a copy of the pointer's value is passed to the function. Pointers are always passed by value, but because the value is a memory address than can be dereferenced, they enable us to pass objects by reference. In languages such as C which have no concept of references, this was the only way to pass by reference. C++ introduced proper references (aliases for existing objects), thus when we want to pass by reference we can choose to use a pointer or an actual reference. Normally we'd only pass by pointer when passing an optional argument that defaults to NULL when no argument is given. Otherwise we pass by reference because references can never be NULL.

You are having Problem in c c plus plus assignment Who can help you?

Get Expert C Assignment Help at ProgrammingHomeworkHelp. 📢

Struggling with your C assignments? 🤔 Don't worry, we've got your back! 🎯

🔍 Need quick solutions for complex C programming problems?

📚 Looking for expert guidance to enhance your coding skills?

🎓 Seeking affordable and reliable C assignment help?

🌟 Your search ends here! 🌟

🎉 Introducing Programming Homework Help, your one-stop destination for top-notch C assignment assistance. Our team of experienced programmers is ready to tackle any challenge and provide you with tailor-made solutions. 🎓💼

Why choose us? 🤔

✅ Expert Programmers: Our skilled C programmers are well-versed in handling diverse assignments, ensuring you receive accurate and efficient solutions.

✅ Timely Delivery: No more missing deadlines! We value your time and guarantee timely delivery of your completed assignments.

✅ 24/7 Support: Got a last-minute query? Our customer support is available round-the-clock to address all your concerns.

✅ Plagiarism-Free Work: We take academic integrity seriously, and all our solutions are 100% original and plagiarism-free.

✅ Affordable Prices: Quality assistance doesn't have to be expensive! Our services are priced competitively to fit your budget.

Don't let C assignments stress you out! 🤯 Let our experts handle the technicalities while you focus on excelling in your studies. 📚📈

💡 Excelling in C programming is just a click away! 💡

What is escape sequence?

Escape sequence is the question of I.T related technology

Escape sequence is the question of C++ Language........

Please answer this question # defines the following character escape sequences: * \' - single quote, needed for character literals * \" - double quote, needed for string literals * \\ - backslash * \0 - Unicode character 0 * \a - Alert (character 7) * \b - Backspace (character 8) * \f - Form feed (character 12) * \n - New line (character 10) * \r - Carriage return (character 13) * \t - Horizontal tab (character 9) * \v - Vertical quote (character 11) * \uxxxx - Unicode escape sequence for character with hex value xxxx * \xn[n][n][n] - Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx) * \Uxxxxxxxx - Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates

Who is the father of object oriented programming?

In 1991 Oak was created by the members of the Green Project which included James Gosling (Chief Programmer) , Patrick Naughton and Mike Sheridan.

Patenting issues gave a new name to Oke -Java

Is a while loop a pre-condition or post condition?

The while loop is a pre-condition loop.  It tests the condition at the beginning of each loop, executes the loop if it is true, and goes back to the test after executing each iteration.

What is inline function what are the advantages of using inline function?

Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say:

int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro:

#define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like:

max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like:

max(a++, b++) An alternative in C++ is to use inline functions:

inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type.

Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say:

int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro:

#define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like:

max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like:

max(a++, b++) An alternative in C++ is to use inline functions:

inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type.

Why a function is not defined in to another function?

A function is not deigned in to another function. It is because that would lead to dependency injection.

Sort integer numbers in ascending order using pointers?

It's hardly the most efficient way to sort integers. Integers are primitive data types, so it's actually quicker to sort the integers directly rather than point at them. Pointers just add an unnecessary level of indirection. Pointers should only be used for complex data types for which copying/moving would result in greater inefficiency than would otherwise be incurred through pointer manipulation.

#include<iostream>

#include<vector>

int main()

{

// instantiate a vector of unsorted integers

std::vector<int> v = {7,3,5,2,9,4,1,6,8};

// instantiate a vector of pointers to the integers in the vector

std::vector<int*> p;

for (size_t i=0; i<v.size(); ++i)

p.push_back (&v[i]);

// sort the pointers (uses insertion sort algorithm):

for (size_t i=1; i<p.size(); ++i)

{

int* x=p[i];

size_t gap = i;

size_t pre = i-1;

while (gap && *x<*p[pre])

{

p[gap--] = p[pre--];

}

p[gap]=x;

}

// print to prove the sorting worked:

std::cout << "Original order:\t\t\t";

for (size_t i=0; i<v.size(); ++i)

std::cout << v[i] << '\t';

std::cout<<std::endl;

std::cout << "Sorted order (by pointer):\t";

for (size_t i=0; i<p.size(); ++i)

std::cout << *p[i] << '\t';

std::cout<<std::endl;

}

Write a c plus plus program to find the square root of a number?

#include<iostream>

int main()

{

unsigned num = 25; // or use user-input to assign the value

unsigned root = sqrt ((double) num)

std::cout << "The square root of " << num << " is " << root;

}

Write a C plus plus program to convert a given number into years weeks and days?

#include<iostream>

void num_to_years_weeks_days(

unsigned num, unsigned& years,

unsigned& weeks, unsigned& days)

{

years = num / 365;

num -= years * 365;

weeks = num / 7;

num -= weeks * 7;

days = num;

}

int main()

{

unsigned years, weeks, days;

unsigned num = 1000;

num_to_years_weeks_days(num, years, weeks, days);

std::cout << num << " days is " << years << " years, " << weeks << " weeks and " << days << " days\n" << std::endl;

num = 12345;

std::cout << num << " days is " << years << " years, " << weeks << " weeks and " << days << " days\n" << std::endl;

}

Can you write a CPP program to displaying XOR truth table using logical AND OR and NOT?

I do not believe this is possible. The only solution I could come up with does not involve either OR or NOT, but it does use the plus and minus operators combined with the logical AND operator (more formally termed addition modulo 2, or addition without carry). The following C++ program demonstrates that the function, XOR(), correctly emulates the logical XOR operator (^).

#include

int XOR( int x, int y )

{

return(( x + y ) - ( x & y ) - (x & y ));

}

int main()

{

int x, y;

std::cout << "Truth table for 0 and 1:\n" << std::endl;

std::cout << "x y ^ XOR" << std::endl;

for( x=0; x<=1; ++x )

for( y=0; y<=1; ++y )

std::cout << x << " " << y << " " << (x^y) << " " << XOR( x,y ) << std::endl;

std::cout << std::endl;

std::cout << "Evaluation of values 0 to 4:\n" << std::endl;

std::cout << "x\ty\t^\tXOR" << std::endl;

for( x=0; x<=4; ++x )

for( y=0; y<=4; ++y )

std::cout << x << "\t" << y << "\t" << (x^y) << "\t" << XOR( x,y ) << std::endl;

std::cout << std::endl;

return(0);

}

Output

Truth table for 0 and 1:

x y ^ XOR

0 0 0 0

0 1 1 1

1 0 1 1

1 1 0 0

Evaluation of values 0 to 4:

x y ^ XOR

0 0 0 0

0 1 1 1

0 2 2 2

0 3 3 3

0 4 4 4

1 0 1 1

1 1 0 0

1 2 3 3

1 3 2 2

1 4 5 5

2 0 2 2

2 1 3 3

2 2 0 0

2 3 1 1

2 4 6 6

3 0 3 3

3 1 2 2

3 2 1 1

3 3 0 0

3 4 7 7

4 0 4 4

4 1 5 5

4 2 6 6

4 3 7 7

4 4 0 0

As you can see, the third and fourth columns are all equal, thus proving the function correctly emulates the logical XOR for values 0 to 4. The same is true for all permutations of x and y.

Note that while it is possible to use AND, OR and NOT when both x and y are guaranteed to be in the range 0 to 1 (and therefore capable of producing the truth table shown above), the function does not correctly emulate the XOR operator when x or y is neither 0 nor 1. You can test this by swapping the XOR function above with the following implementation:

int XOR( int x, int y )

{

return(( x | y ) & !( x & y ));

}

Which code i need to delete middle digit in 3 digit number in c plus plus?

If you know that the number input will always be three digits:

output = 10 * (int)(input / 100) + (input % 10);

If you want to idiot proof it (eg. too many digits):

output = 10 * (int)((input % 1000) / 100) + (input % 10);

When you need to satisfy two or more criteria to initiate an event in a program you must make sure that the second decision is made entirely independently of the first decision?

Evaluate the criteria before executing the conditional expression. In pseudocode:

result1 = evaluate( criteria1 );

result2 = evaluate( criteria2 );

if( result1 && result2 ) initiate_event;

Note that this is not the same as the following:

if( evaluate( criteria1 ) && evaluate( criteria2 )) initiate_event;

If the evaluation of criteria1 fails for any reason, then the whole expression fails and criteria2 is never evaluated. so if the evaluations must be done independently of each other, then they must be done prior to the conditional expression, storing the results for use in the conditional expression.

What are the application of singly linked list?

A singly-linked list is ideally suited to stacks (last in, first out). The list maintains a link with the head node in the list and each node points to the next node in the list (the last node points to NULL). When a new node is added it is added to the head of the list. Since enqueue and dequeue both occur at the head of the list, processing is always in constant time [O(1)].

With slight modification (maintaining a pointer to the tail node as well as the head), singly-linked lists can also be used for queues (first in first out). Processing remains constant time [O(1)].

However, random access and search are both linear [O(n/2) and O(n) respectively]. While the list can be modified to maintain a sorted list, insert and seek times still average [O(n/2)].