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 to write a Program to print powers of 2 in c plus plus?

#include<iostream>

#include<list>

class big_num

{

friend std::ostream& operator<< (std::ostream&, const big_num&);

public:

big_num(unsigned __int64 value = 0): m_list()

{

do

{

m_list.push_front ((value % 10) + '0');

} while (value /= 10);

}

big_num& operator*= (unsigned __int64 value)

{

switch (value)

{

case (0):

m_list.clear();

m_list.push_front ('0');

break;

case (1):

break;

default:

unsigned __int64 carry = 0;

for (std::list<char>::reverse_iterator i=m_list.rbegin(); i!=m_list.rend(); ++i)

{

unsigned __int64 digit = (*i - '0') * value + carry;

*i = (digit % 10) + '0';

carry = digit / 10;

}

while (carry)

{

m_list.push_front ((carry % 10) + '0');

carry /= 10;

}

}

return *this;

}

private:

std::list<char> m_list;

};

std::ostream& operator<< (std::ostream& os, const big_num& num)

{

for (std::list<char>::const_iterator i=num.m_list.begin(); i!=num.m_list.end(); ++i)

{

os << *i;

}

return os;

}

int main ()

{

// test big_num for powers of 2 up to and including 2^100

big_num num(1);

unsigned n = 0;

do

{

std::cout << num << std::endl;

num *= 2;

} while(n++<100);

}

Example output:

1

2

4

8

16

32

64

128

256

512

1024

2048

4096

8192

16384

32768

65536

131072

262144

524288

1048576

2097152

4194304

8388608

16777216

33554432

67108864

134217728

268435456

536870912

1073741824

2147483648

4294967296

8589934592

17179869184

34359738368

68719476736

137438953472

274877906944

549755813888

1099511627776

2199023255552

4398046511104

8796093022208

17592186044416

35184372088832

70368744177664

140737488355328

281474976710656

562949953421312

1125899906842624

2251799813685248

4503599627370496

9007199254740992

18014398509481984

36028797018963968

72057594037927936

144115188075855872

288230376151711744

576460752303423488

1152921504606846976

2305843009213693952

4611686018427387904

9223372036854775808

18446744073709551616

36893488147419103232

73786976294838206464

147573952589676412928

295147905179352825856

590295810358705651712

1180591620717411303424

2361183241434822606848

4722366482869645213696

9444732965739290427392

18889465931478580854784

37778931862957161709568

75557863725914323419136

151115727451828646838272

302231454903657293676544

604462909807314587353088

1208925819614629174706176

2417851639229258349412352

4835703278458516698824704

9671406556917033397649408

19342813113834066795298816

38685626227668133590597632

77371252455336267181195264

154742504910672534362390528

309485009821345068724781056

618970019642690137449562112

1237940039285380274899124224

2475880078570760549798248448

4951760157141521099596496896

9903520314283042199192993792

19807040628566084398385987584

39614081257132168796771975168

79228162514264337593543950336

158456325028528675187087900672

316912650057057350374175801344

633825300114114700748351602688

1267650600228229401496703205376

I have a C plus plus project Visual binary search tree showing traversing graphically . . Can someone explain what it means?

It means you have to represent the tree graphically, much like a family tree, such that when traversing the tree you highlight the currently active node in some way.

Can a variable name can be passed to a value parameter in c plus plus?

No. Pass by value always receives a copy of the value being passed. Even if it were possible to physically pass a user-defined identifier into a function by value, the compiled code would not recognise the name since all identifiers are stripped out by the compiler and replaced with memory addresses.

Strictly speaking, even pass by reference does not pass the variable name, as the function argument is simply an alias, an alternate but informal name, for the formal name you actually pass. In essence you are passing the memory address of the variable, rather the value of the memory address as you would with pass by value.

Greatest of n numbers-c plus plus program?

#include<iostream>

using namespace std;

int main()

{

int N;

cout<<"\nHow many numbers?";

cin>>N;

int arr[N];

cout<<"\nEnter the numbers: ";

for(int i=0;i<N;i++)

cin>>arr[i];

int max=arr[0];

for(int i=1;i<N;i++)

if(arr[i]>max)

max=arr[i];

cout<<"\nThe maximum is "<<max;

return 0;

}

How do I find my compiler on my computer?

Most likely you haven't installed any compilers, otherwise you would know.

What is c if c plus c plus c equals 6?

C = 2

This is solved through the following steps:

Set up the equation

c+c+c=6

Combine like terms

3c=6

Divide both sides by 3

c=2

What is a primary function in C plus plus?

