Monday, October 27, 2014

Calculate the Mean, Variance & Standard Deviation

Calculate the Mean, Variance & Standard Deviation
#include 
#include 
#include 
#define maxsize 10
 
void main()
{
    float x[maxsize];
    int  i, n;
    float average, variance, std_deviation, sum = 0, sum1 = 0;
    clrscr();
    printf("Enter the value of N \n");
    scanf("%d", &n);
    printf("Enter %d real numbers \n", n);
    for (i = 0; i < n; i++)
    {
        scanf("%f", &x[i]);
    }
    for (i = 0; i < n; i++)
    {
        sum = sum + x[i];
    }
    average = sum / (float)n;
    
    for (i = 0; i < n; i++)
    {
        sum1 = sum1 + pow((x[i] - average), 2);
    }
    variance = sum1 / (float)n;
    std_deviation = sqrt(variance);
    printf("Average of all elements = %.2f\n", average);
    printf("variance of all elements = %.2f\n", variance);
    printf("Standard deviation = %.2f\n", std_deviation);
    getch();
}

Output
 
Enter the value of N
5
Enter 5 real numbers
34
88
32
12
10
Average of all elements = 35.20
variance of all elements = 794.56
Standard deviation = 28.19



Evaluate the given Polynomial Equation
#include 
#include 
#include 
#define maxsize 10
 
void main()
{
    int array[maxsize];
    int i, num, power;
    float x, polySum;
    clrscr();
    printf("Enter the order of the polynomial \n");
    scanf("%d", &num);
    printf("Enter the value of x \n");
    scanf("%f", &x);
    printf("Enter %d coefficients \n", num + 1);
    for (i = 0; i <= num; i++)
    {
        scanf("%d", &array[i]);
    }
    polySum = array[0];
    for (i = 1; i <= num; i++)
    {
        polySum = polySum * x + array[i];
    }
    power = num;
 
    printf("Given polynomial is: \n");
    for (i = 0; i <= num; i++)
    {
        if (power < 0)
        {
            break;
        }
        if (array[i] > 0)
            printf(" + ");
        else if (array[i] < 0)
            printf(" - ");
        else
            printf(" ");
        printf("%dx^%d  ", abs(array[i]), power--);
    }
    printf("\n Sum of the polynomial = %6.2f \n", polySum);
    getch();
}
 
Output
 
Enter the order of the polynomial
2
Enter the value of x
2
Enter 3 coefficients
3
2
6
Given polynomial is:
+ 3x^2   + 2x^1   + 6x^0
Sum of the polynomial =  22.00
 
Enter the order of the polynomial
4
Enter the value of x
1
Enter 5 coefficients
3
-5
6
8
-9
Given polynomial is:
+ 3x^4   - 5x^3   + 6x^2   + 8x^1   - 9x^0
Sum of the polynomial =   3.00
Generate Fibonacci Series
#include 
#include 
void main()
{
    int  fib1 = 0, fib2 = 1, fib3, limit, count = 0;
    clrscr();
    printf("Enter the limit to generate the Fibonacci Series \n");
    scanf("%d", &limit);
    printf("Fibonacci Series is ...\n");
    printf("%d\n", fib1);
    printf("%d\n", fib2);
    count = 2;
    while (count < limit)
    {
        fib3 = fib1 + fib2;
        count++;
        printf("%d\n", fib3);
        fib1 = fib2;
        fib2 = fib3;
    } 
    getch();
}

 
 
Output
 
Enter the limit to generate the Fibonacci Series
6
Fibonacci Series is ...
0
1
1
2
3
5
Compute the Surface Area & Volume of a Cube
#include 
#include 
#include 
 void main()
{
    float side, surfacearea, volume;
    clrscr();
    printf("Enter the length of a side \n");
    scanf("%f", &side);
    surfacearea = 6.0 * side * side;
    volume = pow(side, 3);
    printf("Surface area = %6.2f and Volume = %6.2f \n", surfacearea, volume);
    getch();
}
 
Output
 
Enter the length of a side
34
Surface area = 6936.00 and Volume = 39304.00
Read a Coordinate Point in a XY Coordinate System and Determine its Quadrant
#include 
#include 
void main()
{
    int x, y;
    clrscr();
    printf("Enter the values for X and Y\n");
    scanf("%d %d", &x, &y);
    if (x > 0 && y > 0)
        printf("point (%d, %d) lies in the First quandrant\n");
    else if (x < 0 && y > 0)
        printf("point (%d, %d) lies in the Second quandrant\n");
    else if (x < 0 && y < 0)
        printf("point (%d, %d) lies in the Third quandrant\n");
    else if (x > 0 && y < 0)
        printf("point (%d, %d) lies in the Fourth quandrant\n");
    else if (x == 0 && y == 0)
        printf("point (%d, %d) lies at the origin\n");
    getch();
}

Output
 
Enter the values for X and Y
20 30
point (-1079549476, -1079549480) lies in the First quandrant
 
Enter the values for X and Y
-30 -60
point (-1080802740, -1080802744) lies in the Third quandrant
 
Enter the values for X and Y
300 -8
point (-1078902004, -1078902008) lies in the Fourth quandrant
 
Enter the values for X and Y
-180 180

point (-1076456724, -1076456728) lies in the Second quandrant

No comments:

Post a Comment