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.
What Kind of reaction is symbolized by ab plus c CB plus a?
I suppose that you think to synthesis reaction.
When operator function is declared as friend function?
Ideally, never. Friend functions should only be employed when a function (whether an operator overload or not) requires private access to a class, and it is not otherwise possible to provide a public interface without unduly undermining the class encapsulation. However, as programmer, it is your responsibility to ensure all friend functions adhere to the same class rules (which you yourself define) as do the members of your class, even though friends are not regarded as being members of the class. Ultimately, if you have no control over the friend function implementation, then you must not allow that function to be a friend of your class, as this will seriously undermine the encapsulation.
Difference between C and C plus plus languages?
C was the C++ predecessor. As it's name implies, alot of C remains in C++. Although not actually being more powerful than C, C++ allows the programmer to more easily manage and operate with Objects, using an OOP (Object Oriented Programming) concept.
C++ allows the programmer to create classes, which are somewhat similar to C structures. However, to a class can be assigned methods, functions associated to it, of various prototypes, which can access and operate within the class, somewhat like C functions often operate on a supplied handler pointer.
Although it is possible to implement anything which C++ could implement in C, C++ aids to standarize a way in which objects are created and managed, whereas the C programmer who implements the same system has alot of liberty on how to actually implement the internals, and style among programmers will vary alot on the design choices made.
In C, some will prefer the handler-type, where a main function initializes a handler, and that handler can be supplied to other functions of the library as an object to operate on/through. Others will even want to have that handler link all the related function pointers within it which then must be called using a convention closer to C++.
To finish this discussion, C++ applications are generally slower at runtime, and are much slower to compile than C programs. The low-level infrastructure for C++ binary execution is also larger. For these reasons C is always commonly used even if C++ has alot of popularity, and will probably continue to be used in projects where size and speed are primary concerns, and portable code still required (assembly would be unsuitable then).
What is null pointer assignment?
This error message means that somewhere in your program you have used a pointer-varible containing NULL-value. (Within an actual OS it with stop the program immediately, but in MS-DOS it doesn't.)
What is diff between method and class?
method is a process
property is about it.
please check the answer from others also. because its only my guess.
SORRY
THANK YOU
What are escape sequences in c language?
Escape sequences are combination of characters which when used together form one single unit with a special meaning.Eg:
when a blackslash('\') and 'n' are written together like '\n', this represents a newline character.
For more escape sequences visit the related link.
What is meant by the data type of a variable in c plus plus?
The built-in (fundamental) data types are int, char, bool, float, double and pointer. All other integral types are either modified types or aliases (typedefs). User-defined types (including those provided by the standard library) are not considered part of the language.
What is meant by a function returning a value in c?
Consider this example:
#include
int add(int x, int y)
{ int n=x+y;
return n; }
int main()
{ Using namespace std;
cin >> x;
cin >> y;
cout << add(x,y);
return 0; }
What happens is the main() function asks the user for 2 integers then sends them to add() in the cout statement. Add() adds the two integers and returns the sum to the function that called it (main()) so technically 'add(x,y)' in the cout statement is replaced with add()'s return value. So if the user said x=1 and y=2, the program would print 3. NOTE:
For example, return 0 in main(). Anything else is an error code that is RETURNED TO THE OS or SHELL.
In the of Unix/Linux, we can see the returned value of main using
echo $?
In the case of Windows, we can see the returned value of main using
echo %ERROR_LEVEL%
while we are in DOS or Command prompt
Write program to implement stack with POP and PUSH operations?
SOURCE CODE:
#include
#include
void push(int st[],int data,int &top);
void disp(int st[],int &top);
int pop(int st[],int &top);
int flg=0;
int top=-1,tos=-1;
int st[50];
void push(int st[],int data,int &top)
{
if(top==50-1)
flg=0;
else
{
flg=1;
top++;
st[top]=data;
}
}
int pop(int st[],int &top)
{
int pe;
if(top==-1)
{
pe=0;
flg=0;
}
else
{
flg=1;
pe=st[top];
top--;
}
return(pe);
}
void disp(int st[],int &top)
{
int i;
if(top==-1)
{
printf("\nStack is Empty");
}
else
{
for(i=top;i>=0;i--)
printf("\t%d",st[i]);
}
}
void main()
{
int dt,opt;
int q=0;
clrscr();
printf("This Program Is Used to Perform PUSH & POP operations On Stack");
printf("\n\n\tMain Menu.........");
printf("\n\n1.Push");
printf("\n\n2.Pop");
printf("\n\n3.Exit");
do
{
printf("\n\n\tEnter Your Choice 1-3:");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the Element to be Push:");
scanf("%d",&dt);
push(st,dt,tos);
if(flg==1)
{
printf("\nAfter Inserting the Element, Stack is:\n\n");
disp(st,tos);
if(tos==50-1)
printf("\nStack is Now Full");
}
else
printf("\nStack Overflow Insertion Not Possible");
break;
case 2:
dt=pop(st,tos);
if(flg==1)
{
printf("\n\tData Deleted From the Stack is:%d\n",dt);
printf("\n\tAfter Deleting the Element from the stack is:\n\n");
disp(st,tos);
}
else
printf("\nStack Empty,Deletio Not Possible:");
break;
case 3:
q=1;
break;
default:printf("\nWrong Choice Enter 1-3 Only");
}
}while(q!=1);
}
OUTPUT
Main Menu.........
1.push
2.pop
3.exit
Enter your choice 1-3:1
Enter the element to be push:4
After inserting the elements,stack is:
4
Enter your choice 1-3:1
Enter the element to be push:7
After inserting the elements,stack is:
7 4
Enter your choice 1-3:1
Enter the element to be push:4