Difference between revisions of "Team 42 Contributions"

From CDOT Wiki
Jump to: navigation, search
(Created page with '==C/C++ FAQ== <br> '''Q:''' Can a functional pointer be used to point to an overloaded function? If so, which function will it call when the pointer is dereferenced and why? …')
 
 
(8 intermediate revisions by 2 users not shown)
Line 10: Line 10:
 
Back to your question. Call to Function Pointer will call an overloaded function with the same signature as the Function Pointer.  
 
Back to your question. Call to Function Pointer will call an overloaded function with the same signature as the Function Pointer.  
 
<br>'''Answer Submitted by:''' Team42<br>
 
<br>'''Answer Submitted by:''' Team42<br>
 +
 +
 +
 +
'''Q:''' How to visualize a multi-dimensional array? <br>
 +
'''A:''' The simplest way to do so for a 2 dimensional array is a table
 +
<source lang="cpp">
 +
0123
 +
4567
 +
7890
 +
</source>
 +
 +
It gets slightly more complex when it comes to 3 dimensional arrays
 +
*First what comes to minds of majority of people is a cube
 +
<source lang="cpp">
 +
            __________
 +
          /\444444444\
 +
  /\3\444444444\
 +
/\2\3\444444444\
 +
/\1\2\3\444444444\
 +
\0\1\2\/33333333\/
 +
\0\1\/22222222\/
 +
  \0\/11111111\/
 +
  \/00000000\/
 +
</source><br>
 +
(There are many disadvantages to using this form besides awkwardness!)
 +
 +
 +
*Another way is to display each of the layers as separate 2 dimensional arrays
 +
<source lang="cpp">
 +
111
 +
111
 +
111
 +
 +
222
 +
222
 +
222
 +
 +
333
 +
333
 +
333
 +
</source>
 +
 +
'''To be Continued (Feel free to add your ideas! :D)'''
 +
 +
Submitted by Team42
 +
<br><br>
 +
'''Q:''' How to get size of an array without storing the size anywhere? <br>
 +
'''A:''' There are several ways to get the length of an array without explicitly knowing its length. The C method is as follows
 +
<source lang="cpp">
 +
#define SIZEOF_ARRAY( a ) (sizeof( a ) / sizeof( a[ 0 ] ))
 +
</source>
 +
The above define macro can be called to find the size of an array in a C manner. This function exists in the std namespace.<br>
 +
One could also use the below code for a more effective (but c++ only) solution.
 +
<source lang="cpp">
 +
#include <iostream> using namespace std; 
 +
template <typename T, size_t N>
 +
 +
inline
 +
 +
size_t SizeOfArray( const T(&)[ N ] ) {
 +
 +
return N;
 +
 +
}
 +
 +
 +
int main() { 
 +
 +
const char s[] = "Hello world!";
 +
cout << "s[] is " << SizeOfArray( s );
 +
}
 +
</source>
 +
The good thing about the above method is not only its improved efficiency (most compilers will optimize the template function out of existance upon compilation) but the fact that it will not work with a pointer to an array, such as
 +
<source lang="cpp">
 +
const char* s = "Hello world!";  cout << "s is " << SizeOfArray( s )
 +
</source>
 +
Lastly, with C++11 there is an even better method of doing this: std::extent. More info on this trait class can be found [http://www.cplusplus.com/reference/std/type_traits/extent/ here].<br>Submitted by Team42.<br><br>
 +
 +
'''Q:''' How do you get the length of a file / read the entire file without explicitly knowing its length, and how do you use that data afterwards? <br>
 +
 +
'''A:''' There are several ways to read the contents of a file without knowing it's length and then split the result into usable parts.
 +
First, you could use a string class and load the contents of the entire file into the string object. At this point you can manipulate it as any other string object (not to be confused with a char array) by using methods such as find, substr, erase, etc as outlined [http://www.cplusplus.com/reference/string/string/ here]
 +
<source lang="cpp">
 +
std::ifstream in("myfile", ios::binary);
 +
std::stringstream buffer;
 +
buffer << in.rdbuf();
 +
std::string contents(buffer.str());
 +
</source>
 +
You could also use
 +
<source lang="cpp">
 +
inMyStream.seekg(0,std::ios_base::end);
 +
    std::ios_base::streampos end_pos = inMyStream.tellg();
 +
    return end_pos;
 +
</source>
 +
to get the length of the file without actually reading it, and then if needed read / append / modify it as required using a char array.. This method of getting a file's length can be used with char arrays / strings / vectors without problems.
 +
Lastly, you could also load the file into a vector and manipulate it as a vector object from that point onwards (more advanced topic) like so
 +
<source lang="cpp">
 +
std::ifstream ifs("foobar.txt", ios::binary);
 +
ifs.seekg(0, std::ios::end);
 +
std::ifstream::pos_type filesize = ifs.tellg();
 +
ifs.seekg(0, std::ios::beg);
 +
std::vector<char> bytes(filesize);
 +
ifs.read(&bytes[0], filesize);
 +
</source>
 +
For any of these methods, to be able to work with the data afterwords simply use a common delimiter when originally generating the file, and then you can use methods like[http://www.cplusplus.com/reference/clibrary/cstring/strtok/ strtok] (for char array), [http://www.cplusplus.com/reference/string/string/find/ .find] combined with[http://www.cplusplus.com/reference/string/string/substr/ .substr] (string class) or use it as a vector (if that's required).<br>Submitted by Team42.<br><br>
  
 
==[[Assignment 2 (Release 0.1): Q & A | Assignment 2]]==
 
==[[Assignment 2 (Release 0.1): Q & A | Assignment 2]]==
  
 
<br>
 
<br>
 
 
'''Q:''' Are we able to toggle the border's visibility or are we only able to toggle the frame's visibility? If yes, the constructor does not receive any information about the visibility of  the border, do we assume that the border is visible?<br>
 
'''Q:''' Are we able to toggle the border's visibility or are we only able to toggle the frame's visibility? If yes, the constructor does not receive any information about the visibility of  the border, do we assume that the border is visible?<br>
 
'''Question Submitted by:''' Gideon Thomas and  Marie Karimizadeh<br>
 
'''Question Submitted by:''' Gideon Thomas and  Marie Karimizadeh<br>
Line 39: Line 143:
 
</source>
 
</source>
  
So it's safe to assume that modifiers void row(int) and void col(int) will receive coordinates of top-left corner (row and col respectively) of a current frame relative to a parent frame.
+
So it's safe to assume that modifiers void row(int) and void col(int) will receive coordinates of top-left corner (row and col respectively) of the current frame relative to the parent frame.
<br>'''Answer Submitted by:''' 010101000110010101100001011011010011010000110010 <br><br>
+
<br>'''Answer Submitted by:''' 010101000110010101100001011011010011010000110010 <br>
 +
 
 +
 
 +
 
 +
'''Q:''' Since we can have a potential of unlimited frames within frames, how can we tell how many frames are in the console? (how far down in the frames within frames) <br>
 +
'''Question Submitted by:''' Wesley Hamilton and Joe Higginson<br>
 +
 
 +
'''A:''' You can use this simple function to calculate the number of parents within parents of the current frame:
 +
 
 +
<source lang="cpp">
 +
 
 +
int SomeClass::number_of_parents(){
 +
 
 +
    return (parent ? 1+parent->number_of_parents() : 0);
 +
 
 +
    //"parent" is a reference to the parent of the current frame
 +
 
 +
 +
 
 +
</source>
 +
 
 +
But you can't know how many children the current frame has, unless you create special class members to keep/calculate that number.
 +
<br>'''Answer Submitted by:''' [[Team 42 Contributions | Team0x2Au]] <br>
 +
 
 +
==[[Assignment 2 (Release 0.1): Q & A | Possible/Challenging Enhancements]]==
 +
 
 +
 
 +
*Implemented:
 +
**1)  Cross-platform
 +
**2)  Colors usable
 +
**3)  Cursor manipulation - upgraded from macro to function(hiding/unhiding)
 +
**4)  Child Tracking - maintain relationship between parent and children frames: remove children from the parent if child changes parent, adds child to parrent, provides references to every child and their number,
 +
***allows 2 ways destruction:
 +
****kind, when child parent address is assignmed to null and only parent is destroyed
 +
****hard, when parent and all its children are destroyed
 +
****(can be toggled! :P)
 +
**5)  Dumb word wrapping - Cut off string at the end of line and wrap it
 +
**6)  Smart word wrapping - Smart word wrapping - words are not cut off mid-word while wrapping
 +
**7)  Optimization - comparing to posted examples our code is more efficient
 +
**8)  Colors - you can add color to the test program if you're making your own main (updated, use struct instead of macro)     
 +
 
 +
*Proposed but not implemented:
 +
**1)  Files - Read string from file, use that string for editing, then write the result string to that file, overwriting original
 +
**2) Timer - to keep track of how long the user has been running your custom main (decided to make it a separate thread, but that would be too much for this assignment, as a result left it for a3 and a4)

Latest revision as of 21:07, 19 November 2012

C/C++ FAQ


Q: Can a functional pointer be used to point to an overloaded function? If so, which function will it call when the pointer is dereferenced and why?
Question Submitted by: Gideon Thomas and Marie Karimizadeh

A:Function Pointer can be used to point to any function with the same signature as its own.
"Signature is the information about a function that participates in overload resolution: its parameter-type-list... Function signatures do not include return type, because that does not participate in overload resolution." (Working Draft, Standard for Programming Language C++ 2005-10-19, p3, 1.3.11 signature)
In the case of overloaded functions the only thing they have in common is the name. Signatures are different, hence the same pointer to function can't be used for both of them.

Back to your question. Call to Function Pointer will call an overloaded function with the same signature as the Function Pointer.
Answer Submitted by: Team42


Q: How to visualize a multi-dimensional array?
A: The simplest way to do so for a 2 dimensional array is a table

0123
4567
7890

It gets slightly more complex when it comes to 3 dimensional arrays

  • First what comes to minds of majority of people is a cube
            __________	
           /\444444444\
	  /\3\444444444\
	 /\2\3\444444444\
	/\1\2\3\444444444\
	\0\1\2\/33333333\/
	 \0\1\/22222222\/
	  \0\/11111111\/
	   \/00000000\/

(There are many disadvantages to using this form besides awkwardness!)


  • Another way is to display each of the layers as separate 2 dimensional arrays
111
111
111

222
222
222

333
333
333

To be Continued (Feel free to add your ideas! :D)

Submitted by Team42

Q: How to get size of an array without storing the size anywhere?
A: There are several ways to get the length of an array without explicitly knowing its length. The C method is as follows

#define SIZEOF_ARRAY( a ) (sizeof( a ) / sizeof( a[ 0 ] ))

The above define macro can be called to find the size of an array in a C manner. This function exists in the std namespace.
One could also use the below code for a more effective (but c++ only) solution.

#include <iostream> using namespace std;  
template <typename T, size_t N> 

inline 

size_t SizeOfArray( const T(&)[ N ] ) {

	return N; 

}


int main() {  

	const char s[] = "Hello world!";
	cout << "s[] is " << SizeOfArray( s );
}

The good thing about the above method is not only its improved efficiency (most compilers will optimize the template function out of existance upon compilation) but the fact that it will not work with a pointer to an array, such as

const char* s = "Hello world!";  cout << "s is " << SizeOfArray( s )

Lastly, with C++11 there is an even better method of doing this: std::extent. More info on this trait class can be found here.
Submitted by Team42.

Q: How do you get the length of a file / read the entire file without explicitly knowing its length, and how do you use that data afterwards?

A: There are several ways to read the contents of a file without knowing it's length and then split the result into usable parts. First, you could use a string class and load the contents of the entire file into the string object. At this point you can manipulate it as any other string object (not to be confused with a char array) by using methods such as find, substr, erase, etc as outlined here

std::ifstream in("myfile", ios::binary);
std::stringstream buffer;
buffer << in.rdbuf();
std::string contents(buffer.str());

You could also use

inMyStream.seekg(0,std::ios_base::end);
    std::ios_base::streampos end_pos = inMyStream.tellg();
    return end_pos;

to get the length of the file without actually reading it, and then if needed read / append / modify it as required using a char array.. This method of getting a file's length can be used with char arrays / strings / vectors without problems. Lastly, you could also load the file into a vector and manipulate it as a vector object from that point onwards (more advanced topic) like so

std::ifstream ifs("foobar.txt", ios::binary);
ifs.seekg(0, std::ios::end);
std::ifstream::pos_type filesize = ifs.tellg();
ifs.seekg(0, std::ios::beg);
std::vector<char> bytes(filesize);
ifs.read(&bytes[0], filesize);

For any of these methods, to be able to work with the data afterwords simply use a common delimiter when originally generating the file, and then you can use methods likestrtok (for char array), .find combined with.substr (string class) or use it as a vector (if that's required).
Submitted by Team42.

Assignment 2


Q: Are we able to toggle the border's visibility or are we only able to toggle the frame's visibility? If yes, the constructor does not receive any information about the visibility of the border, do we assume that the border is visible?
Question Submitted by: Gideon Thomas and Marie Karimizadeh
A: Yes, we do get to toggle the border visibility by using the

 void bordered(bool);
method which "sets the visibility of the border to the value received". The frame has no border if it is a fullscreen frame, should be safe to assume it has a border if it is not fullscreen (unless otherwise specified).

Answer Submitted by: Team42

Q: In the functions void row(int) and void col(int), are we receiving the values of row and column respectively that are relative to the parent frame or relative to the console screen?
Question Submitted by: Gideon Thomas and Marie Karimizadeh
A: In a CFrame class description most modifiers came in pairs with queries, like

void row(int) - sets the top row to the value received

int row() const - returns the top row position relative to the parent frame, if any; 0 if fullsreen



void col(int) - sets the left column to the value received

int col() const - returns the left column position relative to the parent frame, if any; 0 if fullsreen

So it's safe to assume that modifiers void row(int) and void col(int) will receive coordinates of top-left corner (row and col respectively) of the current frame relative to the parent frame.
Answer Submitted by: 010101000110010101100001011011010011010000110010


Q: Since we can have a potential of unlimited frames within frames, how can we tell how many frames are in the console? (how far down in the frames within frames)
Question Submitted by: Wesley Hamilton and Joe Higginson

A: You can use this simple function to calculate the number of parents within parents of the current frame:

int SomeClass::number_of_parents(){

    return (parent ? 1+parent->number_of_parents() : 0);

    //"parent" is a reference to the parent of the current frame

}

But you can't know how many children the current frame has, unless you create special class members to keep/calculate that number.
Answer Submitted by: Team0x2Au

Possible/Challenging Enhancements

  • Implemented:
    • 1) Cross-platform
    • 2) Colors usable
    • 3) Cursor manipulation - upgraded from macro to function(hiding/unhiding)
    • 4) Child Tracking - maintain relationship between parent and children frames: remove children from the parent if child changes parent, adds child to parrent, provides references to every child and their number,
      • allows 2 ways destruction:
        • kind, when child parent address is assignmed to null and only parent is destroyed
        • hard, when parent and all its children are destroyed
        • (can be toggled! :P)
    • 5) Dumb word wrapping - Cut off string at the end of line and wrap it
    • 6) Smart word wrapping - Smart word wrapping - words are not cut off mid-word while wrapping
    • 7) Optimization - comparing to posted examples our code is more efficient
    • 8) Colors - you can add color to the test program if you're making your own main (updated, use struct instead of macro)
  • Proposed but not implemented:
    • 1) Files - Read string from file, use that string for editing, then write the result string to that file, overwriting original
    • 2) Timer - to keep track of how long the user has been running your custom main (decided to make it a separate thread, but that would be too much for this assignment, as a result left it for a3 and a4)