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

Programming using C

Post by pipigrande »

Sup guys, Just wondering if there are any person out there that knows how to write a program using C?

Im kinda noob at this and I have a big assignment due on sunday o.O...

Tried readin my textbook and everything but I dont get it :?

Like i understand the basics such as printf and scanf and stuff like that... but when it comes down to using nefted loops and ifs... i get lost. I tried using internet to help, but nothing.

Basically i have to write a program, and type the equation of a parabola and then make the program be able to find the maximun and minimun points. Like my professor wrote this: Find the minimum value or maximun value of y for all values of x between -20.0 and +20.0 in increments
of 0.01.

>.<

Anyone help?

Thanks
Last edited by pipigrande on Fri Oct 26, 2007 9:05 pm, edited 1 time in total.

User avatar
Innovacious
Advanced Member
Posts: 2446
Joined: Mon Sep 11, 2006 9:24 pm
Quick Reply: Yes
Location: Off Topic
Contact:

Re: Programming using C+

Post by Innovacious »

pipigrande wrote:Sup guys, Just wondering if there are any person out there that knows how to write a program using C+?

Im kinda noob at this and I have a big assignment due on sunday o.O...

Tried readin my textbook and everything but I dont get it :?

Like i understand the basics such as printf and scanf and stuff like that... but when it comes down to using nexted loops and ifs... i get lost. I tried using internet to help, but nothing.

>.<

Anyone help?

Thanks


its C++, and nexted loops? Its nested, as in one loop is nested inside another (you changed it to nefted while i was replying, still wrong :P ) . By the sounds of it you are screwed. Also, you havnt actually told us what you need to do so we cant help
ImageImage

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

Re: Programming using C+

Post by pipigrande »

Innovacious wrote:
pipigrande wrote:Sup guys, Just wondering if there are any person out there that knows how to write a program using C+?

Im kinda noob at this and I have a big assignment due on sunday o.O...

Tried readin my textbook and everything but I dont get it :?

Like i understand the basics such as printf and scanf and stuff like that... but when it comes down to using nexted loops and ifs... i get lost. I tried using internet to help, but nothing.

>.<

Anyone help?

Thanks


its C++, and nexted loops? Its nested, as in one loop is nested inside another (you changed it to nefted while i was replying, still wrong :P ) . By the sounds of it you are screwed. Also, you havnt actually told us what you need to do so we cant help


Yeah, you are right. I mean loops inside another loops. So nested loops.

I am not using C++. I use the other one simpler than this one, which we call C+ or simply C.

Oh yea.. i forgot to say wat the assignment was... sorry.

I will post it in the post.. take a look at it :)

User avatar
micheal_safian
Common Member
Posts: 106
Joined: Sun Dec 31, 2006 3:43 am
Quick Reply: Yes
Location: Athens

Re: Programming using C+

Post by micheal_safian »

pipigrande wrote:Sup guys, Just wondering if there are any person out there that knows how to write a program using C+?

Im kinda noob at this and I have a big assignment due on sunday o.O...

Tried readin my textbook and everything but I dont get it :?

Like i understand the basics such as printf and scanf and stuff like that... but when it comes down to using nefted loops and ifs... i get lost. I tried using internet to help, but nothing.

>.<

Anyone help?

Thanks


i can help u out if u tell me exactly what u need ..and yeah it's called nested loop where u make a loop inside another

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

Post by pipigrande »

I sent you a pm micheal, with the exact specifications to see if you can help.

I dont think i should copy and paste the exact specifications, cus that might get other 'novice' C programmers confused.

So, I was just asking something not so simple to see if people could help me out here.


Anyways... if anyone else wanna help, please do so

Thanks

User avatar
Innovacious
Advanced Member
Posts: 2446
Joined: Mon Sep 11, 2006 9:24 pm
Quick Reply: Yes
Location: Off Topic
Contact:

Re: Programming using C

Post by Innovacious »

pipigrande wrote:Basically i have to write a program, and type the equation of a parabola and then make the program be able to find the maximun and minimun points. Like my professor wrote this: Find the minimum value or maximun value of y for all values of x between -20.0 and +20.0 in increments
of 0.01.

>.<

Anyone help?

Thanks


lol, its 10pm and i dont feel like working out algorithms right now unless i really have to. I suppose for the main part of it you will need to loop while incrementing x. something like this maybe

Code: Select all

for(x=-20;x<=20;x=x+0.01)
{
}


been a few months since ive done any C programming :\
ImageImage

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

