Is call by reference and pass by reference same thing?
Strictly speaking there is no such term as call by value. Functions are called while parameters are passed. When someone uses the term call by value they really mean pass by value, so in that sense they are the same. However call by value is incorrect terminology.
Menu driven program for selection sort bubble sort and insertion sort in c?
#include<iostream>
#include<time.h>
#include<iomanip>
#include<string>
void swap(int& x, int& y)
{
x^=y^=x^=y;
}
void bubble_sort(int* A, int size)
{
while(size)
{
int n=0;
for(int i=1; i<size; ++i)
{
if(A[i-1]>A[i])
{
swap(A[i-1], A[i]);
n=i;
}
}
size=n;
}
}
void insertion_sort(int* A, int size)
{
for(int i=1; i<size; ++i)
{
int value=A[i];
int hole=i;
while( hole && value<A[hole-1] )
{
A[hole]=A[hole-1];
--hole;
}
A[hole]=value;
}
}
void selection_sort(int* A, int size)
{
for(int i=0; i<size-1; ++i)
{
int j=i;
for(int k=i+1; k<size; ++k)
if(A[k]<A[j])
j=k;
if( i!=j )
swap(A[i],A[j]);
}
}
void sort(int* A, int size, int sort_type)
{
switch(sort_type)
{
case(0): bubble_sort( A, size );
case(1): insertion_sort( A, size );
case(2): selection_sort( A, size );
}
}
int* copy_array(int* A, int size)
{
int* copy=new int[size];
memcpy(copy, A, size*sizeof(int));
return(copy);
}
void print_array(int* A, int size, char* prompt)
{
std::cout<<prompt<<"\t";
for(int i=0; i<size; ++i)
std::cout<<std::setw(2)<<A[i]<<" ";
std::cout<<std::endl;
}
int get_rand(int range_min=0, int range_max=RAND_MAX)
{
return((int) ((double)rand() / (RAND_MAX + 1) * ((range_max + 1) - range_min) + range_min));
}
int input_char(std::string prompt, std::string input)
{
char ch;
do
{
std::cout<<prompt<<": ";
std::cin>>ch;
}
while(input.find(ch)==std::string::npos);
return(input.find(ch)%(input.size()/2));
}
int main()
{
srand((unsigned) time(NULL));
int size = get_rand( 10, 80);
if( int* A = new int[size] )
{
for( int i=0; i<size; ++i )
A[i]=get_rand( 1, size );
int choice=input_char("Please select a sorting method:\n[B]ubble, [I]nsert, [S]election", "bisBIS");
std::cout<<"You chose ";
switch(choice)
{
case(0): std::cout<<"bubble"; break;
case(1): std::cout<<"insertion"; break;
case(2): std::cout<<"selection"; break;
}
std::cout<<" sort...\n"<<std::endl;
print_array( A, size, "Before sorting" );
sort(A, size, choice);
print_array( A, size, "After sorting" );
delete [] A;
}
return(0);
}
What is difference between iostream and iostreamh in c plus plus?
<iostream.h> is an old style of programming and does not allow using namespaces. If you use <iostream> you can use namespaces, and limit number of predefined function (not used) included with your program.
What are destructors in c plus plus?
A destructor in C++ is a method of a class that runs when the class is deleted. It performs cleanup of the members of the class that need cleanup, such as deallocation of subobjects referred to by pointers in the class, subobjects which were earlier allocated by a constructor or other method of the class.
What is linkage in c plus plus?
"An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage"
Types:- External, Internal and None linkage
Example:-
External Linkage= extern int a1;
Internal Linkage= static int a2;
None Linkage= int a3;
Assuming the image is an RGB image, sum the red, green and blue components of the pixels separately then divide each by the number of pixels. E.g., given two pixels RGB(a, b, c) and RGB(x, y, z) the mean is RGB ((a+x)/2, (b+y)/2, (c+z)/2).
Write a program in c plus plus to reverse the sentence?
The simplest solution is to use a template function that will reverse an array of any type. This is achieved by iteratively working from both ends of the array, swapping characters while the left index is less than the right. The following example demonstrates the function using both a character array and a std::string, but the function will also work with an array of any type.
#include<iostream>
#include<string>
template<class T>
void rev_array(T A[], size_t size )
{
size_t left=0, right=size-1;
while( left<right )
{
T tmp=A[left];
A[left]=A[right];
A[right]=tmp;
++left; --right;
}
}
int main()
{
char arr[] = "Hello world!";
std::cout<<arr<<std::endl;
rev_array(arr, strlen(arr));
std::cout<<arr<<std::endl;
std::cout<<std::endl;
std::string str = "The cat sat on the mat.";
std::cout<<str.c_str()<<std::endl;
rev_array(&str[0], str.size() );
std::cout<<str.c_str()<<std::endl;
std::cout<<std::endl;
return(0);
}
Example output:
Hello world!
!dlrow olleH
The cat sat on the mat.
.tam eht no tas tac ehT
Need of constructor in c plus plus?
There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor.
A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.
Why there is a need for C plus plus and how does it overcome the drawbacks of the C language?
Absolutely! C++ and Java are by far the two most popular programming languages in use today. Java is the most popular due to its ease of use, particularly with cross-platform development, but it compiles to byte code rather than native machine code, thus it is nowhere near as efficient as C++ and is therefore unsuitable for time-critical applications. Even its predecessor, C, is still in use today due to the fact that procedural C can be mapped 1:1 with the underlying machine code making it easier to develop small-scale, cross-platform applications such as driver software. However, since C++ is backward-compatible with C, the ability to mix low-level machine code with intermediate object-oriented code is advantageous when developing more complex applications, including operating systems. Java is simply too high level for this.
A program in C plus plus using switch statement To find largest number among three variables?
//The following is very simple program that will find the largest of any 3 numbers
#include <iostream>
using namespace std;
int main()
{
int n1, n2, n3, largest;
cout <<"Enter any 3 numbers ";
cin >>n1;
cin >>n2;
cin >>n3;
if(n1 > n2 && n1 > n3)
largest= n1;
else if(n2 >n1 && n2 >n3)
largest= n2;
else
largest= n3;
return 0;
}
How do you distinguish between a text file and a binary file?
You can distinguish between binary and text files, and for the most part even identify what type of binary file, by using the "file" command. For example:
~$ file unknownfile
unknownfile: PNG image data, 155 x 155, 8-bit/color RGBA, non-interlaced
This tells you that the file is a PNG file by reading metadata and/or magic numbers in the file. When used on a text file, the command will return "ASCII text" or "Unicode text."
Flow chart for addition of two matrices?
For the resulting matrix, just add the corresponding elements from each of the matrices you add. Use coordinates, like "i" and "j", to loop through all the elements in the matrices. For example (for Java; code is similar in C):
for (i = 0; i <= height - 1; i++)
for (j = 0; j<= weidht - 1; j++)
matrix_c[i][j] = matrix_a[i][j] + matrix_b[i][j]
How do you connect Visual Basic with SQL server database?
It depends on what version of VB you are using, and what version of SQL Server you're attempting to connect to. Also, there are several ways to connect from each version of VB to each version of SQL Server (ODBC, ADODB, DSN-less, etc.). Since your question does not provide enough specifics to answer adequately, I refer you to the "Connection Strings" link at the bottom of this page (or you may just type in www.connectionstrings.com in your browser).
How do you display the contents of the memory address stored in an element of a pointer array?
Remember that a pointer is just a variable containing the memory address of another variable. A pointer to a pointer is no different, other than that the address contains the address of another pointer. You use the * indirection operator to get the value of the variable being pointed at (the address of the other pointer), and the ** indirection operator to get at the value pointed at by the other pointer.
The following example illustrates how to access the values of pointers to int via an array of pointers to those pointers.
The memory address and the value of every variable is displayed for the benefit of clarity.
#include <iostream>
using namespace std;
int main()
{
// Set up an array of pointers to pointers to int variables.
int X = 1, Y=2; // The actual variables.
int* pX = &X; // Pointers to those variables
int* pY = &Y;
int** pp[2]; // Array of pointers to those pointers.
pp[0] = &pX;
pp[1] = &pY;
// Print the address of all variables and their stored values:
cout << "Var\t&Address\tValue" << endl;
cout << "---\t--------\t-----" << endl;
cout << "X\t0x" << &X << "\t" << X << endl;
cout << "Y\t0x" << &Y << "\t" << Y << endl;
cout << "pX\t0x" << &pX << "\t0x" << pX << endl;
cout << "pY\t0x" << &pY << "\t0x" << pY << endl;
cout << "pp\t0x" << &pp << "\t0x" << pp << endl;
cout << endl;
cout << "Note that both &pp and pp return the same value: the address of the array." << endl;
cout << "pp is simply an alias for the memory allocated to the array itself, it is" << endl;
cout << "not a variable that contains a value. You must access the elements of the" << endl;
cout << "array to get at the actual values stored in the array." << endl;
cout << endl;
// Use the array elements to access the pointers and the values they point to:
cout << "Elem\t&Address\tValue\t\t*Value\t\t**Value" << endl;
cout << "----\t--------\t-----\t\t------\t\t-------" << endl;
cout << "pp[0]\t0x" << &pp[0] << "\t0x" << pp[0] << "\t0x" << *pp[0] << "\t" << **pp[0] << endl;
cout << "pp[1]\t0x" << &pp[1] << "\t0x" << pp[1] << "\t0x" << *pp[1] << "\t" << **pp[1] << endl;
cout << endl;
return( 0 );
}
What is get function in c plus plus with example?
A "get" or "getter" function is a function that retrieves information. They are called getters because they are often named with a "get_" prefix, such as:
date get_current_date ();
The "get_" prefix is not actually required in C++, it is only required in languages that do not support function overloading. This is because most get functions also have a corresponding set function and you cannot use the same name twice without overloading:
date get_current_date ();
void set_current_date (const date&);
Getters and setters are usually defined as class member functions. However, rather than referring to them informally as getters and setters we refer to them formally as accessors and mutators, respectively. This reflects the fact that a getter typically accesses information without changing it whereas a setter typically mutates or changes information. For example:
class X {
private:
int data;
public:
X (int value): data {value} {} // constructor
X& operator= (int value) { data = value; return *this; } // mutator
int value () const { return data; } // accessor
// ...
};
The accessor is declared const and returns by value. In other words, the caller receives a copy of the information, not the information itself, and the class is left unaffected by the access.
The mutator, on the other hand, is declared non-const because the class representation (the data member) will need to be modified.
Constructors are not mutators because constructors create information, they do not mutate existing information.
Write a c plus plus program to find the greatest of the three numbers?
This snippet assumes you have two values, x and y and you want to find the greater.
int max (int x, int y)
{
if (x > y)
{
return x;
}
return y;
}
Who is the inventor of c plus plus language?
C was initially developed by Dennis Ritchie from 1969 to 1973. C++ was initially developed by Bjarn Stroustrup from 1979 (when it was known as C with Classes) to 1983 (when it was renamed C++). Both developers worked at Bell Labs at the time.
Which operator not overloaded in c plus plus?
The if statement
ex.
if (index < 5)
printf("Index is less than 5\n");
else
printf("index is greater or equal to 5\n");
(You can also replace the "if" with a "?" and the "else" with a "?" -- no, that would be syntax error)
How do you write a program that gives the GCD of three given numbers in C plus plus?
To find the GCD of three numbers, a, b and c, you need to find the GCD of a and b first, such that d = GCD(a, b). Then call GCD(d, c). Although you could simply call GCD(GCD(a, b), c), a more useful method is to use an array and iteratively call the GCD(a, b) function, such that a and b are the first two numbers in the first iteration, which becomes a in the next iteration, while b is the next number. The following program demonstarates this method.
Note that the GCD of two numbers can either be calculated recursively or iteratively. This program includes both options, depending on whether RECURSIVE is defined or not. In a working program you'd use one or the other, but the iterative approach is usually faster because it requires just one function call and no additional stack space.
The program will create 10 random arrays of integers of length 3 to 5 and process each in turn. Note that the more numbers in the array, the more likely the GCD will be 1.
#include<iostream>
#include<time.h>
#define RECURSIVE // comment out to use iterative method
#define ARRAY // comment out to use non-arrays
#ifdef RECURSIVE
// Returns the GCD of the two given integers (recursive method)
unsigned int gcd(unsigned int a, unsigned int b)
{
if(!a)
return(b);
if(!b)
return(a);
if(a==b)
return(a);
if(~a&1)
{
if(b&1)
return(gcd(a>>1,b));
else
return(gcd(a>>1,b>>1)<<1);
}
if(~b&1)
return(gcd(a,b>>1));
if(a>b)
return(gcd((a-b)>>1,b));
return(gcd((b-a)>>1,a));
}
#else
// Returns the GCD of the two given integers (iterative method)
unsigned int gcd(unsigned int a, unsigned int b)
{
if(!a)
return(b);
if(!b)
return(a);
int c;
for(c=0; ((a|b)&1)==0; ++c)
{
a>>=1;
b>>=1;
}
while((a&1)==0)
a>>=1;
do{
while((b&1)==0)
b>>=1;
if(a>b)
{
unsigned int t=a;
a=b;
b=t;
}
b-=a;
}while(b);
return(a<<c);
}
#endif RECURSIVE
// Returns the greatest common divisor in the given array
unsigned int gcd(const unsigned int n[], const unsigned int size)
{
if( size==0 )
return( 0 );
if( size==1 )
return( n[0] );
unsigned int hcf=gcd(n[0],n[1]);
for( unsigned int index=2; index<size; ++index )
hcf=gcd(hcf,n[index]);
return(hcf);
}
int main()
{
using std::cout;
using std::endl;
srand((unsigned) time(NULL));
for(unsigned int attempt=0; attempt<10; ++attempt)
{
unsigned int size=rand()%3+3;
unsigned int* num = new unsigned int[size];
unsigned int index=0;
while(index<size)
num[index++]=rand()%100;
unsigned int hcf=gcd(num,size);
cout<<"GCD(";
index=0;
cout<<num[index];
while(++index<size)
cout<<','<<num[index];
cout<<") = "<<hcf<<endl;
delete[]num;
}
cout<<endl;
}
How do you write a CPP program to find standard deviation of N numbers?
1. Calculate the mean average of the N numbers. Suppose it is stored in the variable D.
2. Now calculate the differences between each number with D and square the value. As there are N numbers so store this difference in an array. Example arr[0] = (num0 - D)^2; arr[1] = (num1 - D)^2; arr[N-1] = (numN - D)^2;
3. Now sum array value and divide by N, suppose the value is stored in F. Now square root F. It is the standard deviation of your N number.
I hope you can write the code by yourself or follow the part:
suppose you will store the N numbers in an array num. Now:
int num[N+2], D = 0; //or declare the num array as float or double if there are any precision value
for(int i = 0; i < N; i++)
{
D += num[i];
}
D /= N;
int arr[N+2];
for(int i = 0; i < N; i++)
{
arr[i] = (num[i] - D)^2; //square the difference.
}
int F = 0; //if precision value is accepted then declare F as float or double not int.
for(int i = 0; i < N; i++)
{
F += arr[i];
}
F /= N;
F = sqrt(F); //use #include <cmath> in your header file list so that you can use sqrt() function or simply use #include <bits/stdc++.h>
cout<<F<<endl;
- Thanks
Write an algorithm to print the factorial of given number and then draw the flowchart?
write an algorithm to print the factorial of a given number and then draw the flowchart.
This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).
Is there any program that can run c but not in c plus plus?
== Yes. It may be funny. You cannot use keywords like newin c++ for variable declaration. But in C it is possible.
== The following definition is valid in C, invalid in C++: char str [3] = "ABC";
What is C plus plus mainly used for?
C++ is a general purpose programming language. It has imperative, object-oriented and generic programming features, while also providing the facilities for low level memory manipulation. Source: Wikipedia.
How do you make a input and output basic statement in C plus plus?
The most basic input/output operations can be performed with std::cin for input and std::cout for output. For example:
#include<iostream>
int main (void) {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
std::cout << "The value " << n << " was entered!\n";
}
How do you Calculate the average of two numbers then return the average in c plus plus?
Use the following function:
/* returns the average of two real numbers */
double average (const double a, const double b) {
return a/2.0 + b/2.0;
}
Note that we do not use (a+b) / 2.0 because the expression a+b could overflow the range of a double. By dividing each value by 2 before summing we ensure the result can never overflow no matter how large a and b are.
Ideally, you should write separate functions to cater for float and long double arguments independently:
/* returns the average of two long doubles */
long double average_lng (const long double a, const long double b) { return a/2.0 + b/2.0;
}
/* returns the average of two floats */
float average_flt (const float a, const float b) {
return a/2.0F + b/2.0F;
}
For mixed-mode arithmetic, always use the highest precision argument. E.g., the average of a float and a double is a double, so use the function that returns a double, not a float. The float argument will be implicitly cast to a double.