Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c programming help

  • 06-05-2011 1:54pm
    #1
    Registered Users Posts: 49


    is there anybody here that can program in c? if so if i post a couple of questions would there be any chance people could help me do it? i dont want the program just a few tips and tricks?
    im studying mathematical sciences and programming is giving me a lot of hassle. so thanks in advance.


Comments

  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    joey12345 wrote: »
    is there anybody here that can program in c? if so if i post a couple of questions would there be any chance people could help me do it? i dont want the program just a few tips and tricks?
    im studying mathematical sciences and programming is giving me a lot of hassle. so thanks in advance.

    What are your questions?


  • Registered Users Posts: 49 joey12345


    well theres a lot that im struggling with heres one for example

    use arrays to write a programme that given a number k will print out the first k rows of pascals triangle. the jth row of pascals triangle has j numbers. the ith entry is found by the sum of the ith and (i-1)th entries in the previous row.

    write a program to calculate the following function

    f(x)=
    x^2 x<=0
    2x/pie 0<x<=pie/2
    sinx pie/2<=x<=pie
    (x-pie)^3 x>= pie

    what are the values of f(2.5), f(f(0.25)), f(f(f(-1.5)))?

    i have a good feeling that the second one will be on the paper at the end of the month it seems to come up a lot.


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    joey12345 wrote: »
    well theres a lot that im struggling with heres one for example

    use arrays to write a programme that given a number k will print out the first k rows of pascals triangle. the jth row of pascals triangle has j numbers. the ith entry is found by the sum of the ith and (i-1)th entries in the previous row.

    write a program to calculate the following function

    f(x)=
    x^2 x<=0
    2x/pie 0<x<=pie/2
    sinx pie/2<=x<=pie
    (x-pie)^3 x>= pie

    what are the values of f(2.5), f(f(0.25)), f(f(f(-1.5)))?

    i have a good feeling that the second one will be on the paper at the end of the month it seems to come up a lot.

    OP, do you have any programming experience in C? Even if you have none, you should pick it up in no time if you know a thing or two about maths. The pascal Triangle is a well defined algorithm, so it's just a matter of converting the steps for that algorithm into a C function and then simply giving that function an input parameter for the number of rows during runtime. Can you print out the questions as they appear on the sheet if possible? i don't mind giving you a hand.


  • Registered Users Posts: 156 ✭✭MoogPoo


    hi threads a week old, are you still looking for help with them?


  • Registered Users Posts: 49 joey12345


    MoogPoo wrote: »
    hi threads a week old, are you still looking for help with them?

    hey sorry didnt see your reply there and yeah im stil looking for help i think the first one wont be coming up that was just an example but the second one id say theres like a 85% percent chance itll come up im just dont have a breeze how to go about it ?


  • Advertisement
  • Registered Users Posts: 65 ✭✭hevsuit


    Is the second question f(x) one function only, or does each line signify a separate function denoted by f(x)?


  • Registered Users Posts: 49 joey12345


    hevsuit wrote: »
    Is the second question f(x) one function only, or does each line signify a separate function denoted by f(x)?

    each line is a seperate function denoted by f(x) man.

    im pretty sure this will be on the exam and so will a program to get nCr. i can do the nCr one no probs just this one i cant at all.


  • Registered Users Posts: 65 ✭✭hevsuit


    joey12345 wrote: »
    each line is a seperate function denoted by f(x) man.

    im pretty sure this will be on the exam and so will a program to get nCr. i can do the nCr one no probs just this one i cant at all.

    #include <stdio.h>
    #include <math.h>

    float pie = 3.141592654;

    int function1(int x){
    if(x<=0)
    return (x*x) ; //returns the square of x.
    else
    printf("\n x must be less than or equal to '0'");
    return 0;
    }

    int function2(int x){
    if(0<x<=(pie/2))
    return((2*x)/pie);
    else
    printf("x must be >0 and <= pie/2");

    return 0;
    }

    int function3(int x){

    if((pie/2)<=x<=pie)
    return sin(x);
    else
    printf("x must be >=pie/2 and <= pie");

    return 0;
    }




    There are the first 3 functions done here, i havent tested it , but it looks right. I dont know if you need to tie them together in a main(). try the last one yourself. If you need help, just say.


  • Registered Users Posts: 49 joey12345


    hevsuit wrote: »
    #include <stdio.h>
    #include <math.h>

    double pie = 3.141592654;

    int function1(int x){
    if(x<=0)
    return (x*x) ; //returns the square of x.
    else
    printf("\n x must be less than or equal to '0'");
    return 0;
    }

    int function2(int x){
    if(0<x<=(pie/2))
    return((2*x)/pie);
    else
    printf("x must be >0 and <= pie/2");

    return 0;
    }

    int function3(int x){

    if((pie/2)<=x<=pie)
    return sin(x);
    else
    printf("x must be >=pie/2 and <= pie");

    return 0;
    }




    There are the first 3 functions done here, i havent tested it , but it looks right. I dont know if you need to tie them together in a main(). try the last one yourself. If you need help, just say.

    whoa thanks a million man i really appreciate that. it will have to be tied together in a main() but that will just be printf's with like please enter a value for x. and things like that im just studying a different subject here and actually making some headway but i will definitely try it out later on today and ill try tie them into a main. but i warn you im really bad at programming. i was really good and the basic stuff but then when functions and structures and arrays and things like that came into it i was lost hence why im repeating it but ill give it a shot later on and hit ya back k thanks again man


  • Registered Users Posts: 49 joey12345


    hevsuit wrote: »
    #include <stdio.h>
    #include <math.h>

    double pie = 3.141592654;

    int function1(int x){
    if(x<=0)
    return (x*x) ; //returns the square of x.
    else
    printf("\n x must be less than or equal to '0'");
    return 0;
    }

    int function2(int x){
    if(0<x<=(pie/2))
    return((2*x)/pie);
    else
    printf("x must be >0 and <= pie/2");

    return 0;
    }

    int function3(int x){

    if((pie/2)<=x<=pie)
    return sin(x);
    else
    printf("x must be >=pie/2 and <= pie");

    return 0;
    }




    There are the first 3 functions done here, i havent tested it , but it looks right. I dont know if you need to tie them together in a main(). try the last one yourself. If you need help, just say.


    ok feck it ill try it now cause i really wanna know if it will work.


    so it'll be


    int function4(int x)
    {
    if(x>=pie)

    return pow(x-pie,3);

    else

    printf("x must be less than pie");

    return 0;
    }

    hows that looking?


  • Advertisement
  • Registered Users Posts: 65 ✭✭hevsuit


    joey12345 wrote: »
    whoa thanks a million man i really appreciate that. it will have to be tied together in a main() but that will just be printf's with like please enter a value for x. and things like that im just studying a different subject here and actually making some headway but i will definitely try it out later on today and ill try tie them into a main. but i warn you im really bad at programming. i was really good and the basic stuff but then when functions and structures and arrays and things like that came into it i was lost hence why im repeating it but ill give it a shot later on and hit ya back k thanks again man


    Ya , it requires some late monster energy filled nites to get good at it :p.:pac:
    In your main() you could print the names of the functions. i.e. 'function1.
    function2....', and then use a scanf followed by a switch-case statement to call the appropriate function.

    Good luck anyway....more exams tmrw:(

    Alex


  • Registered Users Posts: 65 ✭✭hevsuit


    joey12345 wrote: »
    ok feck it ill try it now cause i really wanna know if it will work.


    so it'll be


    int function4(int x)
    {
    if(x>=pie)

    return pow(x-pie,3);

    else

    printf("x must be less than pie");

    return 0;
    }

    hows that looking?


    I've never used the Pow() function before, but otherwise its good, although i thought x must be greater than pie.?


  • Registered Users Posts: 49 joey12345


    hevsuit wrote: »
    I've never used the Pow() function before, but otherwise its good!


    coolio man kk ill try get this program up and running. thanks again for that help man.

    your doing exams aswell? mine start next tuesday ive got programming and linear algebra really not looking forward to the programming at all. but with this step function and the nCr hopefully ill be ok.


  • Registered Users Posts: 49 joey12345


    #include<stdio.h>
    #include<math.h>

    float pie=3.14;

    float f(float x);
    int main()
    {
    float num;
    printf("please enter a value for x--->");
    scanf("%f", &num);
    printf("your answer is %f", float f(num));

    }

    float f(float x)
    {
    if (x<=0)
    {
    return pow(x,2);
    }

    if (0<x&&x>pie/2)
    {
    return (2*x)/pie;
    }

    if (pie/2<=x&&x<=pie)
    {
    return sin(x);
    }

    if (x>=pie)
    {
    return pow(x-pie,3);
    }

    return 0;
    }


    k bear in mind that programming is not at all my strong point but can anyone see there errors here?
    its suppose to be a piecewise function and its just not running i thought i should try it with just f(x) to begin with before i delved into f(f(x)) and so on but im stuck already any suggestions??


  • Registered Users Posts: 49 joey12345


    had me exam today and it went really well so this thread can be closed. thanks for the help guys


Advertisement