Re: Programming using C

Post by pipigrande »

Innovacious wrote:
pipigrande wrote:Basically i have to write a program, and type the equation of a parabola and then make the program be able to find the maximun and minimun points. Like my professor wrote this: Find the minimum value or maximun value of y for all values of x between -20.0 and +20.0 in increments
of 0.01.

>.<

Anyone help?

Thanks


lol, its 10pm and i dont feel like working out algorithms right now unless i really have to. I suppose for the main part of it you will need to loop while incrementing x. something like this maybe

Code: Select all

for(x=-20;x<=20;x=x+0.01)
{
}


been a few months since ive done any C programming :\


C programming is soooo hard. It's like learning another language.

Ya, i was almost thinking the same thing. But shouldnt it be:
for(x=-20;x<=20;x=0.01) ??

User avatar
Innovacious
Advanced Member
Posts: 2446
Joined: Mon Sep 11, 2006 9:24 pm
Quick Reply: Yes
Location: Off Topic
Contact:

Re: Programming using C

Post by Innovacious »

pipigrande wrote:
Innovacious wrote:
pipigrande wrote:Basically i have to write a program, and type the equation of a parabola and then make the program be able to find the maximun and minimun points. Like my professor wrote this: Find the minimum value or maximun value of y for all values of x between -20.0 and +20.0 in increments
of 0.01.

>.<

Anyone help?

Thanks


lol, its 10pm and i dont feel like working out algorithms right now unless i really have to. I suppose for the main part of it you will need to loop while incrementing x. something like this maybe

Code: Select all

for(x=-20;x<=20;x=x+0.01)
{
}


been a few months since ive done any C programming :\


C programming is soooo hard. It's like learning another language.

Ya, i was almost thinking the same thing. But shouldnt it be:
for(x=-20;x<=20;x=0.01) ??


that would make x equal 0.01 after every loop, and would cause that to fall into an infinite loop. You want x to equal what it already equals plus the extra 0.01.
ImageImage

User avatar
micheal_safian
Common Member
Posts: 106
Joined: Sun Dec 31, 2006 3:43 am
Quick Reply: Yes
Location: Athens

Post by micheal_safian »

man i pmed u with the solution ....read it.I hope i helped :D :D :D

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

Post by pipigrande »

micheal_safian wrote:man i pmed u with the solution ....read it.I hope i helped :D :D :D


I dont understand nested loops... Now u gave me something using 'while loops'... man now im more confused :? :banghead: .. Thanks for helping though :love:

I never knew programming was so hard... O M G

User avatar
micheal_safian
Common Member
Posts: 106
Joined: Sun Dec 31, 2006 3:43 am
Quick Reply: Yes
Location: Athens

Post by micheal_safian »

pipigrande wrote:
micheal_safian wrote:man i pmed u with the solution ....read it.I hope i helped :D :D :D


I dont understand nested loops... Now u gave me something using 'while loops'... man now im more confused :? :banghead: .. Thanks for helping though :love:

I never knew programming was so hard... O M G


man while loop is pretty easy to understand and it fits in ur situation here as u don't need a nested for loop ...the while loop i wrote is the exact u need ..if u can't understand it copy and paste it but be careful with the variables names. While loop is all about a condition in the first and iterating in the body of it ..that's all... :D :D how many classes did u take in programming man ?????

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

Post by pipigrande »

micheal_safian wrote:
pipigrande wrote:
micheal_safian wrote:man i pmed u with the solution ....read it.I hope i helped :D :D :D


I dont understand nested loops... Now u gave me something using 'while loops'... man now im more confused :? :banghead: .. Thanks for helping though :love:

I never knew programming was so hard... O M G


man while loop is pretty easy to understand and it fits in ur situation here as u don't need a nested for loop ...the while loop i wrote is the exact u need ..if u can't understand it copy and paste it but be careful with the variables names. While loop is all about a condition in the first and iterating in the body of it ..that's all... :D :D how many classes did u take in programming man ?????


How many?... Let me think.. this is a hard question... This is my first class!
Have never taken it before =S... but i need it in order to pass the course.

Need help >.>

User avatar
micheal_safian
Common Member
Posts: 106
Joined: Sun Dec 31, 2006 3:43 am
Quick Reply: Yes
Location: Athens

Post by micheal_safian »

pipigrande wrote:
micheal_safian wrote:
pipigrande wrote:
micheal_safian wrote:man i pmed u with the solution ....read it.I hope i helped :D :D :D


