We're learning about C Functions right now, however I'm not exactly sure what my homework is asking for....
Here's the problem:
We're learning about C Functions right now, however I'm not exactly sure what my homework is asking for....
Here's the problem:
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. }
In the formula 'a' can be different for each term. Think of your formula as something like: 12x^{4} + 2x^{3} + x^{2} + 6x^{2} + 33. You need to supply the correct "a" value for each term and then do the multiplications required.
Jim
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; } }
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; }
Perhaps you should re-read your instructions?Any ideas?
Where is this function that you will call for every term in the equation?Write one function definition which takes the coefficient (a_{i}), variable (x), and exponent of the variable (in the term) as parameters.
Jim
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.
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.
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.
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:
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> 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); }
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.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; }
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.
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.