Like Tree1Likes
  • 1 Post By anduril462

First Computer Program Assignment using c code?

This is a discussion on First Computer Program Assignment using c code? within the C Programming forums, part of the General Programming Boards category; The assignment is Write a program that asks the user to enter an integer value for x and then calculate ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    First Computer Program Assignment using c code?

    The assignment is Write a program that asks the user to enter an integer value for x and then calculate the value for the following polynomial: x^5-5x^4+10x^3-10x^2+5x+1.
    This is my program and it is not working, but I am not sure why. Help Please!
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
      int x, polynomial;
    
      printf("Please input an interger value for x: ");
      scanf("%d", &x);
      polynomial =
          pow(x, 5) - 5 pow(x, 4) + 10 pow(x, 3) - 10 pow(x, 2) + 5 pow(x,
                                                                        1) + 1;
    
      printf("The value for the polynomial of x = %d is: %d\n", x, polynomial);
    
      return 0;
    }
    Last edited by Salem; 09-11-2012 at 11:18 PM. Reason: demungled crazy fonts

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,516
    Some general suggestions first:

    • Remember, we are here to help you, but you must help us help you. Do everything you can to make our job here easier, and you'll get a better response. Read this link: How To Ask Questions The Smart Way.
    • Copy-paste your program as plain text, and make sure you copy the indentation too. That will allow our syntax highlighter to do it's job.
    • Make sure your post looks nice when you're done, not two funky code blocks, one in variable-width font and no indentation. Edit your post if need be. This makes people more likely to help you.
    • Provide any configuration or input files, links to external resources or assignment instructions we may need to make your program work. This doesn't apply here, but might in the future, for more complex assignments.
    • Give a better description than "doesn't work". Try:
    • Does it compile? If not, copy-paste the error messages with line numbers, so we can more easily trouble shoot.
    • Does your program crash when you try to run it? If so, how far does it get? What is the last line of output you saw? What did you do/input to make it crash?
    • Does it run but the output is wrong? What output do you get? What output did you expect?


    All that will help you get a better answer in the future.

    That being said, I see one problem: C can't infer when it should multiply, you must tell it explicitly. Thus:
    Code:
    5 pow(x,4)
    // should be
    5 * pow(x,4)
    And similar for the other ones.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Thank you, that fixed my error. I will be sure to post more appropriately in the future, but I don't understand what you mean when you say copy the indentation too. If you could please explain that because when I copy and paste my code, I do not get any syntax highlighting. Thank you!

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,435
    In my own experience, the syntax highlighting can be flukey at times. Sometimes it takes a refresh or two for it to come up, and sometimes even that doesn't help.

    Also, if you only have a few lines of code, and all the indentation is lost, just go back and edit and add it manually. I usually use four spaces for each tabstop.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,516
    Quote Originally Posted by blisman20 View Post
    Thank you, that fixed my error. I will be sure to post more appropriately in the future, but I don't understand what you mean when you say copy the indentation too. If you could please explain that because when I copy and paste my code, I do not get any syntax highlighting. Thank you!
    What I mean is, make sure when you copy-paste, you actually copy your program with indentation. Depending on your editor/IDE settings, you may use tabs instead of spaces, or have some other setting that effectively removes the indentation when you paste into the editor window. Basically, your code looks like this:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    {
    int x,polynomial;
    
    
    printf ( "Please input an interger value for x: ");
    scanf ( "%d", &x);
    polynomial = pow (x,5) - 5 pow (x,4) + 10 pow (x,3) - 10 pow (x,2) + 5 pow(x,1) + 1;
    
    
    printf ( "The value for the polynomial of x = %d is: %d\n", x, polynomial);
    
    
    return 0;
    }
    but it should look like this:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    {
        int x,polynomial;
    
    
        printf ( "Please input an interger value for x: ");
        scanf ( "%d", &x);
        polynomial = pow (x,5) - 5 pow (x,4) + 10 pow (x,3) - 10 pow (x,2) + 5 pow(x,1) + 1;
    
    
        printf ( "The value for the polynomial of x = %d is: %d\n", x, polynomial);
    
        // imagine you had an if statement like the following
        if (print_indent_line) {
            printf("this line of code should be more indented because it is subordinate to the if statement\n");
       }
    
    
        return 0;
    }
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-26-2011, 01:14 AM
  2. C++ code for school assignment
    By Pupo in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2010, 07:11 AM
  3. Code compiles for one computer, but not the other
    By Akkernight in forum C++ Programming
    Replies: 8
    Last Post: 03-10-2009, 04:13 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. HELP! this easy code locks my computer!
    By porsuk in forum C Programming
    Replies: 7
    Last Post: 07-13-2005, 12:47 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21