[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

C++ help / C++ reference needed

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 13
Thread images: 2

File: maxresdefault.jpg (78KB, 1280x720px) Image search: [Google]
maxresdefault.jpg
78KB, 1280x720px
So I had to learn C++ in a week despite the whole CS program at my school being in Java until this semester. We weren't allowed to use the standard library or any features in C++ 11 this include strings. The program would take in commands such as: end, load filename.txt, find word ith iteration(an int), and new (reset the data structure). I had work 5 days out of the 7 and I'm about to miss the deadline. To sanitize input I used:

scanf("%s", command) == 1) || (scanf("%s %s", command, word) == 2) || (scanf("%s %s %d", command, word, num) == 3)

that obviously didn't work at all; I'm wondering how you guys would handle taking inputs from different sizes.

Also if you have any great reference textbooks like C's K&R that'd be most helpful.

Sent from Gentoo.
>>
scanf() is c, not c++.
Also K&R is c, not c++.
just use stack overflow and this site:
http://www.cplusplus.com/forum/articles/6046/
>>
>>62399970
>We weren't allowed to use the standard library or any features in C++ 11 this include strings.
utterly disgusting. Strings were in C++ since forever though.

Just write C except use more iostreams so you don't go mad.
>>
>>62401096
>>62399970
Wait, I misread. You can't use C++ standard library? But you can use C standard library I'm sure.
>>
>>62399970
Read the string at one char at a time and use a dynamic array that each time it can't hold the next char from the input it just doubles the capacity. Although I have to say that your profs are retards for not teaching simple c first.
>>
>>62401063
>just use stack overflow
Please no
>>
Here's a screenshot of the piazza post. I'm pretty livid given that we were given a week to learn c++ when the only pre-req for the class was a Java data structures class. Like no company is going to tell me to not use strings or the standard library of a language; the class is being obtuse for the reason of being so. Needless to say 4% of my grade is gone now, because I wasn't able to do all this in a week when I had three other courses and work to think about.
>>
>>62403213
When we were doing exercises on C++ for OOP they didn't allow us STL but they allowed strings. In any case if this was an exercise in Java in which i guess you are more proficient, then how many lines of code or time it would take for you to do it ?
>>
For input I would use Scanner next line and then split the string by white space into an array; from there I would probably use a switch statement to make sure the input was valid. Then for like the load I would open a new file, catch the exception, throw the error and catch it properly. Then for my binary tree I would create custom nodes, based on the string using the comparable class, where I would store the index where the word is found in a list (the second part of the node). I'm not sure how long that would take it but doesn't sound as difficult as using character pointer arrays etc. It's not that I don't know what they're asking it's just the form of how they are asking it for me is ridiculously time consuming.
>>
I create two random char values. If it's -128 I force it to reroll.

I take the magnitude (float) of these two values. If that magnitude is <= 127, that those two values would create a point within a circle radius 127 centered on the origin. There is also a square with sides. 127 is the radius r.

There is a also a square which makes this circle perfectly inset. It has sides of 2r and an area of 4r*r.

The area of a circle is pi * r * r. The probability of two random chars lying within the circle is pi * r * r / ( 4 * r * r).

Or pi/4.

For every time this magnitude is less than 127, I increase a counter by one. I do this ten thousand times. I then take this counted value and divide it by 10,000 - which gives me an estimate on the probability. Then I multiply by 4.

No matter what, I get 3.11. Maybe 3.10, or 3.12. But mostly I get 3.11 which is not at all pi. What am I doing wrong here?
>>
>>62403379
So what I did is just instead of using random numbers, use all values from -127 to 127. I guess you don't need to use random number generation for this method since if you use all values you would have the same effect you are trying to get with random number generation.

The result was 3.089.... which is still confusing because since I had never gotten below a 3.10, and I ran it around 12 times. Does this imply that there is a failure somewhere in random number generation because it prefers lower numbers (and thus a higher probability?)

But I then changed the range to from -10000 to 10000. The thing about this is you only take the counter of magnitudes within 10000 and divide by the number of attempts - this method eliminates the need to multiply by 4.

on to the important part: I calculated 3.1415901. Obviously not the fastest method, but i'm glad with how it turned out.

This is one more higher level of accuracy I could attain using euler's famous formula (that is, pi^2 / 6 = 1/(n^2).

The whole e^(ix) thing was discovered with Cotes (with a sign error if I recall correctly).
>>
Running both euler's sum and random processes for one million entries per point (500k*500k square and 1M points on the series) overnight, will keep you informed on the value of pi
>>
>>62403300
Post no. 62403368 was meant for you sorry
Thread posts: 13
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.