|
|
Program 1: Table of value from a quadratic, using a for statement
#include <stdio.h>
void main ()
{
/* Initialize variables */
float a,b,c,start,stop,increment,x,y,numberofvalues;
int n=0;
/* Input/Output */
printf("The following program will ask for the coefficients of a quadratic and then make a table of values for the funciton.\n\n");
printf("Input the coefficient a in ax^2+bx+c ");
scanf("%f",&a);
printf("Input the coefficient b in ax^2+bx+c ");
scanf("%f",&b);
printf("Input the coefficient c in ax^2+bx+c ");
scanf("%f",&c);
printf("The x value to start the table. ");
scanf("%f",&start);
printf("The x value to stop the table. ");
scanf("%f",&stop);
printf("Number of values in the table. ");
scanf("%f",&numberofvalues);
/* Sets the delta x & starting point */
increment=((stop-start)/numberofvalues);
x=start-increment;
FILE * qFile;
qFile = fopen ("QuadaticFor.xls","w");
fprintf (qFile, “The following program makes a table of values for the function below.\n”);
fprintf (qFile, "%fx^2 + %fx + %f\n\n",a,b,c);
fprintf (qFile,"x\ty\n");
for (n=0;n<=numberofvalues;n++) {
x=x+increment;
y=a*x*x+b*x+c;
fprintf (qFile,"%f\t%f\n",x,y);
}
}
Program 2: Table of positive values of the dependent variable from a quadratic, using a while statement
#include <stdio.h>
void main()
{
int time=0;
float poly, ttime, a, b, increment;
printf ("Input the leading coefficient\t");
scanf ("%f", &a);
printf ("Input the initial condition\t");
scanf ("%f", &b);
printf ("Input the increment to count by\t");
scanf ("%f", &increment);
FILE *qFile;
qFile=fopen ("PolyOutput.xls","w");
fprintf (qFile,"Polynomial Model\n\nTime(s)\tValue\n");
while (poly>=0)
{
ttime=increment*time;
poly=a*ttime*ttime+b;
if (poly>0) fprintf (qFile,"%.2f\t%.2f\n",ttime,poly);
time++;
}
}