Search This Blog

Thursday, 3 January 2013

What will be the output of the following program in c++?

What will be the output of the following program in c++?
when value of a is input as (i)6 , (ii)0:


int a, b=3 ;
cin >> a ;
if (a)
b=a++ -1;

cout<<"a="<<a<<endl ;
cout<<"b="<<b<<endl ;
--------------------------------------…
I want to know to know how the result came?
so please explain me.

Answer:
if (a), when a is either positive or negative as int x = -1;
if (x)
cout << "R";// This will be printed => Condition is true.
When anything within brackets is +ve or negative in if's brackets condition is considered as true.
Similarly if within the bracket of if 0 comes, the condition is False i.e.
if (0)
cout << "R"; // R will not be printed.

In your program, when user enters 6, a number other than ZERO, condition becomes true and the thing with in the body of if is executed => b's value will be replaced by (6-1) = 5. (if user enters 6 as 'a')
When user enters 0, the statement within the if body will not be executed, and value stored in b will remain unchanged (i.e. b = 3).

(i) a = 6 + 1 = 7 // 6 + 1 because it was incremented.
    b = 6 - 1 = 5

(ii) a = 0 
     b = 3.


Question:
1. Which of the following is true?
A. 1
B. 66
C. .1
D. -1
E. All of the above

Wednesday, 2 January 2013

What is the difference between a pointer and an array?

What is the difference between a pointer and an array?

Pointers are variable. You point to one variable, and after sometime you start pointing to another variable with the same pointer.
Example:
int x = 1;
int *p = &x;
//in the next line you can say
*p = &b; // if b has been initialized.

Array name is the address to the first index of array. Array name is a CONSTANT Pointer => You cannot do a = a + 1; if a is an array. It will generate an error since it always point to same location.
When we do for example cout << *(a+1); (a is arraY) We give an offset and elements at 2nd index is printed. a still points to the first location.

Thursday, 27 December 2012

Difference between cin.getline and cin in C++

You should know the difference between
char string[ 10 ];
  cin >> string;
And
cin.getline(string,10,'\n');

In cin>>string, user keeps on entering characters unless either Enter key or Spacebar is pressed or 10 characters are entered and stored. Where as in Case of cin.getline User may press Enter key, Spacebar or whatever except the Delimeter character (in this case '\n' which is Enter Key). Size of array should also be kept in mind that compiler won't allow you to store more than size of array (10 characters in this case).

Static in C++

What is Static in C++?
Ans: I believe that Examples are the best way to make someone understand so:


#include <iostream>

using namespace std;
f1();
void main()
{

f1();
f1();
}
f1()
{
static int s = 0;
cout << s++ << endl;
}




The value of 's' does not start from 0 when the function is called second time, rather it starts from last value of of s when function was called last time (i.e. 1 in this case when Function is called second Time).

Tuesday, 11 December 2012

Precedence of operators in C++

Order of execution within a statement is called Precedence.
For Example: 2+2 % 2 gives 2 NOT 0. How did I come to know?
What will happen first is predefined according to the picture below:


 Link to view this image: http://i.imgur.com/lffHR.jpg
OR goto http://en.cppreference.com/w/cpp/language/operator_precedence

Sunday, 9 December 2012

Program to Copy one String in another String in C++


#include <iostream>
using namespace std;

void copy1( char *, const char * );

int main()
{
char String1[10];
char String2[] = "Hello";

copy1(String1 , String2);
cout << String1 << endl;

return 0;
}

void copy1(char *String1, const char *String2)
{
for (int i = -1;  i<10;  String1[i] = String2[i])
i++;
}
//============================================================================
You can also use following copy function.

void copy1(char *String1, const char *String2)
{
for (int i = 0; (String1[i] = String2[i]) != '\0'; i++);
}
//============================================================================
Another way: or you can use following copy function.

void copy1(char *String1, const char *String2)
{
for ( ; (*String1 = * String2) != '\0' ; String1++, String2++);
}

Wednesday, 5 December 2012

Convert lowercase letters to uppercase letters

Header file: cctype

// Converting lowercase letters to uppercase letters
#include <iostream>
#include <cctype> // For the use of toupper Function.
using namespace std;
int main()
{
 char a = 'a';
 char b = toupper(a);
 cout << b << endl;

 return 0;
}
//====================================

Better way:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
 char a = 'a';
 cout << static_cast<char>( toupper(a) ) << endl;
 return 0;
}
//---------------------------------------------------
//You will be thinking now, why we used static_cast<char> ?
//If we would have done as follows, what was wrong?

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
 char a = 'a';
 cout << toupper(a) << endl;
 return 0;
}

Tuesday, 4 December 2012

Interesting Tip: Increment, Decrement Operator:






Changing a variable by adding or subtracting 1 is so common that there is a special
shorthand for it,

x++  =>  x = x+1
x--   =>  x = x-1













You think you know about increment operator?
Lets see!
What will be its output?

Sunday, 2 December 2012

What is Switch in C++?

#include<iostream>
using namespace std;

int main()

{
cout<< "Enter 1 or 2 -------> ";
int a; cin>>a;
switch (a)
{
case 1:
cout<<"1 pressed"<<endl;
break;

case 2:
cout<<"2 pressed"<<endl;
break;

}
return 0;
}
//===============================================================================
You may want to see: Calculator using switch statement. Takes Expression as we write on our copy.