Programming using C

Anything else. Post a funny site or tell us about yourself. Discuss current events or whatever else you want. Post off topic threads here.
User avatar
pipigrande
Active Member
Posts: 688
Joined: Sat Jul 15, 2006 9:36 pm
Quick Reply: Yes
Location: Canada

Post by pipigrande »

[SD]happynoobing wrote:lol dont ever doubt cruor when it comes to programming xD


I am guessing Cruor will join the SRF's Hall Of Fame with Bakemaster :D

2 smart people in our HOF... who will be the 3rd one?! :P

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Post by NuclearSilo »

Cruor wrote:
NuclearSilo wrote:It's not the extrema he wants, but the min or max between values.
Extrema is just plural for minima and maxima. Like I said, if he just wanted an extrema within the bounds -20≤x≤20 then the assignment would have been to calculate the extrema (which I may add could be done with derivatives as well as the shortcut, how it is done wouldn't matter in that case) and then check to see if it is within those bounds.

Here is what a program that uses your interpretation looks like:

Code: Select all

#include <stdio.h>

int main() {
    /* declare variables */
    float a, b, c, x, y;

    /* request user input */
    printf("y=ax\xFD+bx+c\n\n");
    printf("Enter a: ");
    scanf("%f", &a);
    printf("Enter b: ");
    scanf("%f", &b);
    printf("Enter c: ");
    scanf("%f", &c);

    /* find the extrema */
    x = (-1*b)/(2*a);

    /* calculate y at extrema */
    y = a*x*x+b*x+c;

    /* check extrema against bounds */
    if(x>=-20 && x<=20)
        printf("\nx=%.2f\ny=%.2f\n", x, y);
    else
        printf("\nThere is no extrema within the bounds -20\xF3x%s20\n", "\xF3") ;

    /* print my name */
    printf("\nFirstname Lastname");

    return 0;
}

And here's the derivative method:

Code: Select all

#include <stdio.h>

int main() {
    /* declare variables */
    float a, b, c, i, d, y;

    /* request user input */
    printf("y=ax\xFD+bx+c\n\n");
    printf("Enter a: ");
    scanf("%f", &a);
    printf("Enter b: ");
    scanf("%f", &b);
    printf("Enter c: ");
    scanf("%f", &c);

    /* print the derivative */
    printf("\nThe derivative is:\ndy/dx (x)=%.2fx%+.2f\n\n", a*2, b);

    for(i=-20; i<=20; i+=0.01) {
        /* calculate the value of the derivative at x */
        d = a*2*i+b;
        /* calculate y at x */
        y = (a*i*i)+(b*i)+c;
        /* print the results */
        printf("dy/dx (%.2f)=%.2f; y=%.2f\n", i, d, y);
    }

    /* print my name */
    printf("\nFirstname Lastname");

    return 0;
}

Compare the if/else statement in the first program to the for loop in the second program. The if/else statement has nothing to do with increments of 0.01 while the for loop does. That is why I think he wanted the second solution.

Rereading it, I also realized that the y values should be printed as well.

In the first program the output looks like:

Code: Select all

x=0.20
y=2.80
Or, if the extrema doesn't exist within the bounds:

Code: Select all

There is no extrema within the bounds -20≤x≤20

The output of the second program looks like:

Code: Select all

dy/dx (0.20)=0.00; y=2.80

Regardless of what his professor meant, the OP is now covered in either situation so I suppose it doesn't matter past that.

No. U didnt fully understand what i want to say. Hell, i dont understand what his teacher wants too...

The exercise requires u to find the max or min value located at the range -20,20 and it's not about the extrema of the parabola. So it has nothing to do with -b/(2a) or df=0. Or maybe im wrong. So i think pipigrande, u should ask your teacher again what he really wants.

If it's about min or max, u should compare the current value with the previous value calculated and pick the max between those 2, and so on.

Code: Select all

float max(float a, float b) {
     If (a<b)
           return b;
     else
           return a;
}


Cruor's code will print to the screen a list of 21 value of f(x) and f'(x) which is absolutely not what the teacher want.
Playing Age of Wushu, dota IMBA

User avatar
Cruor
Loyal Member
Posts: 1999
Joined: Wed Apr 12, 2006 1:22 am
Quick Reply: Yes
Location: Off topic

Post by Cruor »

NuclearSilo wrote:No. U didnt fully understand what i want to say. Hell, i dont understand what his teacher wants too...

The exercise requires u to find the max or min value located at the range -20,20 and it's not about the extrema of the parabola. So it has nothing to do with -b/(2a) or df=0. Or maybe im wrong. So i think pipigrande, u should ask your teacher again what he really wants.
Ah, I see now. He's looking for the local extrema then?

Here's a solution for that:

Code: Select all

#include <stdio.h>

int main() {
    /* declare variables */
    float a, b, c, i, j, k;

    /* request user input */
    printf("y=ax\xFD+bx+c\n\n");
    printf("Enter a: ");
    scanf("%f", &a);
    printf("Enter b: ");
    scanf("%f", &b);
    printf("Enter c: ");
    scanf("%f", &c);

    /* check for quadratic function */
    if(a==0){
        printf("\nThat function is linear.");
        return 0;
    }

    /* loop from x=-20 to x=20 in increments of 0.01 */
    for(i=-20; i<=20; i+=0.01) {
        /* compute y for x */
        j = a*i*i+i*b+c;
        /* if function opens down and maximum is passed.. */
        if(i>-20 && a<0 && k>j){
            /* find the previous value of x */
            i-=0.01;
            break;
        }
        /* if function opens up and minimum is passed.. */
        else if(i>-20 && a>0 && k<j){
            /* find the previous value of x */
            i-=0.01;
            break;
        }
        /* store the value of y at x for comparison later */
        k = j;
    }

    /* print the results */
    printf("\nx=%.2f\ny=%.2f", i, k);
    printf("\n\nFirstname Lastname");

    return 0;
}
Hopefully one of these solutions is what the professor wanted. :banghead:

pipigrande wrote:Now im gonna copy them in the program which I will dowload tonight and see how it works. (Program's Name: Quincy).
Eww, try wxDev-C++ or MSVC Express.
Image

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Post by NuclearSilo »

If u use a program in Windows like Dev-C++ to run. Before return 0; u should add: system("PAUSE");
The program will pause temporary so u can see what was printed on the screen.
If u dont use it, u need to run the exe through CMD.

@Cruor, i think u forgot to initiate k and j before making a comparison
Playing Age of Wushu, dota IMBA

User avatar
Cruor
Loyal Member
Posts: 1999
Joined: Wed Apr 12, 2006 1:22 am
Quick Reply: Yes
Location: Off topic

Post by Cruor »

NuclearSilo wrote:@Cruor, i think u forgot to initiate k and j before making a comparison
It skips the comparison if the loop is in its first iteration.
Image

User avatar
pipigrande
Active Member
Posts: 688
Joined: Sat Jul 15, 2006 9:36 pm
Quick Reply: Yes
Location: Canada

Post by pipigrande »

So, yesterday night I wasn't at home so I couldn't dowload the program.

So, today I am at home and i downloaded the program and all yours programs works nicely without any syntax error (as expected).

Also, since you guys having confusion as what the assignment is actually asking for... yesterday I emailed the prof asking what he meant when he wrote: "Find the minimum value (or maximum depending on the parabola)
of y for all values of x between -20.0 and +20.0 in increments
of 0.01."
. He has responded me with the assigment intructions and then a equation (which i wonder if it is the actual equation we have to use or an example):


CPS118
Introductory Programming for Scientists
Assignment #1 – Fall 2007
Due on or before October 29, 2007 11:59 p.m.
What to do:
The equation of the parabola is
y = ax² + bx + c
Write a complete C program capable of finding the maximum or
minimum of a parabola. As input, the program is to ask the user
data consisting of the values of a, b and c for the parabola.
Find the minimum value (or maximum depending on the parabola)
of y for all values of x between -20.0 and +20.0 in increments
of 0.01.
Your program must produce a nicely aligned report. Display the
minimum/maximum value of y and the value of x at that point.
Display your name(s) at the bottom of the report.
Your code must have good style including comments and proper
indentation.
All numerical values on the report must come from variables,
not constants.
equation max/min of y value of x
------------------- -------------- ----------
y = 1.1x*x – 2x + 1 0.09 (min) 0.91

Report presented by your name(s) here.



So, does this help?... cus the prof didn't answer me with anything... just this. Hopefully, by now you guys know what the specifications are... cus I still don't know.

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Post by NuclearSilo »

Still not very clear :)
Write a complete C program capable of finding the maximum or
minimum of a parabola

I understand this one
Find the minimum value (or maximum depending on the parabola)
of y for all values of x between -20.0 and +20.0 in increments
of 0.01.

I understand that the program will search for the min or max of all values between -20, 20

So, the exercise requires u do to 2 things: min(max) of the parabola and min(max) of the values betweens -20-20.
Or only 1 thing...., but what???
Playing Age of Wushu, dota IMBA

User avatar
Cruor
Loyal Member
Posts: 1999
Joined: Wed Apr 12, 2006 1:22 am
Quick Reply: Yes
Location: Off topic

Post by Cruor »

I think that nothing more is needed, save for that formatted output.

Here's one more program so your professor can have his nice formatted output:

Code: Select all

#include <stdio.h>

int main() {
    /* declare variables */
    float a, b, c, i, j, k;
    int min;

    /* request user input */
    printf("y=ax\xFD+bx+c\n\n");
    printf("Enter a: ");
    scanf("%f", &a);
    printf("Enter b: ");
    scanf("%f", &b);
    printf("Enter c: ");
    scanf("%f", &c);

    /* check for quadratic function */
    if(a==0){
        printf("\nThat function is linear.");
        return 0;
    }

    /* loop from x=-20 to x=20 in increments of 0.01 */
    for(i=-20; i<=20; i+=0.01) {
        /* compute y for x */
        j = a*i*i+i*b+c;
        /* if function opens down and critical point is passed.. */
        if(i>-20 && a<0 && k>j){
            /* find the previous value of x */
            i-=0.01;
            min=0;
            break;
        }
        /* if function opens up and critical point is passed.. */
        else if(i>-20 && a>0 && k<j){
            /* find the previous value of x */
            i-=0.01;
            min=1;
            break;
        }
        /* store the value of y at x for comparison later */
        k = j;
    }

    /* print the results */
    printf("\nequation             max/min of y  value of x\n");
    printf("-------------------  ------------  ----------\n");
    printf("y = %.1fx\xFD%+.1fx%+.1f", a, b, c);
    if(a>0)
        printf("   ");
    else
        printf("  ");
    printf("%.2f ", k);
    if(min)
        printf("(min)");
    else
        printf("(max)");
    if(k>=0)
        printf("    ");
    else
        printf("   ");
    printf("%.2f", i);
    printf("\n\nReport presented by Firstname Lastname.");

    return 0;
}

It gets a little chunky at the end in order to keep things spaced properly when accounting for signs. I could have made the leading signs always display and skipped the spacing conditionals but it doesn't look as nice.

I suppose the only thing left now is whether or not there is anything left that you don't understand. It looks like you are expected to know how to sign and truncate floats, so I would assume that you also know the hexadecimal escape sequence "\xFD" used to print ² in the console. Past that, I don't see any problems.
Image

User avatar
pipigrande
Active Member
Posts: 688
Joined: Sat Jul 15, 2006 9:36 pm
Quick Reply: Yes
Location: Canada

Post by pipigrande »

Cruor wrote:I think that nothing more is needed, save for that formatted output.

Here's one more program so your professor can have his nice formatted output:

Code: Select all

#include <stdio.h>

int main() {
    /* declare variables */
    float a, b, c, i, j, k;
    int min;

    /* request user input */
    printf("y=ax\xFD+bx+c\n\n");
    printf("Enter a: ");
    scanf("%f", &a);
    printf("Enter b: ");
    scanf("%f", &b);
    printf("Enter c: ");
    scanf("%f", &c);

    /* check for quadratic function */
    if(a==0){
        printf("\nThat function is linear.");
        return 0;
    }

    /* loop from x=-20 to x=20 in increments of 0.01 */
    for(i=-20; i<=20; i+=0.01) {
        /* compute y for x */
        j = a*i*i+i*b+c;
        /* if function opens down and critical point is passed.. */
        if(i>-20 && a<0 && k>j){
            /* find the previous value of x */
            i-=0.01;
            min=0;
            break;
        }
        /* if function opens up and critical point is passed.. */
        else if(i>-20 && a>0 && k<j){
            /* find the previous value of x */
            i-=0.01;
            min=1;
            break;
        }
        /* store the value of y at x for comparison later */
        k = j;
    }

    /* print the results */
    printf("\nequation             max/min of y  value of x\n");
    printf("-------------------  ------------  ----------\n");
    printf("y = %.1fx\xFD%+.1fx%+.1f", a, b, c);
    if(a>0)
        printf("   ");
    else
        printf("  ");
    printf("%.2f ", k);
    if(min)
        printf("(min)");
    else
        printf("(max)");
    if(k>=0)
        printf("    ");
    else
        printf("   ");
    printf("%.2f", i);
    printf("\n\nReport presented by Firstname Lastname.");

    return 0;
}

It gets a little chunky at the end in order to keep things spaced properly when accounting for signs. I could have made the leading signs always display and skipped the spacing conditionals but it doesn't look as nice.

I suppose the only thing left now is whether or not there is anything left that you don't understand. It looks like you are expected to know how to sign and truncate floats, so I would assume that you also know the hexadecimal escape sequence "\xFD" used to print ² in the console. Past that, I don't see any problems.


Thanks Cruor, but I don't know \xFD is. We haven't done that yet.

So i'm guessing the equation the prof sent me was the way he wanted my work to be printed out. right? or?... :?

Anyways I sent him another email, telling him that I still dont understan the assignment and that I am hoping a REAL asnwer from him.

edit: So, my prof responded me and he says that he only wants one value, as long as x is between-20 and 20. If not, indicate by a message. So, I am guesssing the last program you wrote Cruor is the one... since it works nicely when i put it on Quincy.

Thanks a LOT!. Meh.. If u ever need anything (even gold in sro :D ). Send me a pm.

Post Reply

Return to “Off Topic Lounge”