I dont understand nested loops... Now u gave me something using 'while loops'... man now im more confused :? :banghead: .. Thanks for helping though :love:

I never knew programming was so hard... O M G


man while loop is pretty easy to understand and it fits in ur situation here as u don't need a nested for loop ...the while loop i wrote is the exact u need ..if u can't understand it copy and paste it but be careful with the variables names. While loop is all about a condition in the first and iterating in the body of it ..that's all... :D :D how many classes did u take in programming man ?????


How many?... Let me think.. this is a hard question... This is my first class!
Have never taken it before =S... but i need it in order to pass the course.

Need help >.>

this is foolish thing to give such a task in ur first class..... :shock: :shock: :shock: :shock:

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 »

isnt max or min = f(-2b/a)?
Playing Age of Wushu, dota IMBA

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

Post by pipigrande »

NuclearSilo wrote:isnt max or min = f(-2b/a)?


wat?

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

Post by Cruor »

It sounds to me like he wants you to differentiate f(x) then output a table of values for f'(x) in increments of 0.01

I also assume since you mentioned a parabola that you will only be dealing with quadratic functions. Therefore, you can simply ask the user for a, b, and c. For the function f(x)=5x^2+2x-3, a is 5, b is 2, and c is -3. Of course, the derivative is simply f'(x)=10x+2 so there is nothing difficult to do there. After you've got the derivative all you have to do is plug in numbers for x and print the results. The extrema are of course wherever f'(x)=0

Here's a quickie I wrote:

Code: Select all

#include <stdio.h>

int main() {
    int a;
    int b;
    int c;
    float i;
    float y;

    printf ("f(x)=ax^2+bx+c\n\n");
    printf ("Enter a: ");
    scanf ("%d", &a);
    printf ("Enter b: ");
    scanf ("%d", &b);
    printf ("Enter c: ");
    scanf ("%d", &c);

    a *= 2;

    printf ("\nf'(x)=%dx+%d\n\n", a, b);

    for (i=-20; i<=20; i+=0.01) {
        y = a*i+b;
        printf("f'(%f)=%f\n", i, y);}

    return 0;}

You'll probably have to tweak it to meet your specific needs (possibly error checking and truncation on the floats if it's expected) but that should give you a start. Hope this helped.

If nested loops confuse you I bet you'll have fun with recursive functions. :P
Image

User avatar
micheal_safian
Common Member
Posts: 106
Joined: Sun Dec 31, 2006 3:43 am
Quick Reply: Yes
Location: Athens

Post by micheal_safian »

Cruor wrote:It sounds to me like he wants you to differentiate f(x) then output a table of values for f'(x) in increments of 0.01

I also assume since you mentioned a parabola that you will only be dealing with quadratic functions. Therefore, you can simply ask the user for a, b, and c. For the function f(x)=5x^2+2x-3, a is 5, b is 2, and c is -3. Of course, the derivative is simply f'(x)=10x+2 so there is nothing difficult to do there. After you've got the derivative all you have to do is plug in numbers for x and print the results. The extrema are of course wherever f'(x)=0

Here's a quickie I wrote:

Code: Select all

#include <stdio.h>

int main() {
    int a;
    int b;
    int c;
    float i;
    float y;

    printf ("f(x)=ax^2+bx+c\n\n");
    printf ("Enter a: ");
    scanf ("%d", &a);
    printf ("Enter b: ");
    scanf ("%d", &b);
    printf ("Enter c: ");
    scanf ("%d", &c);

    a *= 2;

    printf ("\nf'(x)=%dx+%d\n\n", a, b);

    for (i=-20; i<=20; i+=0.01) {
        y = a*i+b;
        printf("f'(%f)=%f\n", i, y);}

    return 0;}

You'll probably have to tweak it to meet your specific needs (possibly error checking and truncation on the floats if it's expected) but that should give you a start. Hope this helped.

If nested loops confuse you I bet you'll have fun with recursive functions. :P


nice work ...lol...i didn't think that parabola is calc thing .....lol..i coded it fro him but as just an equation. :D :D :D :D :D

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:It sounds to me like he wants you to differentiate f(x) then output a table of values for f'(x) in increments of 0.01

I also assume since you mentioned a parabola that you will only be dealing with quadratic functions. Therefore, you can simply ask the user for a, b, and c. For the function f(x)=5x^2+2x-3, a is 5, b is 2, and c is -3. Of course, the derivative is simply f'(x)=10x+2 so there is nothing difficult to do there. After you've got the derivative all you have to do is plug in numbers for x and print the results. The extrema are of course wherever f'(x)=0

