Thread: Trying to understand this homework

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    39

    Trying to understand this homework

    We're learning about C Functions right now, however I'm not exactly sure what my homework is asking for....

    Here's the problem:

    Trying to understand this homework-hwk1-png

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I think it is asking for a program that will calculate a running total where each element is tossed inside the exponent function and spit out into the adding program.

    Code:
    int polyTermToValue(int coefficient, int variable, int exponent) {
    	int value;
     
    	// calculations on value
     
    	return value;
    }
     
    int mainStuff() {
    	int coefficient = 0;
    	int variable = 0;
    	int exponent = 0;
    	int sum = 0;
    	int sentinel = 1;
    	while (sentinel) {
    		// ask user for coefficient, variable, exponent or ask user to enter sentinel
    		sum += polyTermToValue(coefficient, variable, exponent);
    	}
     
    	// sum is found.  do what you want with it.
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,431
    In the formula 'a' can be different for each term. Think of your formula as something like: 12x4 + 2x3 + x2 + 6x2 + 33. You need to supply the correct "a" value for each term and then do the multiplications required.

    Jim

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Yeah, so I figured out that you need to ask the user for inputs for the highest exponent, coefficients, and the value of X. You then need to calculate the function for user inputted X (coeffiecient(x)^exp) from the highest exponent to 0.

    Ex: 2x^3 + 3x^2 + 6x^1 + 2x^0

    Not sure how I'd do that, I have a start, but not sure where to go from next.

    Code:
    #include <stdio.h>
    
    
    int polynomial();
    int expon, valx, coeff, a;
    
    
    void main(){
        printf("Enter the highest exponent of your polynomial\n");
    	scanf("%d", expon);
    	
    	printf("\nEnter the value of x");
    	scanf("%d", valx);
    	
    	printf("\nEnter %d coefficients", expon+1);
    }
    
    
    int polynomial(){
    
    
    	int expon, valx, coeff, a;
    	int myArray[100];
    	
    	
    	printf("Enter the highest exponent of your polynomial\n");
    	scanf("%d", expon);
    	
    	printf("Enter the value of x\n");
    	scanf("%d", valx);
    	
    	printf("Enter %d coefficients\n", expon+1);
    	
    	
    	for(int i=0; i<expon; i++){
    		scanf("%d", coeff);
    		myArray[i] = coeff;
    	for(int j=0; j<expon; j++){
    		a = myArray[i];
    		myArray[i] = myArray[i+1];
    		myArray[i+1] = a;
    	}
    	}
    	
    	
    	for(int i=0; i<expon; i++){
    		a = myArray[i];
    		myArray[i] = myArray[i+1];
    		myArray[i+1] = a;
    	}
         
    
    
          
    }

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Any ideas? Updated code:

    Code:
    #include <stdio.h>
    
    
    int polynomial();
    int expon, valx, coeff, a;
    
    
    void main(){
       printf("The answer to your polynomial is %d\n ", polynomial());
    }
    
    
    int polynomial(){
    
    
    int xval, expo, coeff; //declare values
    int i, sum; // loop tracker/sum(total value)
    
    
    int myArray[1000]; // Array declaration
    
    
    printf("Please enter an x-value\n"); //scanning in x-value
    scanf("%d", xval);
    
    
    printf("Please enter the highest power of the exponent\n"); // Scan in highest exponent
    scanf("%d", expo);
    
    
    
    
    for (i = 0; i < expo; i++){
        printf("Please enter a coefficient\n");
    	scanf("%d", coeff);
    	coeff = myArray[i];
    	//scan in coefficients using array
    }
    for (i = 0; i < expo; i++){
        xval = xval * xval;
    }
    
    
    for(i = 0; i < expo; i++){
        xval = xval * myArray[i];//take xval and multiply it by the coefficient
        sum += myArray[i]; //add that to the sum
    }
    
    
    return sum;
    
    
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,431
    Any ideas?
    Perhaps you should re-read your instructions?
    Write one function definition which takes the coefficient (ai), variable (x), and exponent of the variable (in the term) as parameters.
    Where is this function that you will call for every term in the equation?

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    It's all inside the function polynomial()

    I need to do all of the math and return the value of (a, x and the exponent) in F(X).

    The value returned should be an integer. I don't need to show the polynomial.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,608
    I think the teacher wants something a little different than figuring it all out in polynomial().
    Code:
    input a
    input x
    input E
    double F_x = a;
    while (E > 0) {
       F_x += CalculateTerm(a, x, E--);
    }
    print F_x

  9. #9
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Quote Originally Posted by whiteflags View Post
    I think the teacher wants something a little different than figuring it all out in polynomial().
    Code:
    input a
    input x
    input E
    double F_x = a;
    while (E > 0) {
       F_x += CalculateTerm(a, x, E--);
    }
    print F_x

    I'm afraid I don't follow. There are terms "a*x^e". I think the function is supposed to calculate each term and then I'm supposed to call it as many times as the user inputted exponents (plus 1 for x^0)... I'm not sure how I'd go about doing that.

    I'm assuming the While loop is to call the function as many as times as exponents the user inputted.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,608
    The idea is that the highest exponent is also the number of times that you need to call the function that calculates terms. So if you write a function that can compute any one term, you end up with code similar to what I suggested.

    CalculateTerm(a,x,5) for example would return ax^5 to you, you add it to F_x, then you do the 4th power term, etc.

  11. #11
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I admit I don't have extensive experience in programming, but I have found that any programming problem is most easily solved if you use plenty of abstraction at the most advantageous points. I feel that is supposed to be the main point of your exercise here. I think your teacher is trying to help you to learn how to handle abstraction.

    When I posted above, I used the term running total. This was intentional. If you start with a simpler program perhaps it might be easier to visualize what you are to do. If you begin with a running total program you have something like this:
    Code:
    #include <stdio.h>
     
    int main(void) {
        int userEntry = -1;
        int sum = 0;
        while (userEntry) {
            puts("Enter an integer to add to the sum.  Enter 0 to quit.");
            scanf("%d", &userEntry);
            sum += userEntry;
        }
        printf("The sum of these integers is %d.\n", sum);
    }
    This is essentially a much simpler version of what I think you are supposed to do. The program above adds a sequence of numbers the user inputs. You could go one level deeper and ask the user to enter in something not quite so simple as an integer such as combinations of log elements (base and argument to find an answer) to find the sum of a series of logs which would essentially look like this:
    Code:
    #include <stdio.h>
    #include <math.h>
     
    int logToValue(int base, int arg);
     
    int main(void) {
        int base = -1;
        int arg = 0;
        int sum = 0;
        while (base) {
            puts("Enter an int log base to add to the sum.  Enter 0 to quit.");
            scanf("%d", &base);
            puts("Enter an int log argument to add to the sum.");
            scanf("%d", &arg);
            sum += logToValue(base, arg);
        }
        printf("The sum of these integers is %d.\n", sum);
    }
     
    int logToValue(int base, int arg) {
        int value = (int)(log(arg) / log(base));
        return value;
    }
    I guess what I first see in your original post is that you have a series of terms to sum. At a top layer of abstraction, it isn't terribly important how these individual terms are calculated. This is something you deal with after you have the top abstraction lain out. I know you don't need a series of logs. I just tried to illustrate the idea that you can get a running total of anything, and that getting a running total of logrithms or polynomial terms is done very much the same as any other running total.

    It could also be that my post is distracting from whiteflags. If so I apolgize.
    Last edited by jack jordan; 10-29-2017 at 11:12 PM.

  12. #12
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    By the way, I just noticed that there is a divide by zero possibility in the code I wrote that should be fixed with an if statement in the logToValue function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. don't understand
    By loongkee in forum C Programming
    Replies: 5
    Last Post: 12-22-2012, 03:49 AM
  2. Please help me understand how to think in C.
    By Strik3r in forum C Programming
    Replies: 7
    Last Post: 11-20-2011, 12:16 PM
  3. I don't understand
    By AndreyS10 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2011, 09:48 AM
  4. plz help me understand this
    By Farnaz in forum C# Programming
    Replies: 3
    Last Post: 07-06-2011, 11:15 AM
  5. I don't understand what this is asking!
    By mabufo in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2006, 10:24 AM

Tags for this Thread