write c programming of this question

Dani AI

Generated

asked for a C program to read coefficients m and c. showed the minimal scanf approach and reminded that attempting the work first is expected. Below is a practical, more robust pattern: prefer validation and parsing rather than assuming perfect input. Also consider the data type — slopes and intercepts are often real numbers, so double is usually a better choice than int.

A safe way is to read a whole line with fgets and parse with strtod, checking conversion success and range. The example below demonstrates this pattern and rejects malformed lines.

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

int main(void) {
    char buf[256];
    if (!fgets(buf, sizeof buf, stdin)) return 1;

    char *p = buf, *end;
    errno = 0;
    double m = strtod(p, &end);
    if (p == end || errno == ERANGE) return 2;

    p = end;
    errno = 0;
    double c = strtod(p, &end);
    if (p == end || errno == ERANGE) return 3;

    while (*end && isspace((unsigned char)*end)) end++;
    if (*end && *end != '\n') return 4;

    printf("Line: y = %.6g*x + %.6g\n", m, c);
    return 0;
}

Notes and troubleshooting: check fgets return for EOF, reset errno before strtod, and handle ERANGE for overflow/underflow. If integers are required, use strtol instead. See the standard library documentation for details: fgets and strtod manuals for behavior and error handling (fgets, strtod, scanf).

Recommended Answers

All 2 Replies

Is that an order towards us?
Why don't YOU write it and come back with a question if you encounter problems.
Then we will be happy to help.

Read a pair on integers?

int a, b;
scanf("%i %i", &a, &b);

Easiest homework question ever. This thread can now be closed :)

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.