I have to create a program in c using this formula,

y=ax(cubed)+bx(squared)+cx+d,

I have to ask the user to read in the four coefficients of the cubic,
Ask the user for start and finish values for x,
ask the user to view the graph and Plot a graph using grx20 using the start and finish values of x given by the user
and draw the y and x axis in an appropriate placefor any value of x.

I don't know much about c programming at all, as I have only been doing it for about five hours, any help would be much appreciated

Dani AI

Generated

wanted a simple C program to let the user enter a cubic and an x range and plot it with grx20. correctly suggested showing any attempt, and noted this is a typical beginner task. A clear, practical approach: read the four coefficients and the start/finish x, swap if needed, sample the polynomial across the range (use at least one sample per horizontal pixel), find the y min/max from the samples, map math coordinates to pixel coordinates, then draw lines between consecutive points. Evaluate the cubic with Horner's method for speed and numerical stability.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    double a,b,c,d, x0,x1;
    if (scanf("%lf %lf %lf %lf", &a,&b,&c,&d) != 4) return 1;
    if (scanf("%lf %lf", &x0,&x1) != 2) return 1;
    if (x0 == x1) x1 = x0 + 1.0;
    if (x0 > x1) { double t=x0; x0=x1; x1=t; }

    const int W = 640, H = 480;
    int samples = W;
    double step = (x1 - x0) / (samples - 1);
    double *ys = malloc(samples * sizeof(double));
    double ymin = 1e300, ymax = -1e300;

    for (int i = 0; i < samples; ++i) {
        double x = x0 + i * step;
        double y = ((a * x + b) * x + c) * x + d; /* Horner */
        ys[i] = y;
        if (y < ymin) ymin = y;
        if (y > ymax) ymax = y;
    }

    if (ymin == ymax) { ymin -= 1; ymax += 1; }
    /* px = (int)((x - x0)/(x1 - x0)*(W-1));
       py = (int)((ymax - y)/(ymax - ymin)*(H-1));
       draw lines between consecutive (px,py) with the grx20 calls */
    free(ys);
    return 0;
}

Troubleshooting notes: always use double, guard against huge coefficients (overflow), clamp or clip out-of-range pixels, and handle the case where y=0 lies outside ymin..ymax (axis will be off-screen). If the curve has sharp features, increase sampling or use adaptive subdivision to avoid missed peaks. Check grx20 initialization and pixel/line functions in its docs before drawing.

Recommended Answers

All 2 Replies

I don't know much about c programming at all, as I have only been doing it for about five hours, any help would be much appreciated

Post your attempt with any error messages and the specific questions you have. Or read this.

This is a beginning programming class or something? And this is the first assignment? Phew.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.