- C - 5 Updates
- Functions and the linker - 4 Updates
- didactics of reference parameters[Supersedes] - 6 Updates
- What does this do ??? - 2 Updates
- Use of decltype - 1 Update
- Inheritance-type enum - 2 Updates
- What does this do ??? - 5 Updates
"Merve Tektaş" <mervetektas1995@gmail.com>: Mar 16 11:05AM -0700 1. Design an algorithm to print hello world to the screen. a. Write pseudo code of your solution first. (10 pts) b. Write the c code for your solution. (20 pts) 2. Design an algorithm that takes an integer value from the user and notifies the user either this number is even or odd. a. Write pseudo code of your solution first. (20 pts) b. Write the c code for your solution. (20 pts) 3. (30pts) Write the c code of the algorithm given in the slide 18 of 03.ppt. |
Gareth Owen <gwowen@gmail.com>: Mar 16 06:12PM > 1. Design an algorithm to print hello world to the screen. Call echo with the arguments "hello" and "world" > a. Write pseudo code oef your solution first. (10 pts) Call "echo hello world" > b. Write the c code for your solution. (20 pts) system("/bin/echo hello world"); |
Wouter van Ooijen <wouter@voti.nl>: Mar 16 07:44PM +0100 Op 16-Mar-16 om 7:12 PM schreef Gareth Owen: > Call "echo hello world" >> b. Write the c code for your solution. (20 pts) > system("/bin/echo hello world"); This is the c.l.c++, so I assume that for lack of a C++ shell he must use the C shell ;) Wouter |
red floyd <no.spam@its.invalid>: Mar 16 11:51AM -0700 On 3/16/2016 11:05 AM, Merve Tektaş wrote: > a. Write pseudo code of your solution first. (20 pts) > b. Write the c code for your solution. (20 pts) > 3. (30pts) Write the c code of the algorithm given in the slide 18 of 03.ppt. 1. This is comp.lang.c++, not comp.lang.c, so you are off-topic. 2. The answer to all of your questions may be found at: https://www.cs.rit.edu/~mjh/docs/c++-faq/how-to-post.html#faq-5.2 |
Jorgen Grahn <grahn+nntp@snipabacken.se>: Mar 16 06:58PM On Wed, 2016-03-16, Merve Tekta? wrote: > 1. Design an algorithm to print hello world to the screen. An odd choice of words, by the way: "algorithm" normally means roughly "a practical way of implementing a mathematical function". The function here is little more than a constant:, "hello world". > a. Write pseudo code of your solution first. (10 pts) > b. Write the c code for your solution. (20 pts) /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid>: Mar 16 09:38AM +0100 > The above program prints »1« here. > Does the above program have undefined behavior because the > function »s« is just being declared but not defined anywhere? Your compiler can help you. Mine says: warning: the address of 'const string s()' will always evaluate as 'true' -- Alain. |
Juha Nieminen <nospam@thanks.invalid>: Mar 16 09:13AM > #include <iostream> /* ::std::cout */ > #include <ostream> /* << */ While you are at it, why don't you also include <ios> and <streambuf>? You know, just in case. You never know. --- news://freenews.netfront.net/ - complaints: news@netfront.net --- |
legalize+jeeves@mail.xmission.com (Richard): Mar 16 04:19PM [Please do not mail me a copy of your followup] ram@zedat.fu-berlin.de (Stefan Ram) spake the secret code >#include <string> /* ::std::string */ >int main() { ::std::string const s(); ::std::cout << '>' << s << "<\n"; } > The above program prints »1« here. How does it even link? You have referenced something that is undefined, you should get undefined symbol for 's'. This is exactly what I expect to get and do indeed get from MSVC. Who knows WTF gcc is doing, but it successfully links and prints some junk. IMO, this is a bug in gcc. You can't print the address of something that isn't defined. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
red floyd <no.spam@its.invalid>: Mar 16 11:48AM -0700 On 3/16/2016 2:13 AM, Juha Nieminen wrote: >> #include <ostream> /* << */ > While you are at it, why don't you also include <ios> and <streambuf>? > You know, just in case. You never know. I believe that was *technically* required in C++03. Practically, iostream always included ostream. I think C++11 fixed this. |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Mar 16 04:37AM On 16/03/2016 03:19, Stefan Ram wrote: > to /call/ such functions, not to /write/ them. What do I > need to teach about references in this case, so that errors > in the client's code are avoided?) Your habit of Typing ::std::foo instead of std::foo is both really annoying and absurd. /Flibble |
Juha Nieminen <nospam@thanks.invalid>: Mar 16 09:27AM > when he wants to learn just enough to be able to /call/ > pre-defined functions with reference parameters, but does > not need to be able to write such functions himself? I'm trying to think of any way that calling a function that takes an object by value is different from calling a function that takes the object by const reference, but even if there is some situation where it's different (from the caller's perspective), I'm drawing a blank. I suppose that if the object itself cannot be copied, then it makes a difference, but with std::string there is no such problem. That's the only thing I can think of. --- news://freenews.netfront.net/ - complaints: news@netfront.net --- |
Reinhardt Behm <rbehm@hushmail.com>: Mar 16 09:02PM +0800 Juha Nieminen wrote: > I suppose that if the object itself cannot be copied, then it makes a > difference, but with std::string there is no such problem. That's the only > thing I can think of. If the object is large copying it will be a performance bottleneck and if it is really large the copy for call by value will not be possible because of not enough memory. -- Reinhardt |
Jerry Stuckle <jstucklex@attglobal.net>: Mar 16 09:03AM -0400 On 3/16/2016 5:27 AM, Juha Nieminen wrote: > difference, but with std::string there is no such problem. That's the only > thing I can think of. > --- news://freenews.netfront.net/ - complaints: news@netfront.net --- If the called function needs a mutable object (but doesn't want to change the original), it can either take a copy or make a copy of a constant reference. Either way, it needs to make a copy. But if it doesn't require a mutable copy, using a constant reference is obviously more efficient. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ================== |
Gareth Owen <gwowen@gmail.com>: Mar 16 05:41PM > by value is different from calling a function that takes the object by > const reference, but even if there is some situation where it's different > (from the caller's perspective), I'm drawing a blank. Anything "mutable" will be one case... Alternatively: std::string s("I'm a global\n"); bool is_global_s(const std::string& str) { return (&s == &str); } ..vs.. bool is_global_s(const std::string str) { return (&s == &str); } |
woodbrian77@gmail.com: Mar 16 11:22AM -0700 On Tuesday, March 15, 2016 at 11:38:17 PM UTC-5, Mr Flibble wrote: > Your habit of Typing ::std::foo instead of std::foo is both really > annoying and absurd. > /Flibble I don't do that to be annoying. I do it because it's the responsible thing to do. Brian Ebenezer Enterprises - In G-d we trust. http://webEbenezer.net |
"Öö Tiib" <ootiib@hot.ee>: Mar 16 12:23AM -0700 On Tuesday, 15 March 2016 23:36:59 UTC+2, Bob Langelaan wrote: > } > And I have figured out what is happening by varying whether true or false is assigned or statement is commented out. > If assigned false, all 100 bools become false; if assigned true, only the first element becomes true, the remainder become false; if commented out the 100 bools have seemingly random values (memory in bArray is not initialized in other words). Yes, it is aggregate so when initialized it will be default-initialized (so falses) to end and when not initialized then it will be uninitialized. |
Bob Langelaan <bobl0456@gmail.com>: Mar 16 10:23AM -0700 On Wednesday, March 16, 2016 at 12:24:14 AM UTC-7, Öö Tiib wrote: > > If assigned false, all 100 bools become false; if assigned true, only the first element becomes true, the remainder become false; if commented out the 100 bools have seemingly random values (memory in bArray is not initialized in other words). > Yes, it is aggregate so when initialized it will be default-initialized > (so falses) to end and when not initialized then it will be uninitialized. But in my mind an entity can only be "initialized" when it is defined. Therefore the statement: bArray = { false }; does not initialize bArray because bArray was previously defined. I am assuming that what is happening that the { false } value on the right of the assignment statement is being converted to a temporary std::array<bool, 100> object which is initialized to { false }. This temporary object is then assigned to bArray. Is my assumption correct? Thanks, Bob |
SG <s.gesemann@gmail.com>: Mar 16 06:33AM -0700 On Friday, March 11, 2016 at 12:56:48 PM UTC+1, Paul wrote: > std::vector<int> v{1}; > std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()); > Why does this fail to compile? If v[0] is replaced by 5, it does compile. My thinking is that decltype(v[0]) means same type as v[0] which means same type as 1 which is int and that therefore std::sort(v.begin(),v.end(), std::greater<int>()); should be executed. decltype is kind of overloaded. IIRC there are three cases to consider: (1) It gives you the *declared* type of some item. (2) It gives you the return type of whatever function you called. (3) It gives you the type and "lvalueness" of an arbitrary expression: T& for lvalue expressions of type T, T for rvalue expressions of type T An extra set of parentheses surrounding decltype's argument basically forces case 3. Examples: struct S { int m; } int i = 42; int& L = i; int&& R = std::move(i); const S s = { 99 }; std::vector<double> vec = { 3.14 }; // Examples for case 1: "declared type" decltype(i) // int decltype(L) // int& decltype(R) // int&& decltype(s.m) // int (S::m is not declared const) // Examples for case 3: "type and lvalueness of expression" decltype(+i) // int decltype((i)) // int& decltype((L)) // int& decltype((R)) // int& decltype((s.m)) // const int& (s is const, so is s.m) decltype((std::move(i))) // int // Examples for case 2: "return type of function" decltype(std::move(i)) // int&& decltype(std::rand()) // int decltype(vec[0]) // double& > Where am I going wrong? What should be done in practice if the type of v[0] is not simply int but a very complex type, and I want to avoid repeating the name of the type? I suppose typedef is the solution, but I thought decltype was used to solve this type of problem, too. You could use typename std::decay<decltype(v[0])>::type // C++11, or std::decay_t<decltype(v[0])> // C++14 The type transformation trait std::decay basically gets rid of references as well as const and volatile. std::decay_t is a templated type alias since C++14 which makes this shorter. But if you're OK with reling on C++14, you could just as well write std::sort(v.begin(), v.end(), std::greater<>()); This creates a std::greater<void> which is specialized to have a generic function call operator since C++14. :-) Cheers! sg |
JiiPee <no@notvalid.com>: Mar 16 12:27AM Is it possible to get a behaviour of "inherited" enum? What I want is, that I have a class, something like: enum animal_type .... class Dog { public: animal_type GetType() { return animal_type::Dog; } }; class Cat { public: animal_type GetType() { return animal_type::Dog; } }; and later on if I create an elephant: class Elephant { public: animal_type GetType() { return animal_type::Elephant; } }; So how to dynamically add elements to an enum or simulate it somehow so that when adding a new class I do not need to touch the original enum definition but I can add the type inside the new class. I want to be able to call a function: void CheckAnimal(animal_type a) { if(a == animal_type::Elephant) ... } So all animals have the same type (like animal_type). I know I could create integer constants for each animal type in each class, but then they would be integer type which is "weak"... I would like to have something similar to enum or class-type. |
JiiPee <no@notvalid.com>: Mar 16 01:29PM Interesting idea, have to think about this On 16/03/2016 00:42, Stefan Ram wrote: > #include "all_animals.h" > #undef INCLUDE_FOR_ID_WITH_COMMA > TOP_SENTINEL } what is TOP_SENTINEL here? |
ram@zedat.fu-berlin.de (Stefan Ram): Mar 15 11:27PM >Are you saying that the last statement assigns all 100 bool >values in bArray to false? An object of type »::std::array« is an aggregate. 2016: 8.5.1p7: »If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its default member initializer (9.2) or, if there is no default member initializer, from an empty initializer list (8.5.4).« When an object of the type »bool« is initialized from an empty initializer list, this is a value-initialization (2016: 8.5.4p3.9). In the case of the type »bool«, value- initialization means zero-initialization (2016: 8.5p8.4), which means it is initialized to the value obtained by converting the integer literal 0 (zero) to »bool« (8.5p6), i.e., to »false«. |
ram@zedat.fu-berlin.de (Stefan Ram): Mar 16 12:42AM >animal_type GetType() { return animal_type::Dog; } >animal_type GetType() { return animal_type::Elephant; } You would have a file dog.h /* grep_tag::is_animal */ #ifdef INCLUDE_FOR_ID_WITH_COMMA Dog,
Subscribe to:
Post Comments (Atom)
|
No comments:
Post a Comment