Here's a quickie I wrote:

Code: Select all

#include <stdio.h>

int main() {
    int a;
    int b;
    int c;
    float i;
    float y;

    printf ("f(x)=ax^2+bx+c\n\n");
    printf ("Enter a: ");
    scanf ("%d", &a);
    printf ("Enter b: ");
    scanf ("%d", &b);
    printf ("Enter c: ");
    scanf ("%d", &c);

    a *= 2;

    printf ("\nf'(x)=%dx+%d\n\n", a, b);

    for (i=-20; i<=20; i+=0.01) {
        y = a*i+b;
        printf("f'(%f)=%f\n", i, y);}

    return 0;}

You'll probably have to tweak it to meet your specific needs (possibly error checking and truncation on the floats if it's expected) but that should give you a start. Hope this helped.

If nested loops confuse you I bet you'll have fun with recursive functions. :P

Are u kidding? :roll:
U dont need the derivative in order to find the extrema. For all parabola type f(x) = ax²+bx+c:
extrema = f(-b/(2a))
Playing Age of Wushu, dota IMBA

User avatar
Jstar1
Senior Member
Posts: 4757
Joined: Wed Mar 28, 2007 1:30 am
Quick Reply: Yes
Location: Off Topic

Post by Jstar1 »

what is it, college or a high school class? cause I don't think we have programming at our high school
Image

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:Are u kidding? :roll:
U dont need the derivative in order to find the extrema. For all parabola type f(x) = ax²+bx+c:
extrema = f(-b/(2a))
And where do you think that equation came from? (although the way you wrote it, the extrema would be equal to a(-b/2a)²+b(-b/2a)+c)

f(x) = ax²+bx+c
f'(x) = 2ax+b
2ax+b = 0
2ax = -b
x = -b/(2a)

Anyway, the problem with it is that it doesn't satisfy the requirements of the assignment. He needs to output values for f'(x) where -20≤x≤20. Whether the extrema is in that range doesn't matter. He can't just calculate the extrema using that shortcut.
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:

Code: Select all

#include <stdio.h>

int main() {
    int a;
    int b;
    int c;
    float i;
    float y;

    printf ("f(x)=ax^2+bx+c\n\n");
    printf ("Enter a: ");
    scanf ("%d", &a);
    printf ("Enter b: ");
    scanf ("%d", &b);
    printf ("Enter c: ");
    scanf ("%d", &c);

a *= 2
    printf ("\nf'(x)=%dx+%d\n\n", a, b);

    for (i=-20; i<=20; i+=0.01) {
        y = a*i+b;
        printf("f'(%f)=%f\n", i, y);}

    return 0;}



Ok, first of all, thanks everyone for trying to help me.. I really appreaciate it :love: .

Now, could anyone explain wat does a *= 2
part on cruor program means?...

Also, taking a look in the program it seems, that the program will keep looping from i=-20 until i reaches equal to 19 (20 is not included - i suppose). But, then i dont want to find all the vallues in between -20 and 20. How should I write the program so, it just gives me the maximun point and the minimun point (at -20 and 20).

Do you guys understand what im trying to say? If not I could type the specific intructioons the prof gave me.

Jstar1 wrote:what is it, college or a high school class? cause I don't think we have programming at our high school


I am at Ryerson University, and my program is biology. In high school, I never took programming, but for the university ALL first year science students must take the course. Also, the university expect us to know some basic programming stuff, since we were supposed to take it in high school, but my high school didnt offer the course.

This is pretty much new to me, and trust me ITS HARD. I read 2 books on this stuff, and i dont really understand anything...

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

Post by pipigrande »

Well, I think I am gonna copy and paste the specific intructioons, so you guys know what I am talking about.

So, here it goes:

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.

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

Post by Cruor »

pipigrande wrote:explain wat does a *= 2
It's shorthand for:

Code: Select all

a = a * 2


pipigrande wrote:the program will keep looping from i=-20 until i reaches equal to 19 (20 is not included - i suppose)
No, 20 is included because the condition is less-than-or-equal-to:

Code: Select all

x <= 20


My solution basically meets all his requirements, although I have no idea what is he talking about when he says:
Find the minimum value (or maximum depending on the parabola)
of y for all values of x
Parabolas only have one extrema, therefore there won't be an extrema for every point on the parabola. That is why I took the derivative approach.
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 »

