C Programming - Dynamic Pointer Cast vs Dynamic Reference Cast

Only difference is that in case of bast casts to references a suitable handler can be provided.

Like the one below


try {
A &a = dynamic_cast < A & >(b); // suppose that this fails
}
catch (bad_cast) {
// do what ever you want
}

Labels:

Posted by - at 1:54 am | 0 comments read on

C++ Programming - Using Declarations for Access Control


class A {
private:
int a;
protected:
int b;
public:
int c;
}
class B: private A {
using B::b;
using B::c;
}


This gives access to B::b and B::c but not B::a.

Labels:

Posted by - at 6:36 am | 0 comments read on

C Programming - Format Strings are Important!!

Guess, What will be the output?


#include < stdio.h >

int main(int argc, char *argv[])
{
int i=10;
int *j=&i;
printf("aaa%n\n");
printf("%d\n",i);
}



I ran this program on .NET platform with optimization disabled, it should be portable across all other platforms also.

Labels:

Posted by - at 11:44 pm | 3 comments read on

C Programming - Is that a Prime Number?

Interviewers favourite :)

The problem is to find out whether a number is prime or not?

This is the one best solution that I could find out!!!
Solution:

/* returns 1, if number is prime else 0 */
int prime(int number)
{
int iter,bound;
if (!(number % 2)) return (number == 2);
if (!(number % 3)) return (number == 3);
if (!(number % 5)) return (number == 5);
for(iter = 7; (iter * iter)<= number; iter += 2)
if ( number % iter == 0 )
return 0;
return 1;
}

Labels:

Posted by - at 9:57 pm | 1 comments read on

C Programming - Max Sum

You have an array of n nonzero numbers. The array is randomly filled with positive and negative numbers.

Write an algorithm/program to find 2 such indexes in the array, so as the sum of the numbers between these indexes is the maximum.

For e.g. if the array is
10, -20, 9, 8, 3, -12, 6, 3, 4, 1, -5, 3, -1
Then the indexes would be 2 and 9, the sum is 9 + 8 + 3 + -12 + 6 + 3 + 4 + 1 =22

At first glance it might look that the correct answer is 2 and 4, the sum is 9 + 8 + 3 =20

And if u r thinking that if we sum up all, then the sum is 10 + -20 + 9 + 8 + 3 + -12 + 6 + 3 + 4 + 1 + -5 + 3 + -1 = 9.

In some cases it might turn out to be the right one, but not always.

Solution:


#define LIST 10, -20, 9, 8, 3, -12, 6, 3, 4, 1, -5, 3, -1

#define LIST -5, -2, -1

#define LIST -5, -2, 0



int main()

{

int nArray[]={LIST};

int nMax,nTempMax,i;

int nStartIndex=0, nStopIndex=0,nSIndex=0;

nMax=nTempMax=nArray[0];

for(i=1;i < sizeof(nArray)/sizeof(int);i++) {

(nTempMax +nArray[i] > 0) ? (nTempMax +=nArray[i]) : (nSIndex= i, nTempMax =nArray[i]);

(nMax < nTempMax ) ? (nMax = nTempMax, nStartIndex = nSIndex, nStopIndex = i) : 0;

}

printf("Start: %d, Stop: %d Max: %d\n",nStartIndex+1,nStopIndex+1,nMax);

}

Labels:

Posted by - at 3:23 am | 0 comments read on

Archives

  • May 2011
  • February 2009
  • December 2008
  • November 2008
  • October 2008
  • August 2008
  • April 2008
  • December 2007
  • November 2007
  • January 2007
  • December 2006
  • October 2006
  • September 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005

Links