SlideShare une entreprise Scribd logo
1  sur  7
Télécharger pour lire hors ligne
Please determine all the bugs that exist in this program. You must also correct syntax errors.
Here is the code in C program:
/*----------------------------------------------------
File: plotPoly
This program plots a polynomial function.
------------------------------------------------------*/
#include
#include "gng1106plplot.h"
#define N 50 // number of points for plotting
#define X_IX 0 // index to row of x values
#define FX_IX 1 // index to row of f(x) values
#define MAX_COEFF 11 // max number of coefficiencts, degree 10
double poly(double, double[], int );
int getCoefficients(double *, double *, double []);
void calculatePoints(int np, double points[][np], double, double,
int, double []);
void plot(int n, double[][n]);
double getMin(double *, int );
double getMax(double *, int );
/*-----------------------------------------------------
Function: main
Description: Call getCoefficients to fill in the array of
coefficients and get range for plotting the
polynomial. Calls calculate points to fill
in the 2D array with polynomial points. Then
calls plot to plot the polynomial.
-------------------------------------------------------*/
int main()
{
//Variables declarations
double coefficients[MAX_COEFF]; // Largest degree is MAX_COEEF-1
double points[2][N]; // for storing fx/x values
double start, end; // Start and end x values
double n; // number of coefficients given by user
// Get User input
n = getCoefficients(start, end, coefficients);
// Compute the points
calculatePoints(N, points, start, end, n, coefficients);
// Plot
plot(N, points);
return 0;
}
/*----------------------------------------------------------
Function: getCoefficients
Parameters:
sPtr - pointer for storing start value
ePtr - pointer for storing end value
coeff - reference to array of coefficients
Returns: number of coefficients given by user
Description: Requests from the user the coeffients for
defining the polynomial (the number
must be less than MAX_COEFF) and the start/end
values for plotting the polynomial.
-----------------------------------------------------------*/
int getCoefficients(double *sPtr, double *ePtr, double coeff[])
{
int n; // coefficient number
char answer;
// First lets prompt for the coeffients
printf("Please provide up to 11 coefficients (polynomial of degre 10)n");
n = 0;
do
{
printf("Coefficient c[%d]: ",n);
scanf("%lf",&coeff[n]);
n = n + 1;
printf("Do you wish to add another coefficient (y/n)? ");
fflush(stdin);
scanf("%c", &answer);
} while(answer != 'y' && n <= MAX_COEFF);
// Get range
do
{
printf("Give value for start: ");
scanf("%lf",sPtr);
printf("Give value for end: ");
scanf("%lf",ePtr);
if(*ePtr <= *sPtr)
printf("End value must be greater than start valuen");
} while(*ePtr <= *sPtr);
return(n); // returns number of coefficients given by user
}
/*----------------------------------------------------------
Function: calculatePoints
Parameters:
np - number of points to compute (store in points 2D array)
points - reference to 2D array for storing the points
start/end - start and end range for computing points
nc - number of coefficents in the coeffient array
coeff - reference to array of coefficients
Description: Fills in the points array with np points with
x varying between start and end. Parameters
nc/coeff defines the polynomial function for
calculating f(x) using calls to poly.
-----------------------------------------------------------*/
void calculatePoints(int np, double points[][np], double start, double end,
int nc, double coeff[])
{
double inc; // for incrementing x
int ix; // for indexing into 2D colunms
// Calculate function points
inc = (end-start)/(np-1);
// Comput the first point
points[X_IX][0] = start;
points[FX_IX][0] = poly(points[X_IX][0],coeff,nc);
// Compute all other points
for(ix = 1; ix < N; ix = ix + 1)
{
points[X_IX][ix] = points[X_IX][ix -1] + inc;
points[FX_IX][ix] = poly(points[X_IX][ix],coeff,nc);
}
}
/*----------------------------------------------------------
Function: poly
Parameters:
x - x value of polynomial
c - reference to array of coefficients
n - number of coefficients
Returns: value y of polynomial
Description: Calculates the value of the polynomial, f(x), for
the given value of x. The polynomial is:
y = c[0] x^n-1 + c[1] x^n-2 + ... c[n-2] x + c[n-1
The numerical method presented in Lab 7 is applied.
-----------------------------------------------------------*/
double poly(double x, double c[], int n)
{
double fx;
int coeffIx;
fx = c[0];
for(coeffIx = 0; coeffIx < n; coeffIx = coeffIx+1)
fx = fx*x + c[coeffIx];
return(fx);
}
/*-------------------------------------------------
Function: plot()
Parameters:
n: number of points in the 2D array
points: pointer 2D array that contains points.
Description: Initialises the plot. The following values
in the referenced structure are used to setup
the plot:
points[X_IX][0], points[X_IX][n-1] - assume that x values are sequential
minfx, maxfx - vertical axis range (add 10% to min/max value)
Sets up white background and black forground
colors.
Then plots the curve accessed using xPtr and yPtr.
-------------------------------------------------*/
void plot(int n, double points[][n])
{
double minfx, maxfx;
double range; // range of vertical axix
// Setup plot configuration
plsdev("wingcc"); // Sets device to wingcc - CodeBlocks compiler
// Initialise the plot
plinit();
// Configure the axis and labels
plwidth(3); // select the width of the pen
// Find range for axis
minfx = getMin(points[FX_IX], n);
maxfx = getMax(points[FX_IX], n);
range = maxfx - minfx; // the width of the range
minfx = minfx + 0.1*range;
minfx = minfx - 0.1*range;
plenv(points[X_IX][0], points[X_IX][n-1], minfx, maxfx,
0, 1);
plcol0(GREEN); // Select color for labels
pllab('x', "f(x)", "Polynomial Function");
// Plot the velocity.
plcol0(BLUE); // Color for plotting curve
plline(n, points[X_IX], points[X_IX]);
plend();
}
/*----------------------------------------------------------
Function: getMin
Parameters:
array - reference to an array with double values
n - number of elements in the array
Returns
min: the minimum value found in the array
Description: Traverses the array to find its minimum value.
----------------------------------------------------------------*/
double getMin(double *array, int n)
{
int ix;
double min = array[0];
for(ix = 1; ix < n; ix = ix +1)
if(min > array[ix]) min = array[ix];
return(min);
}
/*----------------------------------------------------------
Function: getMax
Parameters:
array - reference to an array with double values
n - number of elements in the array
Returns
max: the maximum value found in the array
Description: Traverses the array to find its maximum value.
----------------------------------------------------------------*/
double getMax(double *array, int n)
{
int ix;
double max = array[0];
for(ix = 1; ix < n; ix = ix +1)
if(max < array[ix]) max = array[ix];
return(max);
} A. Exercise: Debugging a program that plots polynomial functions Statement of the problem
Develop a program plots a polynomial function f(x) between a given range of values for x. The
user provides the coefficients and the range of values. Background A polynomial can be
represented by an array of coefficients (e.g. {c0,c1,c2,.cN} ). The general form of the polynomial
of order N is: f(x)=c0xN+c1xN1+c2xN2++cN1x+cNEquation1 It is possible to evaluate the
value of f(x) using the "pow" math function but this is not the most efficient approach. The
following is a quote from Numerical Recipes 1. We assume that you know enough never to
evaluate a polynomial this way: p=c[0]xxxx+c[1]xxx+c[2]xx+c[3]x+c[4]; or (event worse!)
p=c[0)pow(x,4)+c[1] pow (x,3)+c[2] pow (x,2)+c[3]x+c[4]; Come the (computer) revolution, all
persons found guilty of such criminal behavior will be summarily executed, and their programs
won't be! To achieve a more efficient numerical approach to evaluating a polynomial, Equation 1
can be rearranged as following by factoring out x :
f(x)=(((((c0x+c1)x+c2)x+)x+cN2)x+cN1)x+cNEquation2 Equation 2 can then easily be
transformed into a simple loop for evaluation a polynomial. First fx (variable for computing f(x)
is initialized to the first coefficient c0, and loop iterates over all coefficients c1 to cx updating
fx=fxx+ci. The loop will calculate the expressions starting with the inner parentheses towards the
exterior to encompass the complete equation. Design (summary) The program provided (project
PlotPely in the zip file PlotPoly.zip) includes the following functions: - main: The main function
calls getcoefficients to obtain the values of the polynomial coefficients and the start/end values
for plotting. An array is declared and passed to the function to be filled in with the coefficients.
A 2D array is declared for storing points to be plotted. The function calculatepoints is called to
fill in the array. Finally plot is called to plot the polynomial. - getcoefficients: Obtains from the
user the coefficients and the start/end values. Pointers are used to store the values read from the
user (to save into the calling function array/variables). The function ensures that no more than 11
coefficients (maximum degree of the polynomial is
10) are read and that the start value is less than the end value. The number of coefficients
obtained from the user is returned by the function. - calculatepoints: This function computes the
points to fill in the 2D array between the start/end values for x and using a polynomial defined by
an array of coefficients. - poly: This function computes the value of any polynomial for a given
value of x. The polynomial is represented using a coefficients array as described in the
background section above. - plot: Plots the polynomial function from start to end range for x
using the point values provided by 2D array. - getmin: Gets the minimum value of a double array
(can be used to help find the maximum value for the vertical axis. - getMax: Gets the maximum
value of a double array (can be used to help find the minimum value for the vertical axis. Test
Case The program should produce a plot similar to the following produced using Excel for the
function shown. Exercise: Like with past labs, this first exercise is a debugging exercise. You
must determine all the bugs that exist in this program. You must also correct syntax errors.
Report your corrections to your TA for your marks.

Contenu connexe

Similaire à Please determine all the bugs that exist in this program. You must a.pdf

eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college2017eee0459
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
Python 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptxPython 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptxTseChris
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfjyothimuppasani1
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxAaliyanShaikh
 
C standard library functions
C standard library functionsC standard library functions
C standard library functionsVaishnavee Sharma
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersSheila Sinclair
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)hhliu
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
In C Programming- Create a program that converts a number from decimal.docx
In C Programming- Create a program that converts a number from decimal.docxIn C Programming- Create a program that converts a number from decimal.docx
In C Programming- Create a program that converts a number from decimal.docxmckerliejonelle
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxgilpinleeanna
 

Similaire à Please determine all the bugs that exist in this program. You must a.pdf (20)

eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Functions
FunctionsFunctions
Functions
 
Python Manuel-R2021.pdf
Python Manuel-R2021.pdfPython Manuel-R2021.pdf
Python Manuel-R2021.pdf
 
Array Cont
Array ContArray Cont
Array Cont
 
Python 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptxPython 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptx
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
 
4. functions
4. functions4. functions
4. functions
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Function in c program
Function in c programFunction in c program
Function in c program
 
In C Programming- Create a program that converts a number from decimal.docx
In C Programming- Create a program that converts a number from decimal.docxIn C Programming- Create a program that converts a number from decimal.docx
In C Programming- Create a program that converts a number from decimal.docx
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
 

Plus de amarnathmahajansport

Please ask experts to solve this in the correct way. 1. (20 point.pdf
Please ask experts to solve this in the correct way.  1. (20 point.pdfPlease ask experts to solve this in the correct way.  1. (20 point.pdf
Please ask experts to solve this in the correct way. 1. (20 point.pdfamarnathmahajansport
 
please answer this multiple choice question2.What major Department.pdf
please answer this multiple choice question2.What major Department.pdfplease answer this multiple choice question2.What major Department.pdf
please answer this multiple choice question2.What major Department.pdfamarnathmahajansport
 
please answer this multiple choice question4.This Technical Review.pdf
please answer this multiple choice question4.This Technical Review.pdfplease answer this multiple choice question4.This Technical Review.pdf
please answer this multiple choice question4.This Technical Review.pdfamarnathmahajansport
 
Please answer question in full 2.1 Security management function.pdf
Please answer question in full  2.1 Security management function.pdfPlease answer question in full  2.1 Security management function.pdf
Please answer question in full 2.1 Security management function.pdfamarnathmahajansport
 
Please answer these questions 150 words only 1) List four lines o.pdf
Please answer these questions 150 words only  1) List four lines o.pdfPlease answer these questions 150 words only  1) List four lines o.pdf
Please answer these questions 150 words only 1) List four lines o.pdfamarnathmahajansport
 
Please answer the 4 questions at the end please do all of them at th.pdf
Please answer the 4 questions at the end please do all of them at th.pdfPlease answer the 4 questions at the end please do all of them at th.pdf
Please answer the 4 questions at the end please do all of them at th.pdfamarnathmahajansport
 
Please answer the questions in orderExplain why a virus is co.pdf
Please answer the questions in orderExplain why a virus is co.pdfPlease answer the questions in orderExplain why a virus is co.pdf
Please answer the questions in orderExplain why a virus is co.pdfamarnathmahajansport
 
Please answer the questions in orderCompare and contrast Virus a.pdf
Please answer the questions in orderCompare and contrast Virus a.pdfPlease answer the questions in orderCompare and contrast Virus a.pdf
Please answer the questions in orderCompare and contrast Virus a.pdfamarnathmahajansport
 
Please answer the questions in ordarExplain the advantage for sur.pdf
Please answer the questions in ordarExplain the advantage for sur.pdfPlease answer the questions in ordarExplain the advantage for sur.pdf
Please answer the questions in ordarExplain the advantage for sur.pdfamarnathmahajansport
 
Please give a detailed summary of the article where I can write at l.pdf
Please give a detailed summary of the article where I can write at l.pdfPlease give a detailed summary of the article where I can write at l.pdf
Please give a detailed summary of the article where I can write at l.pdfamarnathmahajansport
 
Please answer the 4 questions at the end Causes and Outcomes of Co.pdf
Please answer the 4 questions at the end Causes and Outcomes of Co.pdfPlease answer the 4 questions at the end Causes and Outcomes of Co.pdf
Please answer the 4 questions at the end Causes and Outcomes of Co.pdfamarnathmahajansport
 
please help In multiple regression analysis, residuals (YY^) shou.pdf
please help  In multiple regression analysis, residuals (YY^) shou.pdfplease help  In multiple regression analysis, residuals (YY^) shou.pdf
please help In multiple regression analysis, residuals (YY^) shou.pdfamarnathmahajansport
 
please fill in the chart A company that uses job order c.pdf
please fill in the chart A company that uses job order c.pdfplease fill in the chart A company that uses job order c.pdf
please fill in the chart A company that uses job order c.pdfamarnathmahajansport
 
Please fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfPlease fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfamarnathmahajansport
 
Please explain fully each step and what inputs and the number of row.pdf
Please explain fully each step and what inputs and the number of row.pdfPlease explain fully each step and what inputs and the number of row.pdf
Please explain fully each step and what inputs and the number of row.pdfamarnathmahajansport
 
please explain how attaker crack 2DES and show your work. 3DES = Run.pdf
please explain how attaker crack 2DES and show your work. 3DES = Run.pdfplease explain how attaker crack 2DES and show your work. 3DES = Run.pdf
please explain how attaker crack 2DES and show your work. 3DES = Run.pdfamarnathmahajansport
 
Please explain answer choice 26. Small functionally disruptive hardw.pdf
Please explain answer choice 26. Small functionally disruptive hardw.pdfPlease explain answer choice 26. Small functionally disruptive hardw.pdf
Please explain answer choice 26. Small functionally disruptive hardw.pdfamarnathmahajansport
 
Please explain answer choice 15. Match each of the following counter.pdf
Please explain answer choice 15. Match each of the following counter.pdfPlease explain answer choice 15. Match each of the following counter.pdf
Please explain answer choice 15. Match each of the following counter.pdfamarnathmahajansport
 
Please draw an activity diagram using Enterprise. or draw using symb.pdf
Please draw an activity diagram using Enterprise. or draw using symb.pdfPlease draw an activity diagram using Enterprise. or draw using symb.pdf
Please draw an activity diagram using Enterprise. or draw using symb.pdfamarnathmahajansport
 

Plus de amarnathmahajansport (20)

Please ask experts to solve this in the correct way. 1. (20 point.pdf
Please ask experts to solve this in the correct way.  1. (20 point.pdfPlease ask experts to solve this in the correct way.  1. (20 point.pdf
Please ask experts to solve this in the correct way. 1. (20 point.pdf
 
please answer this multiple choice question2.What major Department.pdf
please answer this multiple choice question2.What major Department.pdfplease answer this multiple choice question2.What major Department.pdf
please answer this multiple choice question2.What major Department.pdf
 
please answer this multiple choice question4.This Technical Review.pdf
please answer this multiple choice question4.This Technical Review.pdfplease answer this multiple choice question4.This Technical Review.pdf
please answer this multiple choice question4.This Technical Review.pdf
 
Please answer question in full 2.1 Security management function.pdf
Please answer question in full  2.1 Security management function.pdfPlease answer question in full  2.1 Security management function.pdf
Please answer question in full 2.1 Security management function.pdf
 
Please answer these questions 150 words only 1) List four lines o.pdf
Please answer these questions 150 words only  1) List four lines o.pdfPlease answer these questions 150 words only  1) List four lines o.pdf
Please answer these questions 150 words only 1) List four lines o.pdf
 
Please answer the 4 questions at the end please do all of them at th.pdf
Please answer the 4 questions at the end please do all of them at th.pdfPlease answer the 4 questions at the end please do all of them at th.pdf
Please answer the 4 questions at the end please do all of them at th.pdf
 
Please answer the questions in orderExplain why a virus is co.pdf
Please answer the questions in orderExplain why a virus is co.pdfPlease answer the questions in orderExplain why a virus is co.pdf
Please answer the questions in orderExplain why a virus is co.pdf
 
Please answer the questions in orderCompare and contrast Virus a.pdf
Please answer the questions in orderCompare and contrast Virus a.pdfPlease answer the questions in orderCompare and contrast Virus a.pdf
Please answer the questions in orderCompare and contrast Virus a.pdf
 
Please answer the questions in ordarExplain the advantage for sur.pdf
Please answer the questions in ordarExplain the advantage for sur.pdfPlease answer the questions in ordarExplain the advantage for sur.pdf
Please answer the questions in ordarExplain the advantage for sur.pdf
 
Please give a detailed summary of the article where I can write at l.pdf
Please give a detailed summary of the article where I can write at l.pdfPlease give a detailed summary of the article where I can write at l.pdf
Please give a detailed summary of the article where I can write at l.pdf
 
Please answer the 4 questions at the end Causes and Outcomes of Co.pdf
Please answer the 4 questions at the end Causes and Outcomes of Co.pdfPlease answer the 4 questions at the end Causes and Outcomes of Co.pdf
Please answer the 4 questions at the end Causes and Outcomes of Co.pdf
 
please help In multiple regression analysis, residuals (YY^) shou.pdf
please help  In multiple regression analysis, residuals (YY^) shou.pdfplease help  In multiple regression analysis, residuals (YY^) shou.pdf
please help In multiple regression analysis, residuals (YY^) shou.pdf
 
please fill in the chart A company that uses job order c.pdf
please fill in the chart A company that uses job order c.pdfplease fill in the chart A company that uses job order c.pdf
please fill in the chart A company that uses job order c.pdf
 
Please fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfPlease fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdf
 
Please explain fully each step and what inputs and the number of row.pdf
Please explain fully each step and what inputs and the number of row.pdfPlease explain fully each step and what inputs and the number of row.pdf
Please explain fully each step and what inputs and the number of row.pdf
 
please explain how attaker crack 2DES and show your work. 3DES = Run.pdf
please explain how attaker crack 2DES and show your work. 3DES = Run.pdfplease explain how attaker crack 2DES and show your work. 3DES = Run.pdf
please explain how attaker crack 2DES and show your work. 3DES = Run.pdf
 
Please explain answer choice 26. Small functionally disruptive hardw.pdf
Please explain answer choice 26. Small functionally disruptive hardw.pdfPlease explain answer choice 26. Small functionally disruptive hardw.pdf
Please explain answer choice 26. Small functionally disruptive hardw.pdf
 
Please explain answer choice 15. Match each of the following counter.pdf
Please explain answer choice 15. Match each of the following counter.pdfPlease explain answer choice 15. Match each of the following counter.pdf
Please explain answer choice 15. Match each of the following counter.pdf
 
Please draw an activity diagram using Enterprise. or draw using symb.pdf
Please draw an activity diagram using Enterprise. or draw using symb.pdfPlease draw an activity diagram using Enterprise. or draw using symb.pdf
Please draw an activity diagram using Enterprise. or draw using symb.pdf
 
please do the extr.pdf
please do the extr.pdfplease do the extr.pdf
please do the extr.pdf
 

Dernier

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Dernier (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Please determine all the bugs that exist in this program. You must a.pdf

  • 1. Please determine all the bugs that exist in this program. You must also correct syntax errors. Here is the code in C program: /*---------------------------------------------------- File: plotPoly This program plots a polynomial function. ------------------------------------------------------*/ #include #include "gng1106plplot.h" #define N 50 // number of points for plotting #define X_IX 0 // index to row of x values #define FX_IX 1 // index to row of f(x) values #define MAX_COEFF 11 // max number of coefficiencts, degree 10 double poly(double, double[], int ); int getCoefficients(double *, double *, double []); void calculatePoints(int np, double points[][np], double, double, int, double []); void plot(int n, double[][n]); double getMin(double *, int ); double getMax(double *, int ); /*----------------------------------------------------- Function: main Description: Call getCoefficients to fill in the array of coefficients and get range for plotting the polynomial. Calls calculate points to fill in the 2D array with polynomial points. Then calls plot to plot the polynomial. -------------------------------------------------------*/ int main() { //Variables declarations double coefficients[MAX_COEFF]; // Largest degree is MAX_COEEF-1 double points[2][N]; // for storing fx/x values double start, end; // Start and end x values double n; // number of coefficients given by user // Get User input
  • 2. n = getCoefficients(start, end, coefficients); // Compute the points calculatePoints(N, points, start, end, n, coefficients); // Plot plot(N, points); return 0; } /*---------------------------------------------------------- Function: getCoefficients Parameters: sPtr - pointer for storing start value ePtr - pointer for storing end value coeff - reference to array of coefficients Returns: number of coefficients given by user Description: Requests from the user the coeffients for defining the polynomial (the number must be less than MAX_COEFF) and the start/end values for plotting the polynomial. -----------------------------------------------------------*/ int getCoefficients(double *sPtr, double *ePtr, double coeff[]) { int n; // coefficient number char answer; // First lets prompt for the coeffients printf("Please provide up to 11 coefficients (polynomial of degre 10)n"); n = 0; do { printf("Coefficient c[%d]: ",n); scanf("%lf",&coeff[n]); n = n + 1; printf("Do you wish to add another coefficient (y/n)? "); fflush(stdin); scanf("%c", &answer); } while(answer != 'y' && n <= MAX_COEFF); // Get range
  • 3. do { printf("Give value for start: "); scanf("%lf",sPtr); printf("Give value for end: "); scanf("%lf",ePtr); if(*ePtr <= *sPtr) printf("End value must be greater than start valuen"); } while(*ePtr <= *sPtr); return(n); // returns number of coefficients given by user } /*---------------------------------------------------------- Function: calculatePoints Parameters: np - number of points to compute (store in points 2D array) points - reference to 2D array for storing the points start/end - start and end range for computing points nc - number of coefficents in the coeffient array coeff - reference to array of coefficients Description: Fills in the points array with np points with x varying between start and end. Parameters nc/coeff defines the polynomial function for calculating f(x) using calls to poly. -----------------------------------------------------------*/ void calculatePoints(int np, double points[][np], double start, double end, int nc, double coeff[]) { double inc; // for incrementing x int ix; // for indexing into 2D colunms // Calculate function points inc = (end-start)/(np-1); // Comput the first point points[X_IX][0] = start; points[FX_IX][0] = poly(points[X_IX][0],coeff,nc); // Compute all other points for(ix = 1; ix < N; ix = ix + 1)
  • 4. { points[X_IX][ix] = points[X_IX][ix -1] + inc; points[FX_IX][ix] = poly(points[X_IX][ix],coeff,nc); } } /*---------------------------------------------------------- Function: poly Parameters: x - x value of polynomial c - reference to array of coefficients n - number of coefficients Returns: value y of polynomial Description: Calculates the value of the polynomial, f(x), for the given value of x. The polynomial is: y = c[0] x^n-1 + c[1] x^n-2 + ... c[n-2] x + c[n-1 The numerical method presented in Lab 7 is applied. -----------------------------------------------------------*/ double poly(double x, double c[], int n) { double fx; int coeffIx; fx = c[0]; for(coeffIx = 0; coeffIx < n; coeffIx = coeffIx+1) fx = fx*x + c[coeffIx]; return(fx); } /*------------------------------------------------- Function: plot() Parameters: n: number of points in the 2D array points: pointer 2D array that contains points. Description: Initialises the plot. The following values in the referenced structure are used to setup the plot: points[X_IX][0], points[X_IX][n-1] - assume that x values are sequential minfx, maxfx - vertical axis range (add 10% to min/max value)
  • 5. Sets up white background and black forground colors. Then plots the curve accessed using xPtr and yPtr. -------------------------------------------------*/ void plot(int n, double points[][n]) { double minfx, maxfx; double range; // range of vertical axix // Setup plot configuration plsdev("wingcc"); // Sets device to wingcc - CodeBlocks compiler // Initialise the plot plinit(); // Configure the axis and labels plwidth(3); // select the width of the pen // Find range for axis minfx = getMin(points[FX_IX], n); maxfx = getMax(points[FX_IX], n); range = maxfx - minfx; // the width of the range minfx = minfx + 0.1*range; minfx = minfx - 0.1*range; plenv(points[X_IX][0], points[X_IX][n-1], minfx, maxfx, 0, 1); plcol0(GREEN); // Select color for labels pllab('x', "f(x)", "Polynomial Function"); // Plot the velocity. plcol0(BLUE); // Color for plotting curve plline(n, points[X_IX], points[X_IX]); plend(); } /*---------------------------------------------------------- Function: getMin Parameters: array - reference to an array with double values n - number of elements in the array Returns
  • 6. min: the minimum value found in the array Description: Traverses the array to find its minimum value. ----------------------------------------------------------------*/ double getMin(double *array, int n) { int ix; double min = array[0]; for(ix = 1; ix < n; ix = ix +1) if(min > array[ix]) min = array[ix]; return(min); } /*---------------------------------------------------------- Function: getMax Parameters: array - reference to an array with double values n - number of elements in the array Returns max: the maximum value found in the array Description: Traverses the array to find its maximum value. ----------------------------------------------------------------*/ double getMax(double *array, int n) { int ix; double max = array[0]; for(ix = 1; ix < n; ix = ix +1) if(max < array[ix]) max = array[ix]; return(max); } A. Exercise: Debugging a program that plots polynomial functions Statement of the problem Develop a program plots a polynomial function f(x) between a given range of values for x. The user provides the coefficients and the range of values. Background A polynomial can be represented by an array of coefficients (e.g. {c0,c1,c2,.cN} ). The general form of the polynomial of order N is: f(x)=c0xN+c1xN1+c2xN2++cN1x+cNEquation1 It is possible to evaluate the value of f(x) using the "pow" math function but this is not the most efficient approach. The following is a quote from Numerical Recipes 1. We assume that you know enough never to evaluate a polynomial this way: p=c[0]xxxx+c[1]xxx+c[2]xx+c[3]x+c[4]; or (event worse!) p=c[0)pow(x,4)+c[1] pow (x,3)+c[2] pow (x,2)+c[3]x+c[4]; Come the (computer) revolution, all
  • 7. persons found guilty of such criminal behavior will be summarily executed, and their programs won't be! To achieve a more efficient numerical approach to evaluating a polynomial, Equation 1 can be rearranged as following by factoring out x : f(x)=(((((c0x+c1)x+c2)x+)x+cN2)x+cN1)x+cNEquation2 Equation 2 can then easily be transformed into a simple loop for evaluation a polynomial. First fx (variable for computing f(x) is initialized to the first coefficient c0, and loop iterates over all coefficients c1 to cx updating fx=fxx+ci. The loop will calculate the expressions starting with the inner parentheses towards the exterior to encompass the complete equation. Design (summary) The program provided (project PlotPely in the zip file PlotPoly.zip) includes the following functions: - main: The main function calls getcoefficients to obtain the values of the polynomial coefficients and the start/end values for plotting. An array is declared and passed to the function to be filled in with the coefficients. A 2D array is declared for storing points to be plotted. The function calculatepoints is called to fill in the array. Finally plot is called to plot the polynomial. - getcoefficients: Obtains from the user the coefficients and the start/end values. Pointers are used to store the values read from the user (to save into the calling function array/variables). The function ensures that no more than 11 coefficients (maximum degree of the polynomial is 10) are read and that the start value is less than the end value. The number of coefficients obtained from the user is returned by the function. - calculatepoints: This function computes the points to fill in the 2D array between the start/end values for x and using a polynomial defined by an array of coefficients. - poly: This function computes the value of any polynomial for a given value of x. The polynomial is represented using a coefficients array as described in the background section above. - plot: Plots the polynomial function from start to end range for x using the point values provided by 2D array. - getmin: Gets the minimum value of a double array (can be used to help find the maximum value for the vertical axis. - getMax: Gets the maximum value of a double array (can be used to help find the minimum value for the vertical axis. Test Case The program should produce a plot similar to the following produced using Excel for the function shown. Exercise: Like with past labs, this first exercise is a debugging exercise. You must determine all the bugs that exist in this program. You must also correct syntax errors. Report your corrections to your TA for your marks.