C/Data Type/Array Double
Summing 100 data values
#include <stdio.h>
int main()
{
double data[100];
double sum = 0.0;
int i = 0;
double sign = 1.0;
printf("\nYou entered the values:\n");
for(i = 1 ; i<=100 ; i++){
data[i-1] = 1.0/(2*i*(2*i+1)*(2*i+2));
}
for(i = 0 ; i<100 ; i++)
{
sum += sign*data[i];
sign = -sign;
}
printf("\nResult is %.4lf\n", 4.0*sum+3);
}
Summing reciprocals of five values
#include <stdio.h>
void main()
{
double data[5];
double sum = 0.0;
int i = 0;
printf("Enter five values separated by spaces:\n");
for(i = 0 ; i<5 ; i++)
scanf("%lf", &data[i]);
printf("\nYou entered the values:\n");
for(i = 0 ; i<5 ; i++)
printf("%10.2lf", data[i]);
printf("\n");
for(i = 0 ; i<5 ; i++)
{
sum += 1.0/data[i];
if(i>0)
printf(" + ");
printf("1/%.2lf", data[i]);
}
printf(" = %lf\n", sum);
}
using a 2-dimensional array of doubles
#include <stdio.h>
#include <math.h>
#define iBASES 6
#define iEXPONENTS 3
#define iBASE 0
#define iRAISED_TO 1
#define iRESULT 2
int main( )
{
double dpowers[iBASES][iEXPONENTS]={
1.1, 1, 0,
2.2, 2, 0,
3.3, 3, 0,
4.4, 4, 0,
5.5, 5, 0,
6.6, 6, 0
};
int irow_index;
for(irow_index=0; irow_index < iBASES; irow_index++)
dpowers[irow_index][iRESULT] =
pow(dpowers[irow_index][iBASE],
dpowers[irow_index][iRAISED_TO]);
for(irow_index=0; irow_index < iBASES; irow_index++) {
printf(" %d\n",(int)dpowers[irow_index][iRAISED_TO]);
printf(" %2.1f = %.2f\n\n",dpowers[irow_index][iBASE],
dpowers[irow_index][iRESULT]);
}
return(0);
}