Thursday, July 17, 2008

Calculation of Sum, Average and Standard Deviation using Functions and Pointers.
Write a function that receives 5 integers and returns the sum, average and standard
deviation of these numbers. Call this function from main() and print the results in main().



#include
#include

int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);

int main()
{
float a, b, c, d, e, sum=0.0, avg=0.0;

float sd=0.0;

printf("Enter Five Numbers:");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);

calc (a, b, c, d, e, &sum, &avg, &sd);


printf("\nSum=%f", sum);
printf("\nAverage=%f", avg);

printf("\nStandard Deviation=%f\n", sd);


getchar();

return 0;
}

calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)

{
float Calc=0.0;

*sum = a+b+c+d+e;

*avg = *sum / 5.0;

Calc += ( a - *avg) * ( a - *avg);

Calc += ( b - *avg) * ( b - *avg);

Calc += ( c - *avg) * ( c - *avg);
Calc += ( d - *avg) * ( d - *avg);

Calc += ( e - *avg) * ( e - *avg);


*sd = sqrt((double)Calc/5.0);

}


Calculation of Product of Two Numbers using Function - Returns a Float
This program seems to be rather simple. But there's one little thing to be noted in this particular program. The thing is that the function in this program returns a float. The function declaration is usually given outside main..but due to some other standards that I am following, I have prototyed it inside main..but that doesn't cause much of a difference in this simple program.

Write a function which receives a float and an int from main(), finds the product
of these two and returns the product which is printed through main().

#include
main()
{

int i;
float j, prod;
float product (int x, float y);

printf("Enter the i(int) and j(float):");
scanf ("%d %f", &i, &j);

prod = product(i,j);

printf("Product:%f", prod);

}

product (int x, float y)
{

float product;
product = x*y;
return (product);

}




Calculation of Area and Circumference of a Circle using Pointers
The following program is one good example that illustrates how we can return more than one value in a function. The answer is certainly using Pointers. The following program demonstrates the method.

Write a function that calculates both Area and Perimeter/ Circumference of the Circle, whose Radius is
entered through the keyboard.


#include
main()
{

int radius;
float area, perimeter;

printf("\nEnter radius of a circle:");

scanf ("%d", &radius);
areaperi (radius, &area, &perimeter);

printf("Area=%f", area);
printf("\nPerimeter=%f", perimeter);

}


areaperi(int r, float *a, float *p)

{
*a=3.14*r*r;
*p=2*3.14*r;

}



//This Program exhibits the use of Call By Reference.

Monday, July 7, 2008

Access a Class Member Function Without Creating a Class Object

--------------------------------------------------------------------------------

In some cases, it is possible to call a class member function without creating the class object.
In the following example, the program will print "hello world" although class A has never been created. When the program enters the "PrintMe" function, the "this" pointer is zero. This is fine as long as you don't access data members through the "this" pointer.


Code: CPP
#include
class A {
public:
void PrintMe();
};


void A::PrintMe()
{
printf("Hello World\n");
}

void main()
{
A* p = 0;
p->PrintMe();

}
Negative Numbers Represented in C++

--------------------------------------------------------------------------------

You probably know that integers are represented in binary--in base 2. This is pretty straightforward for positive numbers, but it means you must choose an encoding for representing negatives. The encoding used by C++ (as well was by C and Java) is two's complement.
In two's complement, the first bit of a negative number is always 1. Otherwise, the number is 0 or postive. To find the bitstring representing a negative number, you take the bitstring representing the corresponding positive number and flip all the bits. Next, add 1 to the result.

In the following example, Ive used 4-bit numbers for simplicity:


-5d = -(0101b) = (1010b + 1b) = 1011b

Notice that -1d is represented as all 1's:

-1d = -(0001b) = 1110b + 1 = 1111b

A nice property of this encoding is that you can subtract by negating and then adding the following:

7d - 3d = 0111b
- 0011b

=> 0111b
+ 1100b + 1

=> 0111b
+ 1101b
= 0100b = 4d

Yet another nice property is that overflows and underflows wrap around and cancel one another out, like this:

5d + 6d = 0101b
+ 0110b
= 1011b
= -(0100b + 1)
= -0101b = -5d

If you subtract 6 from this result (by adding its negation), youll get 5 back. First, compute -6:

-6d = -(0110b) = 1001b + 1 = 1010b

Then, add -6d to -5d to get the original value:

1011b
+ 1010b
= 0101

Matthew Johnson