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

The Geniuses' Thread

1121315171830

Comments

  • Registered Users, Registered Users 2 Posts: 656 ✭✭✭Richard Cranium


    Could anyone tell me if there's a simple way to ensure that an input in C is a natural number? As part of a programme I'm writing I need to allow the user to select how many decimal places to display in the results of a later calculation, and catch any invalid inputs.

    This is the relevant bit (so far), but it's not working for me:
    //Number of decimal places to be displayed
        printf("Enter number of decimal places to be displayed\n");
        cond=1;
        while (cond){
        scanf("%lf",&dec);
        if((dec<1)||(dec%1)!==0){
                 printf("Entry must be a strictly positive natural number\n Please try again\n");
                 }
                 else{ 
                 cond=0;
                 }
                 }
    

    The error being thrown up says "invalid operands of types `double' and `int' to binary `operator%'


  • Posts: 0 [Deleted User]


    Could anyone tell me if there's a simple way to ensure that an input in C is a natural number? As part of a programme I'm writing I need to allow the user to select how many decimal places to display in the results of a later calculation, and catch any invalid inputs.

    This is the relevant bit (so far), but it's not working for me:
    //Number of decimal places to be displayed
        printf("Enter number of decimal places to be displayed\n");
        cond=1;
        while (cond){
        scanf("%lf",&dec);
        if((dec<1)||(dec%1)!==0){
                 printf("Entry must be a strictly positive natural number\n Please try again\n");
                 }
                 else{ 
                 cond=0;
                 }
                 }
    

    The error being thrown up says "invalid operands of types `double' and `int' to binary `operator%'

    There are a few ways:

    -You could read it instead as a string, and check each character.

    -You could just declare dec as an int and let it give an error if the user inputs something else. Not elegant, but it works. >.>

    -Not sure if this will work, but define dec as a float, and then:
    if(dec == (int)dec) {
    ...
    }

    The most flexible way is probably the first. Fortunately, there's a website with far more clarity and detail than I could manage when talking about C.

    http://jk-technology.com/c/code/strtol.html


  • Registered Users, Registered Users 2 Posts: 656 ✭✭✭Richard Cranium


    There are a few ways:

    -You could read it instead as a string, and check each character.

    -You could just declare dec as an int and let it give an error if the user inputs something else. Not elegant, but it works. >.>

    -Not sure if this will work, but define dec as a float, and then:
    if(dec == (int)dec) {
    ...
    }

    The most flexible way is probably the first. Fortunately, there's a website with far more clarity and detail than I could manage when talking about C.

    http://jk-technology.com/c/code/strtol.html

    Thanks for that, I'm after making slightly more progress now. For the time being I've gone with the second method. If I have the time and the inclination I might try something more properly codey later on.


  • Registered Users, Registered Users 2 Posts: 708 ✭✭✭syncosised


    I just created a Python program that can automatically text someone over and over using webtext. I just need to find a good use for it now!


  • Registered Users, Registered Users 2 Posts: 4,305 ✭✭✭Chuchoter


    syncosised wrote: »
    I just created a Python program that can automatically text someone over and over using webtext. I just need to find a good use for it now!

    My brother says if your phone gets 205 texts really fast you're phone breaks. Pick someone you hate and you're away


  • Advertisement
  • Closed Accounts Posts: 6,919 ✭✭✭Grindylow


    My brother says if your phone gets 205 texts really fast you're phone breaks. Pick someone you hate and you're away

    LOLOL your brother lied.


  • Registered Users, Registered Users 2 Posts: 708 ✭✭✭syncosised


    205 texts is very specific! I'm sure there's some phone out there that'll crash under that. I think just the annoyance of getting lots of messages in a row would be enough!


  • Registered Users, Registered Users 2 Posts: 4,586 ✭✭✭sock puppet


    enhanced-buzz-3285-1288470266-5.jpg

    From the Rally to Restore Sanity. Lulz


  • Registered Users, Registered Users 2 Posts: 4,305 ✭✭✭Chuchoter


    Smart people, I need your help with some basic maths. How did they get first part of the question?
    2003q1pa.png

    Like what I would have said would be

    6y(2x)-3(x(x+4y))
    2x(x(x+4y))

    But thats obviously wrong...


  • Registered Users, Registered Users 2 Posts: 1,269 ✭✭✭cocoa


    what you've got is fine, they just canceled an x immediately. If you look at your fraction and factorise on top and on bottom, you'll see an x cancels, then you should be on the right track.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,269 ✭✭✭cocoa


    ermm, no it should work for any equation, you can take any value, multiply it by the same thing top and bottom (or, multiply and divide it by the same value) and it's still the same value, no trouble...


  • Registered Users, Registered Users 2 Posts: 4,305 ✭✭✭Chuchoter


    OK so I think the first x in (x)(x+4y) cancels with the one on the bottom, but why would that one cancel as opposed to the (x+4y) is also on the top and bottom?
    EDIT: Nevermind, my brain has unfrozen, it all makes sense now! :P Thanks everyone!


  • Moderators, Education Moderators, Motoring & Transport Moderators Posts: 7,396 Mod ✭✭✭✭**Timbuk2**


    We got our Microeconomics Mid Terms back on Friday - I got 80/100, which I am delighted with.

    As our results were given in tabular form, I had no choice but to import it into R, and draw various graphs and whatnot.

    The mean was 49.65, and the median was 50 (nice!). The standard deviation is 14.72966.

    I'm not sure how to draw a frequency curve in R, but I attempted doing it using the plot function, and this is what I got
    resultsg.jpg
    What I did was:
    Defined 'tab' as a table of the results data (called results)
    tab<-table(results)
    Then to plot the frequency curve
    plot(tab, lwd=2, type="l")

    Is there a correct way of doing this, which gives a smoother curve?

    My score was 2.06 standard deviations above the mean. Providing the data is normally distributed (which it isn't, but it's close!) that would mean I am in the 97th percentile (but I doubt it's that high). Using Chebyshev's Rule, I am higher than at least 76% of the data - but there's a big difference between those two figures.

    For the lulz, I drew a boxplot, using boxplot(results$V1, horizontal=TRUE)
    boxplot.jpg

    Hmm, a bit confused as to the difference in percentile figures as well as how to draw a frequency curve (the bell-shaped one, if the data was normally distributed). Still though - who doesn't love statistics? R is brilliant! I have too much free time :o

    Note: This was an MCQ, of 20 questions, hence why the figures all seem to be nice and round (e.g. the IQR above in the boxplot) - they are all in multiples of 5


  • Registered Users, Registered Users 2 Posts: 4,944 ✭✭✭Jay P


    Man, I kinda wish I'd gone to more than two R practicals last year....


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    Jay P wrote: »
    Man, I kinda wish I'd gone to more than two R practicals last year....

    I've gone to one so far, haven't looked at it since :(


  • Registered Users, Registered Users 2 Posts: 656 ✭✭✭Richard Cranium


    I went to all of mine last year. They didn't help half as much as a couple of Google searches did.:pac:

    My stats lecturer sent us a free book on R through Blackboard last year. I never bothered downloading it and now I can't remember what it was called. Ah well, it wasn't much help either anyway.


  • Registered Users, Registered Users 2 Posts: 4,944 ✭✭✭Jay P


    I went to all of mine last year. They didn't help half as much as a couple of Google searches did.:pac:

    My stats lecturer sent us a free book on R through Blackboard last year. I never bothered downloading it and now I can't remember what it was called. Ah well, it wasn't much help either anyway.

    An Introduction to Statistics Using R, maybe? That sounds right to me....

    *shrug*


  • Posts: 0 [Deleted User]


    We got our Microeconomics Mid Terms back on Friday - I got 80/100, which I am delighted with.

    As our results were given in tabular form, I had no choice but to import it into R, and draw various graphs and whatnot.

    The mean was 49.65, and the median was 50 (nice!). The standard deviation is 14.72966.

    I'm not sure how to draw a frequency curve in R, but I attempted doing it using the plot function, and this is what I got
    resultsg.jpg
    What I did was:
    Defined 'tab' as a table of the results data (called results)
    tab<-table(results)
    Then to plot the frequency curve
    plot(tab, lwd=2, type="l")

    Is there a correct way of doing this, which gives a smoother curve?

    My score was 2.06 standard deviations above the mean. Providing the data is normally distributed (which it isn't, but it's close!) that would mean I am in the 97th percentile (but I doubt it's that high). Using Chebyshev's Rule, I am higher than at least 76% of the data - but there's a big difference between those two figures.

    Hmm, a bit confused as to the difference in percentile figures as well as how to draw a frequency curve (the bell-shaped one, if the data was normally distributed). Still though - who doesn't love statistics? R is brilliant! I have too much free time :o

    The difference in percentiles is because Chebyshev's rule is a lower limit. It makes sense if you think about it. He has to make sure 76% of the data is within this number of s.d.'s. Probabilistically, it's likely, but not guaranteed, that around 95% of the data will be in this range, so your value is fine.

    As for the smooth frequency curve, you can get R to draw a normal distribution with mean mu and sd sigma by taking enough random normally distributed numbers with these parameters, and plotting them. (enough to make the graph smooth).

    You could alternately use the pdf for a normal distribution, and graph that, using the analytic formula.

    I think the way to get random normally distributed numbers is rnorm(). Type ?rnorm to find out the usage.


  • Registered Users, Registered Users 2 Posts: 7,962 ✭✭✭jumpguy


    Hey lads, need help with maths here.

    2010 paper 2 Q4 (c) which is here on page 3.

    I got the first part fine, but then me and the marking scheme have a disagreement.

    For part 2 of the c (prove it's a right angled triangle), I used the Cos Rule to find the angle BAC. I got CosBAC = 0 and hence the angle BAC = 90 degrees and it's a right angled triangle.

    Buuuut the marking scheme on page 54 begs to differ. Can you do it both ways or am I wrong?


  • Registered Users, Registered Users 2 Posts: 656 ✭✭✭Richard Cranium


    jumpguy wrote: »
    Hey lads, need help with maths here.

    2010 paper 2 Q4 (c) which is here on page 3.

    I got the first part fine, but then me and the marking scheme have a disagreement.

    For part 2 of the c (prove it's a right angled triangle), I used the Cos Rule to find the angle BAC. I got CosBAC = 0 and hence the angle BAC = 90 degrees and it's a right angled triangle.

    Buuuut the marking scheme on page 54 begs to differ. Can you do it both ways or am I wrong?

    The way it's done in the marking scheme is just Pythagoras' Theorem, which is the simplest way I suppose.

    a is the longest side, obviously enough, so they squared it.
    Then they got the sum of the square of the other two sides, and showed that this is equal to a^2. This happens if and only if the triangle is right angled, so the question is answered.

    Your way seems right, but a bit long winded. I'm too tired to do it out myself, but if you've shown that one of the angles in the triangle is right angled, you should get full marks.

    EDIT: I'm doing exactly what I despise my lecturers doing, and assuming that you're seeing everything that I see without the need for it to be explained. Fire away with questions if anything isn't as obvious as I think it is.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,269 ✭✭✭cocoa


    jumpguy wrote: »
    Buuuut the marking scheme on page 54 begs to differ. Can you do it both ways or am I wrong?

    Dude, the marking scheme doesn't beg to differ, check point 7 on page 4, it's not an exhaustive list of solutions. All this means is that the marking scheme can't tell you whether or not you're right, it just gives one of many possible right examples.

    Off the top of my head, applying the cos rule would be another perfectly valid solution, assuming you've gone through the steps correctly (can't imagine how you might not as it's just subbing into a formula but anyway...).

    That said, it's been many many years since I've seen or used the cos rule, so maybe have your teacher / someone else on here glance over it ;)

    EDIT : one obvious hole in your solution however is the fact that you have to guess which is the right angle or use trial and error. However in their solution they have to guess the longest side. A little bit of text at the start of your solution stating 'a is longest side, therefore BAC is candidate for right angle' might be worth having. (sorry for awkward wording...)


  • Posts: 0 [Deleted User]


    Such a simple question!

    I'm just noting connections - the values for the sides of the right-angled triangle are the beginnings of the standard proof of Euler's Last Theorem for the n=3 case.
    This is a theorem concerning whole numbers, so it's perhaps intuitive that r in part (iii) should be an integer.


    Now, the area of the triangle is one half of the dot product of the vectors BA and BC.

    Expand this using geometric identities to introduce the quantity b and the quantity r.

    I joke. This is probably how some college lecturers would tell you to do it. Obviously, it's needlessly complicated :P I didn't make it up though, it should work.


  • Moderators, Education Moderators, Motoring & Transport Moderators Posts: 7,396 Mod ✭✭✭✭**Timbuk2**


    In a C++ assignment I have to do, we are asked (as part of it) to ask the user how many values he/she wants to enter (maximum of 50), and then to store them into an array, of type integer.

    My question is what size do I make the array? Do I just create an array of size 50, or do I let some variable denote the size of the array, say 'size' and then create the array as int a[size];. What's the purpose of even saying maximum 50?


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    In a C++ assignment I have to do, we are asked (as part of it) to ask the user how many values he/she wants to enter (maximum of 50), and then to store them into an array, of type integer.

    My question is what size do I make the array? Do I just create an array of size 50, or do I let some variable denote the size of the array, say 'size' and then create the array as int a[size];. What's the purpose of even saying maximum 50?

    I'm not entirely sure as my brain's been poisoned with Java due to college ;_;, and I haven't done C++ in a while, but you might be able to do:
    cin >> size;
    int a[size]; // (or int *a = new int[50]; or something)
    

    I'd imagine that's what's expected if it works, but tbh I wouldn't be too surprised if it didn't let you, in which case you could use something from the Standard library (vectors are grand), use something like malloc() and free(), or just pre-define a maximum size (which would be the case if you haven't heard of the others yet :P).

    Edit: Yeah the above code does seem to work, I kinda realised just after posting that it would take less time to actually check than typing this post took :P


  • Moderators, Education Moderators, Motoring & Transport Moderators Posts: 7,396 Mod ✭✭✭✭**Timbuk2**


    Thanks! The above is what I have, and it does work, as it did for the last assignment we did (good 'oul Bubble sort), but I was just wondering that if he specified max 50, that it must come into the code somewhere (and he didn't teach us how to make maximum array sizes, or anything, so I thought it must be something to do with the initial size of the array).

    In the last assignment that I did, I asked the users how many values they wanted to enter, created a vector of that size, and asked the user to input the values one by one (cumbersome, but my knowledge is too basic to do it another way :o). He also specified max 50, but my program would have worked fine if the user entered 51, 150 or even 500.

    I haven't got the last one marked yet, so I'm not sure if I missed out on something!

    Thanks for the help!


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    Thanks! The above is what I have, and it does work, as it did for the last assignment we did (good 'oul Bubble sort), but I was just wondering that if he specified max 50, that it must come into the code somewhere (and he didn't teach us how to make maximum array sizes, or anything, so I thought it must be something to do with the initial size of the array).

    In the last assignment that I did, I asked the users how many values they wanted to enter, created a vector of that size, and asked the user to input the values one by one (cumbersome, but my knowledge is too basic to do it another way :o). He also specified max 50, but my program would have worked fine if the user entered 51, 150 or even 500.

    I haven't got the last one marked yet, so I'm not sure if I missed out on something!

    Thanks for the help!

    I assume he's just expecting you to make the array 50 long, but only use the first n values of it.
    But like, this way is better, as well as letting you use bigger numbers it means if they enter 25 it won't use an extra 100 bytes of space unnecessarily >.>.


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    Also, since this thread tends to have a lot of prograsming.

    The Tao of Programming :P


  • Moderators, Education Moderators, Motoring & Transport Moderators Posts: 7,396 Mod ✭✭✭✭**Timbuk2**


    First and foremost, I DO believe in God and all that.

    But let's assume that God does NOT exist, and that the world was created by 'chance'.

    I was thinking about this in statistics the other day. For example, if you have a large amount of data about a storm, and then you are given some value, e.g. wind speed, you have to determine how likely it is. If something odd happens, such as it being 3 standard deviations from the mean, then we reject it as the probability of it happening is very very slim.

    Now, if God doesn't exist, the probability that everything on this world was here, and 'works', without intervention is extremely slim. Does this not hint at the existence of God? I know there are factors such as the things that can survive on this planet are the ones that actually survived and thus carried on through generations, but in overall terms!

    Just a random thought, not that important and probably not suited to the Genius thread :P


  • Registered Users, Registered Users 2 Posts: 1,269 ✭✭✭cocoa


    Now, if God doesn't exist, the probability that everything on this world was here, and 'works', without intervention is extremely slim. Does this not hint at the existence of God? I know there are factors such as the things that can survive on this planet are the ones that actually survived and thus carried on through generations, but in overall terms!

    How do you calculate this probability exactly? All you've done here is stated it's extremely slim, why? What are you taking as accepted fact for the basis of the probabilities? Eg, if you take as fact there being an incredibly large amount of space (being a non entity, we can even say an infinite amount, yay!) and an incredibly large amount of matter (ok, not infinite, but still pretty huge) which formed into planets and stars and other fun things, even if the odds of something (hell, let's go ahead and say sentient life) happening on any particular planet are low, the probability of that thing happening on some planet out of the many are quite good.

    Just because an event has a low probability does not mean it didn't happen or will not happen. Some diseases have probabilities as low as 1 in 10^8, but they still occur thanks to our world population being so big.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,586 ✭✭✭sock puppet


    First and foremost, I DO believe in God and all that.

    But let's assume that God does NOT exist, and that the world was created by 'chance'.

    I was thinking about this in statistics the other day. For example, if you have a large amount of data about a storm, and then you are given some value, e.g. wind speed, you have to determine how likely it is. If something odd happens, such as it being 3 standard deviations from the mean, then we reject it as the probability of it happening is very very slim.

    Now, if God doesn't exist, the probability that everything on this world was here, and 'works', without intervention is extremely slim. Does this not hint at the existence of God? I know there are factors such as the things that can survive on this planet are the ones that actually survived and thus carried on through generations, but in overall terms!

    Just a random thought, not that important and probably not suited to the Genius thread :P



Advertisement