There is no such thing. You probably meant the main function. The main function is the only function that is required as it serves as the entry point of the program.

How do you enter a sentence as input in c plus plus?

Use the C++ getline() function from the standard library.

Can static member function be overloaded?

Yes. Any function can be overloaded. However you cannot override a static member function. Only instance members can be overridden.

Can you have inheritance without polymorphism?

Yes. Inheritance and polymorphism are two different things.

Inheritance is when the attributes and methods of a class are inherited by a deriving class that creates a more specialized type.

Polymorphism is when two methods exist with the same name, differing only in argument types, or in class type. The former type, argument types, is an example of ad-hoc polymorphism that does not even require a class.

Example of Fibonacci in c plus plus?

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

}

How does xcode 3.2 work with c plus plus?

XCode is really nothing more than a GUI front-end for a wide-variety of coding tools. Both C and C++ are built-in to the Mac operating system so you can use the cc command line tool to compile both C and C++, however most users prefer a GUI to the command line, thus gaining the advantage of a more integrated development environment (IDE). XCode integrates with gcc, which is better suited to cross-platform development than the built-in cc compiler.

Code for changing background color in C plus plus?

There is no generic C++ code for changing background colours as consoles are platform-specific. In Windows, for instance, you would use the SetConsoleTextAttribute function. The following code demonstrates how it works:

#include<iostream>

#include<Windows.h>

int main()

{

for(int colour=0x00; colour<=0xff; ++colour)

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),colour);

std::cout<<"Using colour:"<<colour<<std::endl;

}

return(0);

}

Note that the least-significant nybble (4-bits or a half byte) changes the foreground colour, while the most-significant nybble changes the background colour.

Write a program in c plus plus to call a function and function has essentional exception handling statements?

#include<iostream>

extern void handle_eptr (std::exception_ptr eptr)

{

try

{

if (eptr)

std::rethrow_exception (eptr);

}

catch (const std::exception& e)

{

std::cerr << "Unhandled exception: " << e.what() << "\n";

}

}

void bar()

{

// Throw "invalid string position" exception.

std::string().at(1);

}

void foo()

{

try

{

std::cout << "Calling bar() from foo():\n";

bar();

}

catch (const std::exception& e)

{

std::cerr << "Exception in foo(): " << e.what() << "\n";

}

}

int main()

{

try

{

std::cout << "Calling foo() from main():\n";

foo();

std::cout << "Calling bar() from main():\n";

bar();

}

catch (...) // catch all exceptions...

{

handle_eptr (std::current_exception());

}

}

Write a C plus plus code segment that generates and displays 100 random 3-digit positive integers?

#include<stdio.h> #include<stdlib.h>

int main(void)

{

int i;

int j[100];

for(i=0;i<100;i++)

{

j[i]=rand()%100;

printf("%d",j[i]);

}

}

In c plus plus are local declarations visible to the function in a program?

If you declare a variable inside of any fuction (except main) it will not be available to other functions.

How C plus plus program to implement employee directory which will let the organization to perform the functions. Insert the record of new employee?

#include<iostream>

#include<vector>

#include<string>

struct employee

{

std::string forename;

std::string surname;

std::string department;

};

struct directory

{

std::vector<employee> v;

void add_employee (const std::string&, const std::string&, const std::string&);

};

void directory::add_employee (const std::string& forename, const std::string& surname, const std::string& department)

{

v.push_back (employee{forename, surname, department});

}

int main()

{

std::string forename, surname, department;

std::cout << "Enter employee's forename: ";

std::cin >> forename;

std::cout << "Enter employee's surname: ";

std::cin >> surname;

std::cout << "Enter employee's department: ";

std::cin >> department;

add_employee (forename, surname, department);

}

Check duplicate elements in 100 elements array c plus plus?

1. By Sorting

2. By Hashing

3. By Brute Force

1. Sort the array using any of the sorting algorithms, quick sort, bubble sort, etc, and check each consecutive pair for equality.

2. Create a hash table, then insert each element to the table, checking for matching elements.

3. Take first element and check all others if they are equal to to that, Continue with the second to the end, third to to the end and so on.

//sample program....

#include<stdio.h>

main()

{

int a[100];

int i,k;

printf("\n Enter the elements of the array......\n");

for(i=0;i<100;i++)

{

scanf("%d",&a[i]);

}

printf("\n Entered Array...\t");

for(i=0;i<100;i++)

{

printf("%d\t",a[i]);

}

for(i=0;i<100;i++)

{

for(k=0;k<100;k++)

{

if(a[i]==a[k])

{

printf("\t Duplicate element:%d",a[i]);

}

}

printf("\n");

}

}