Thread: I could use a little help with this simple program

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    1

    I could use a little help with this simple program

    Hello everybody,

    I am new to C, and have been playing around with various little makeshift programs. I would like to construct a program that takes whatever equation the user puts in, whether it be quadratic, linear, etc, and spits out the y variables into a neat little table. The x variables will be defined by a parameter as follows: (x=-5;x<=5;x++)

    The issue I'm having with the program that I made thus far, is that I don't know how to instruct the program to take whatever the user put in at scanf, and use it to construct new variables each time.

    Perhaps someone could point me in the right direction?

    Many thanks and kind regards,

    RL
    Attached Images Attached Images I could use a little help with this simple program-1-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,629
    <rant>
    Why is everyone posting pictures rather than code inside [code][/code] tags?
    Maybe they see one picture in a post and decide that's the norm.
    Perhaps I should go round deleting all "here is a picture of my code" posts to stop people thinking this is a good idea.
    </rant>

    The short answer is, it's complicated.
    c expression Evaluator - Stack Overflow

    C doesn't have an 'eval' like interpreted languages, so you can't just type in "x*x+7*x+3" and hand it over to some built-in language feature to do all the work for you.
    Sure you can do it, but it's not your average newbie question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    241
    Fancy input is certainly a nice feature, though not usually really necessary in most cases. A more simplified format might help get things up and running in the meantime.

    Example using command line parameters as input:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define MAX_TERMS 100
    
    void reverse_polynomial(double* polynomial, size_t length)
    {
     double* left = polynomial;
     double* right = left + length;
     while(left < --right)
     {
      double swap = *right;
      *right = *left;
      *left++ = swap;
     }
    }
    
    double evaluate_polynomial(double base, double* polynomial, size_t length)
    {
     double sum = 0;
     for(size_t power = 0; power < length; ++power)
      sum += polynomial[power] * pow(base, power);
     return sum;
    }
    
    void print_polynomial(double* polynomial, size_t length)
    {
     size_t degree = length - 1;
     for(size_t index = 0; index < length; ++index)
     {
      size_t power = degree - index;
      double value = polynomial[power];
      if(value == 0)
       continue;
      if(power == degree)
       printf("%g", value);
      else
      {
       if(value < 0)
        printf(" - %g", -value);
       else
        printf(" + %g", value);
      }
      if(power == 1)
       putchar('x');
      else if(power != 0)
       printf("x^%zu", power);
     }
     puts("");
    }
    
    #define die(message)\
     do\
     {\
      puts(message);\
      printf("Usage: %s [START] [END] [INCREMENT] [TERMS...]\n", argv[0]);\
      printf("Example: %s -1 1 0.1 2 0 -12 0.5 -1.5e3\n", argv[0]);\
      puts(" (From -1 to 1 in 0.1 increments evaluate 2x^4 - 12x^2 + 0.5x - 1500)");  \
      exit(EXIT_FAILURE);  \
     }\
     while(0)
    
    #define usage() die("")
    
    int main(int argc, char** argv)
    {
     if(argc < 5)
      usage();
     size_t adx = 1;
     double current = atof(argv[adx++]);
     double end = atof(argv[adx++]);
     if(current > end)
      die("END range must be less than START range");
     double increment = atof(argv[adx++]);
     if(increment == 0)
      die("Increment must be nonzero");
     double terms[MAX_TERMS];
     size_t count = 0;
     for(;;)
     {
      char* text = argv[adx++];
      if(text == NULL)
       break;
      if(count >= MAX_TERMS)
       die("Too many terms!");
      terms[count++] = atof(text);
     }
     reverse_polynomial(terms, count);
     print_polynomial(terms, count);
     puts("*** Evaluate ***");
     while(current <= end)
     {
      double x = current;
      printf("x: %g\n", x);
      double y = evaluate_polynomial(x, terms, count);
      printf("y: %g\n", y);
      current += increment;
      puts("");
     }
     return EXIT_SUCCESS;
    }

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    72
    Quote Originally Posted by Salem View Post
    <rant>
    Why is everyone posting pictures rather than code inside tags?
    Maybe they see one picture in a post and decide that's the norm.
    Perhaps I should go round deleting all "here is a picture of my code" posts to stop people thinking this is a good idea.
    </rant>
    What about removing the ability to use images? I mean this is a code site, right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM

Tags for this Thread