Trying to do this horrible horrible extra credit assignment for a class and i noticed how much i suck/hate programming. Not to get into to much detail about the extra credit assignment but it involves rewriting a code using loops.
So my 1st question for anyone who can answer it for me is ... what does \n mean and/or do for a line of code?
Replies
\n = new line
\r = return carriage
\t = tab
for instance(in python)
print "hello\nworld"
would look like:
hello
world
some common ones are \n for newline, \t for tab-space, and \\ which simply goes to \ (to avoid the code trying to escape the next character in the string after the initial \).
*
***
*****
***
*
EDIT: the * should all be aligned in the center but it didn't come out that way.
like this diamond here, using loops.
i am extremely terrible with loops and arrays. So forgive me for asking this dumb question but ...
int _tmain(int argc, _TCHAR* argv[])
{
for(int i = 0; i < 7; i++){
string Display;
cout << i << "*";
do{
} while(true);
}
return 0;
}
does this look ok? am i on the right track? any tips on what to do afterwards?
good luck though!
int _tmain(int argc, _TCHAR* argv[]) { for(int i = 0; i < 7; i++){ string Display; cout << i << "*"; do{ } while(true); } return 0; }if you're meant to have a dynamic "diamond" you want the maximum number as your input, save that number as an integer. then solve the number of lines necessary to reach that number so your program will print '*' at line one in the position max_stars / 2, then expand towards the middle and regress afterwards. if this doesn't make sense i could write it for you
#include <iostream> using std::cout; using std::endl; void diamond(int size) { int spaces = size / 2; // loop over the number of lines for (int i = 0; i != size; ++i) { // loop to write whitespaces for each line for (int j = abs(spaces); j != 0; --j) cout << " "; // loop to write * for each line for (int k = 0; k != (size - abs(spaces) * 2); ++k) cout << "*"; --spaces; // go to next line and flush the stream cout << endl; } } int main() { // call the function diamond() // it works as it's meant if size is odd diamond(5); return 0; }If you need some references for C++, try the sites:
http://www.learncpp.com/
http://www.cplusplus.com/
Good luck!