#include <iostream> #include <cstdlib> #include <cmath> //for the abs whose argument is an int using namespace std; int main() { const int width = 300; //number of columns of pixels const int height = width * 2 / 3; //number of rows of pixels const int xmax = width / 2; //place origin at center const int ymax = height / 2; const int xmin = xmax - width; const int ymin = ymax - height; cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum //Draw a simplified United Kingdom flag. for (int y = ymax - 1; y >= ymin; --y) { //top to bottom for (int x = xmin; x < xmax; ++x) { //left to right if (abs(x) <= 10 || abs(y) <= 10) { //red stripes along axes cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } else if (abs(x) <= 20 || abs(y) <= 20) { //red stripes have white border cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else if (abs(abs(y) - .5 * abs(x)) <= 5) { //This pixel is close to the line y == .5 * x. //red diagonal stripe cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } else if (abs(abs(y) - .5 * abs(x)) <= 15) { //red diagonal stripe has white border cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else { //blue background cout << 0 << "\t" << 0 << "\t" << 255 << "\n"; } } } return EXIT_SUCCESS; }
Call
abs
once and for all.
#include <iostream> #include <cstdlib> #include <cmath> //for the abs whose argument is an int using namespace std; int main() { const int width = 300; //number of columns of pixels const int height = width * 2 / 3; //number of rows of pixels const int xmax = width / 2; //place origin at center const int ymax = height / 2; const int xmin = xmax - width; const int ymin = ymax - height; cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum //Draw a simplified United Kingdom flag. for (int y = ymax - 1; y >= ymin; --y) { //top to bottom for (int x = xmin; x < xmax; ++x) { //left to right const int ax = abs(x); const int ay = abs(y); if (ax <= 10 || ay <= 10) { //red stripes along axes cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } else if (ax <= 20 || ay <= 20) { //red stripes have white border cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else if (abs(ay - .5 * ax) <= 5) { //red diagonal stripe cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } else if (abs(ay - .5 * ax) <= 15) { //red diagonal stripe has white border cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else { //blue background cout << 0 << "\t" << 0 << "\t" << 255 << "\n"; } } } return EXIT_SUCCESS; }
What does this function return when it doesn’t find what it’s looking for?
struct record { int ID; char name[20]; int age; float pay; }; int find(int a, record Person[], size_t n) { for (int i = 0; i < n; i++) { if (a == Person[i].ID) { return i; break; } } }
Variables usually have lowercase names.
Let the array be a read-only argument.
i
should be
size_t
.
Use prefix increment where possible.
The
break
statement is never executed.
size_t find(int a, const record person[], size_t n) { for (size_t i = 0; i < n; ++i) { if (a == person[i].ID) { return i; } } return n; //Did not find a. }
cout << "How many rows of blank in each box (e.g., 10)? "; int nblankrows; //uninitialized variable cin >> nblankrows; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } cout << "How many columns of blank in each box (e.g., 10)? "; int nblankcolumns; //uninitialized variable cin >> nblankrows; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; }
cout << "How many rows of blanks in each box (e.g., 10)? "; int nblankrows; //uninitialized variable cin >> nblankrows; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } cout << "How many columns of blanks in each box (e.g., 10)? "; int nblankcolumns; //uninitialized variable cin >> nblankcolumns; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; }
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const int width = 300; //number of columns of pixels
const int height = width * 2 / 3; //number of rows of pixels
const int xmax = width / 2; //place origin at center
const int ymax = height / 2;
const int xmin = xmax - width;
const int ymin = ymax - height;
const int nstripes = 3; //number of stripes
const int npairs = nstripes / 1; //number of pairs of stripes
const int pairheight = height / npairs; //height of each pair of stripes
cout << "P3\n" //magic number of Netpbm .ppm file
<< width << " " << height << "\n"
<< 255 << "\n"; //maximum color value; 0 is minimum
//Draw a simplified American flag.
for (int y = ymax - 1; y >= ymin; --y) { //top to bottom
for (int x = xmin; x < xmax; ++x) { //left to right
if (y< pairheight -100) {
//black stripe
cout << 0 << "\t" << 0 << "\t" << 0 << "\n";
} else if (pairheight -100 <=y && y < 100 - pairheight) {
//white stripe
cout << 255 << "\t" << 255 << "\t" << 255 << "\n";
} else {
//red stripe
cout << 255 << "\t" << 0 << "\t" << 0 << "\n";
}
}
}
return EXIT_SUCCESS;
}
The top 66 rows are red, the middle 67 rows are white, and the bottom 67 rows are black. It would be simpler to put the origin in the lower left corner. Get rid of the hard-coded number 100. Alloow the flag to scale up and down when you change the width.
#include <iostream> #include <cstdlib> using namespace std; int main() { const int width = 300; //number of columns of pixels const int height = width * 2 / 3; //number of rows of pixels const int xmax = width - 1; //Place origin at lower left corner. const int ymax = height - 1; const int xmin = 0; const int ymin = 0; cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum //Draw the flag of Yemen. for (int y = ymax - 1; y >= ymin; --y) { //top to bottom for (int x = xmin; x < xmax; ++x) { //left to right if (y < height / 3) { //black stripe cout << 0 << "\t" << 0 << "\t" << 0 << "\n"; } else if (y < height * 2 / 3) { //white stripe cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else { //red stripe cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } } } return EXIT_SUCCESS; }
struct subject_t { size_t number; //0 for singular, 1 for plural string text; }; const subject_t subject[] = { {0, "A cop who doesn't play by the rules"}, {1, "Three naughty nurses"} }; const size_t nsubject = sizeof subject / sizeof subject[0]; const string predicate[][2] = { {"fights crime", "fight crime"}, {"raises a baby", "raise a baby"} }; const size_t npredicate = sizeof predicate / sizeof predicate[0]; const string modifier[] = { }; const size_t nmodifier = sizeof modifier / sizeof modifier[0]; const size_t n = rand() % nsubject; cout << subject[n].text << "\n" << predicate[rand() % npredicate][subject[n].number] << "\n" << modifier[rand() % nmodifier] << "\n"
const subject_t& s = subject[rand() % nsubject]; cout << s.text << "\n" << predicate[rand() % npredicate][s.number] << "\n" << modifier[rand() % nmodifier] << "\n"
#include <iostream> #include <cstdlib> #include <string> //for class string using namespace std; // Sorting the famous Beatles song "Yesterday" backwards // using an array of structures. struct song { size_t line_number; string line_text; }; void sort_song(song *p, const size_t nsong, const bool sort_backwards); void print_song(const song *p, const size_t nsong); int main() { song s[] = { { 0, "Yesterday, all my troubles seemed so far away"}, { 1, "Now it looks as though they're here to stay"}, { 2, "Oh, I believe in Yesterday"} }; const size_t nsong = sizeof s / sizeof s[0]; sort_song(s, nsong, true); cout << "Song \"Yesterday\" Sorted backwards\n\n"; print_song(s, nsong); //Now, let's sort it back to the original. sort_song(s, nsong, false); cout << "\n\nSong \"Yesterday\" Sorted back to original\n\n"; print_song(s, nsong); system("PAUSE"); return EXIT_SUCCESS; } void sort_song(song *p, const size_t nsong, const bool sort_backwards) { for (song *v = p + nsong - 1; v > p; --v) { for (song *w = p; w < v; ++w) { if (sort_backwards) { if (w->line_number < (w + 1)->line_number) { //swap w and [w + 1] const size_t temp_number = w->line_number; const string temp_text = w->line_text; w->line_number = (w + 1)->line_number; w->line_text = (w + 1)->line_text; (w + 1)->line_number = temp_number; (w + 1)->line_text = temp_text; } } else { if (w->line_number > (w + 1)->line_number) { //swap w and [w + 1] const size_t temp_number = w->line_number; const string temp_text = w->line_text; w->line_number = (w + 1)->line_number; w->line_text = (w + 1)->line_text; (w + 1)->line_number = temp_number; (w + 1)->line_text = temp_text; } } } } } void print_song(const song *p, const size_t nsong) { for (const song *v = p; v < p + nsong; ++v) { cout << v->line_text << "\n"; } }
Song "Yesterday" Sorted backwards Oh, I believe in Yesterday Now it looks as though they're here to stay Yesterday, all my troubles seemed so far away Song "Yesterday" Sorted back to original Yesterday, all my troubles seemed so far away Now it looks as though they're here to stay Oh, I believe in Yesterday
Consolidate the repetition with the
?:
operator.
void sort_song(song *p, size_t nsong, bool sort_backwards) { for (song *v = p + nsong - 1; v > p; --v) { for (song *w = p; w < v; ++w) { if (sort_backwards ? w->line_number < (w + 1)->line_number : w->line_number > (w + 1)->line_number) { //swap w and [w + 1] const size_t temp_number = w->line_number; const string temp_text = w->line_text; w->line_number = (w + 1)->line_number; w->line_text = (w + 1)->line_text; (w + 1)->line_number = temp_number; (w + 1)->line_text = temp_text; } } } }
Apply a subscript to the pointer
w
.
void sort_song(song *p, size_t nsong, bool sort_backwards) { for (song *v = p + nsong - 1; v > p; --v) { for (song *w = p; w < v; ++w) { if (sort_backwards ? w[0].line_number < w[1].line_number : w[0].line_number > w[1].line_number) { //swap w[0] and w[1] const size_t temp_number = w[0].line_number; const string temp_text = w[0].line_text; w[0].line_number = w[1].line_number; w[0].line_text = w[1].line_text; w[1].line_number = temp_number; w[1].line_text = temp_text; } } } }
You’re allowed to assign one structure to another.
void sort_song(song *p, size_t nsong, bool sort_backwards) { for (song *v = p + nsong - 1; v > p; --v) { for (song *w = p; w < v; ++w) { if (sort_backwards ? w[0].line_number < w[1].line_number : w[0].line_number > w[1].line_number) { //swap w[0] and w[1] const song temp = w[0]; w[0] = w[1]; w[1] = temp; } } } }
Use the C+ Standard Template Library (STL).
#include <iostream> #include <cstdlib> #include <string> //for class string #include <iterator> //for class ostream_iterator #include <functional> //for clas binary_function #include <algorithm> //for the copy and sort algorithms using namespace std; // Sorting the famous Beatles song "Yesterday" backwards // using an array of structures. struct song { size_t line_number; string line_text; //What field(s) of the structure should be printed out? friend ostream& operator<<(ostream& ost, const song& s) { return ost << s.line_text; } }; //Return true if a should come before b when the array of structures is sorted. struct forwards: public binary_function<song, song, bool> { bool operator()(const song& a, const song& b) const { return a.line_number < b.line_number; } }; int main() { song s[] = { { 0, "Yesterday, all my troubles seemed so far away"}, { 1, "Now it looks as though they're here to stay"}, { 2, "Oh, I believe in Yesterday"} }; const size_t nsong = sizeof s / sizeof s[0]; sort(s, s + nsong, not2(forwards())); cout << "Song \"Yesterday\" Sorted backwards\n\n"; copy(s, s + nsong, ostream_iterator<song>(cout, "\n")); //Now, let's sort it back to the original. sort(s, s + nsong, forwards()); cout << "\n\nSong \"Yesterday\" Sorted back to original\n\n"; copy(s, s + nsong, ostream_iterator<song>(cout, "\n")); system("PAUSE"); return EXIT_SUCCESS; }