Cruor wrote:
NuclearSilo wrote:Are u kidding? :roll:
U dont need the derivative in order to find the extrema. For all parabola type f(x) = ax²+bx+c:
extrema = f(-b/(2a))
And where do you think that equation came from? (although the way you wrote it, the extrema would be equal to a(-b/2a)²+b(-b/2a)+c)

f(x) = ax²+bx+c
f'(x) = 2ax+b
2ax+b = 0
2ax = -b
x = -b/(2a)

Anyway, the problem with it is that it doesn't satisfy the requirements of the assignment. He needs to output values for f'(x) where -20≤x≤20. Whether the extrema is in that range doesn't matter. He can't just calculate the extrema using that shortcut.

Whatever...
In my school, the formula of the extrema is acquired automatically, it means that u can use it and u dont need to demonstrate through the derivative.

Code: Select all

#include <stdio.h>

int main() {
    int a;
    int b;
    int c;
    float i;
    float y;

    printf ("f(x)=ax^2+bx+c\n\n");
    printf ("Enter a: ");
    scanf ("%d", &a);
    printf ("Enter b: ");
    scanf ("%d", &b);
    printf ("Enter c: ");
    scanf ("%d", &c);

a *= 2
    printf ("\nf'(x)=%dx+%d\n\n", a, b);

    for (i=-20; i<=20; i+=0.01) {
        y = a*i+b;
        printf("f'(%f)=%f\n", i, y);}

    return 0;}


This code calculate f'(x) from x=-20 to x=+20, which is not what the exercise tell u to do.

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.

If u read carefully again, u have to calculate f(x) and then give the max or min between those value. Because extrema can be located out of the range of -20,20 so u cant absolutely use the derivative.
Playing Age of Wushu, dota IMBA

andrejat
Active Member
Posts: 626
Joined: Sat Jul 21, 2007 3:49 am
Quick Reply: Yes
Location: Odin

Post by andrejat »

*reads all above posts*

Hah, forget taking C programming next year xD No wonder you need calculus at my school to even start.

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:If u read carefully again, u have to calculate f(x) and then give the max or min between those value. Because extrema can be located out of the range of -20,20 so u cant absolutely use the derivative.
I'm reading it, and it's hard to tell what the professor is asking for. If he just wants the extrema then why does he say this?
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
.
It's like he's asking for more than one extrema, which doesn't make sense. If he just wants the extrema he would just ask that it be calculated and then checked for the bounds -20≤x≤20, but you wouldn't use increments of x for that.

Anyway, if he is asking for the derivative method, I fixed that earlier program up a bit so that it meets his instructions.

Code: Select all

#include <stdio.h>

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

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

    /* differentiate a */
    a*=2;

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

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

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

    return 0;
}
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 »

Cruor wrote:
NuclearSilo wrote:If u read carefully again, u have to calculate f(x) and then give the max or min between those value. Because extrema can be located out of the range of -20,20 so u cant absolutely use the derivative.
I'm reading it, and it's hard to tell what the professor is asking for. If he just wants the extrema then why does he say this?
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
.
It's like he's asking for more than one extrema, which doesn't make sense. If he just wants the extrema he would just ask that it be calculated and then checked for the bounds -20≤x≤20, but you wouldn't use increments of x for that.

Anyway, if he is asking for the derivative method, I fixed that earlier program up a bit so that it meets his instructions.

Code: Select all

#include <stdio.h>

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

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

    /* differentiate a */
    a*=2;

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

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

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

    return 0;
}

lol
It's not the extrema he wants, but the min or max between values.
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: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.
Image

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

Post by pipigrande »

WOW, thanks Cruor and NuclearSilo... those programs are gonna help me a lot.

Now im gonna copy them in the program which I will dowload tonight and see how it works. (Program's Name: Quincy).

Really Thankgful guys...

See, now I will probably wont fail the course. I had my 1st midterm and i got 96%, and the 2nd midterm i got 60% (this one had problems similar to this assignment). This assignment is suppose to cover 20% of my final mark... now gotta study for the final exam (this gonna be a killer).

Thanks again guys... you saved my ass!.

Cookies?!! :D :love:

User avatar
[SD]happynoobing
Advanced Member
Posts: 2349
Joined: Thu Jan 04, 2007 2:06 am
Quick Reply: Yes
Location: Off Topic

Post by [SD]happynoobing »

lol dont ever doubt cruor when it comes to programming xD
Image

Post Reply

Return to “Off Topic Lounge”