One Way to detect the memory leaks is to check that while using Inheritance and Virtual Functions some where we have initialized baseclass pointer with the derived class object but when calling destructor the destructor that u have used in the baseclass is not declared as Virtual.
eg:
#include
using namespace std;
class Base1 {
public:
~Base1() { cout « “~Base1()\n”; }
};
class Derived1 : public Base1 {
public:
[Read More]
wo teen women wali puzzle ka sol..
Q : Three very intelligent women (rare case :P) are sitting across a circular table. A fourth person notifies them that he will blind fold them and put either blue or red mark mark on their forehead. Then he will open their eyes. If any woman sees a red mark on the forehead of any of the women, she should raise her hand. And as soon as, one can logically deduce the mark of her forehead, she should then lower her hand, if she raised it earlier.
[Read More]
how to chk if a no. is integer or not.
Source : http://stackoverflow.com/questions/784563/c-check-whether-is-number-is-int-float/784825
#include
using namespace std;
int main(){
double num1;
cin»num1;
if(static_cast(num1) == num1)//casts num1 to type int
cout«"Integer"<
else
cout«"Not Integer"<
return 0;
}
Another method is
char *test = "asdf1234";
int i;
for (i = 0; i < strlen (test); i++)
{ if (isdigit (test[i]))fprintf (stdout, "test[%d] is a digit!\n", i);
}
print 1-100 no.swithout loop in c
Basically the Question generally is to Write a C function to print all numbers from 1 to m (m is the input no) without any loop specifically for, while, switch, do and recursion
#include
#include
char globalStr[1000000];
void nprint(int n)
{
char strn[7], *cp;
sprintf(strn,"%d\n”, n);
cp = strstr(globalStr, strn);
*(cp + strlen(strn)) = 0;
printf("%s”,globalStr);
}
int main(){
int n,i;
globalStr[0] = 0;
/* Lopp is not allowed only in the function nprint()……still if not convinced, you are free to form the string manually :P*/
[Read More]
Write a C program which when compiled and run, prints out a message indicating whether the compiler that it is compiled with, allows /* */ comments to nest.
#include
int allowed(){
int a=1;
/* /* */ a=0; // */
return a;
}
int main(){
if (allowed())
printf(“Nested comments Allowed\n”);
else
printf(“Nested comments not Allowed\n”);
return 0;
}
In case of nested comments not allowed, we have it as /*/* */ a=0;// */
Bigger font show commented area……..
What if the single line comment style is not supported?
After all, single line style comments were introduced with C++………..
#include
[Read More]
Find the nth Ugly Number
Problem
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. …By convention, 1 is included. Write a program to find and print the 1500’th ugly number. Solution
Method 1 - Brute force
Loop for all positive integers until ugly number count is smaller than n, if an integer is ugly than increment ugly number count.
[Read More]
Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)
**Syntax of strstr** const char * strstr ( const char * str1, const char * str2 );
Returns a pointer to the first occurrence of _str2_ in _str1_, or a null pointer if _str2_ is not part of _str1_. The matching process does not include the terminating null-characters. **Approach1** Append str1 to itself and do a strstr for str2. That should work. (I am assuming strstr looks for string x within string y).
[Read More]
Reverse a linked list
Problem Reverse a linked list.
Follow up - Can you do it with only 2 pointers.
Solution This is one of the famous interview questions.
Following are two procedures to reverse a single linked list:
Iterative Procedure Recursive Procedure Iterative solution Method 1 - Iterative Procedure
The following are the sequence of steps to be followed:
Initially take three pointers: PrevNode, CurrNode, NextNode Let CurrNode point to HeaderNode of the list.
[Read More]
Remove duplicates from a sorted linked list
Problem : Remove duplicates from a sorted linked list
Example
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
Solutions
As the linked list is sorted, we can start from the beginning of the list and compare adjacent nodes. When adjacent nodes are the same, remove the second one. There’s a tricky case where the node after the next node needs to be noted before the deletion.
// Remove duplicates from a sorted list void RemoveDuplicates(struct node\* head) { struct node\* current = head; if (current == NULL) return; // do nothing if the list is empty // Compare current node with next node while(current->next!
[Read More]
Insert nodes into a linked list in a sorted fashion
The solution is to iterate down the list looking for the correct place to insert the new node. That could be the end of the list, or a point just before a node which is larger than the new node.
Note that we assume the memory for the new node has already been allocated and a pointer to that memory is being passed to this function.
// Special case code for the head end **void linkedListInsertSorted(struct node** headReference, struct node* newNode) ** { // Special case for the head end if (*headReference == NULL || (*headReference)->data >= newNode->data) { newNode->next = *headReference; *headReference = newNode; } else { // Locate the node before which the insertion is to happen!
[Read More]