Instructions to write C code in Visual
Studio .NET 2003
Start à All Programs à Microsoft
Visual Studio .NET 2003
Program
Difference Engine in C:
1. You have learned difference engine in class.
In this part of the lab, you are required to implement the difference engine in
C. The skeleton of the program has been given in step 3.
2. We are using sine function here. You can
input DELTA value, which should be small, e.g. 0.01. x[0]
is set to 0 and x[i] = x[i-1] + Delta. Then we can compute y[0]=sin(x[0]),
y[1]=sin(x[1]), y[2]=sin(x[2]), and y[3]=sin(x[3]). After we get y[0], y[1],
y[2], and y[3], we can find the first column of the difference engine table,
which are Delta0y[0], Delta1y[0], Delta2y[0], and Delta3y[0]. Then, we can
compute the remaining of the table. The x, y, Delta0y, Delta1y, Delta2y, and
Delta3y arrays can be very large given enough memory. Therefore, we can look up
the difference engine table and get the result of y=sin(x).
3. Create a new Visual Studio .Net project for
the difference engine problem. Copy and paste the following skeleton into the
new project and complete the program. Output the first 10 columns of the table.
4. Use
your calculator to obtain the value of sin(x) for the x values from your table.
Verify that your difference engine has
in fact calculated the sin. Experiment by
using different values for delta and checking the accuracy of the difference
engine output compared to the actual value of sin. Show your results to TA.
// 282xprog2.cpp : Defines the entry
point for the console application.
//
// 282x_Prog2.cpp : Defines the entry
point for the console application.
//
#include "stdafx.h"
//Difference Engine Program
#include <iostream>
#include "math.h"
#define column 100
int
fact(int n)
{
if(n
< 1)
return (1);
else
return (n*fact(n-1));
}
main()
{
int i = 0;
//Enter the
value of DELTA
double
Delta;
printf("Please enter the Delta value: ");
scanf("%lf", &Delta);
printf("Delta = %1.6lf \n", Delta);
//Calculate
first several values: y0,y1,y2,y3,y4 given x0 and
Delta
//compute x[i]
double
x[column], y[column];
x[0] = 0.00;
x[1] = x[0] +
Delta;
x[2] = x[1] +
Delta;
x[3] = x[2] +
Delta;
x[4] = x[3] +
Delta;
//compute
sin(x[i])
y[0] = x[0] - pow(x[0],3)/fact(3)
+ pow(x[0],5)/fact(5);
y[1] = x[1] - pow(x[1],3)/fact(3)
+ pow(x[1],5)/fact(5);
y[2] = x[2] - pow(x[2],3)/fact(3)
+ pow(x[2],5)/fact(5);
y[3] = x[3] - pow(x[3],3)/fact(3)
+ pow(x[3],5)/fact(5);
//compute
Delta0y[i], Delta1y[i], Delta2y[i], Delta3y[i]
//to get the first column
double
Delta0y[column], Delta1y[column], Delta2y[column], Delta3y[column];
Delta0y[0] = y
[0];
Delta0y[1] = y
[1];
Delta0y[2] = y
[2];
Delta0y[3] = y
[3];
Delta1y[0] =
Delta0y[1]-Delta0y[0];
Delta1y[1] =
Delta0y[2]-Delta0y[1];
Delta1y[2] =
Delta0y[3]-Delta0y[2];
Delta2y[0] =
Delta1y[1]-Delta1y[0];
Delta2y[1] =
Delta1y[2]-Delta1y[1];
//all the
Delta3y[n] are the same
Delta3y[0] =
Delta2y[1]-Delta2y[0];
//compute the
first 10 columns
for
(i=1; i<11; i++)
{
/*********************/
//Write
your code here
}
//print out the columns
of Difference Machine Table
/*********************/
//Write your
code here
getchar();
getchar();
return(0);
}