* What is a bad program? * What is a good program? * What are good programming practices? * Are there programming languages that make it easy to write good programs? * What is your previous experience with C? * What are the advantages of C as a programming language? * What are the disadvantages of C? What about C++? * What is a data-structure? * Which data structures do you know in C? * Can you write a "hello, world" in C? #include int main() { printf("Oi, Mundo!\n"); return 0; } * When was the C programming language designed and implemented? * What about C++? * Can you write a "hello, world" in C++? #include int main() { std::cout << "Oi, Mundo!\n"; return 0; } * What are the main differences between the two programs? * And what are the differences that you think exist between C and C++? * How can we use strings in C? #include int main() { char s0[4] = {'d', 'c', 'c', '\0'}; char s1[] = "dcc"; printf("%s\n", s0); printf("%s\n", s1); } * How can we know the size of a string in C? #include #include unsigned size(char* s) { unsigned i = 0; while (s[i] != '\0') i++; return i; } int main() { char s0[] = "abc"; char *s1 = "wxyz"; printf("Last char s0 = %u\n", size(s0)); printf("Last char s1 = %u\n", size(s1)); printf("Last char s0 = %lu\n", strlen(s0)); } * Can you modify a string constant in C? #include int main() { char s0[] = "abcde"; char *s1 = "vwxyz"; printf("s0 = %s, s1 = %s\n", s0, s1); s0[0] = '_'; printf("s0 = %s, s1 = %s\n", s0, s1); s1[0] = '_'; printf("s0 = %s, s1 = %s\n", s0, s1); } Read: https://stackoverflow.com/questions/4208444/c-difference-between-and "s0[] declares an array of characters, which is filled with abcde. *s1 is a pointer to a string constant." * How can we use the string class in C++? #include #include using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; // copy str1 into str3 str3 = str1; cout << "str3 : " << str3 << endl; // concatenates str1 and str2 str3 = str1 + str2; cout << "str1 + str2 : " << str3 << endl; // total length of str3 after concatenation len = str3.size(); cout << "str3.size() : " << len << endl; return 0; } * Can we modify strings? #include #include using namespace std; int main () { string str = "oi"; str[0] = 'e'; printf("%d\n", str[0]); return 0; } * What if they are marked as constants? #include using namespace std; int main () { const string str = "oi"; str[0] = 'e'; printf("%d\n", str[0]); return 0; } * What will be printed below? #include using namespace std; int main() { string s0 = "oi"; string s1 = s0; s0[0] = 'a'; cout << s1 << endl; } * What are typical operations that we can perform on strings? #include #include using namespace std; int main() { // Declaring string string str; // Taking string input using getline() // "geeksforgeek" in givin output getline(cin,str); // Displaying string cout << "The initial string is : "; cout << str << endl; // Using push_back() to insert a character at end str.push_back('s'); // Displaying string cout << "The string after push_back operation is : "; cout << str << endl; // Using pop_back() to delete the character at the end str.pop_back(); // Displaying string cout << "The string after pop_back operation is : "; cout << str << endl; return 0; } * What do you think is the meaning of the syntax o.m()? * How can we iterate over the characters in a string? #include #include using namespace std; int main() { string str = "DCC204"; for (auto c: str) { cout << c << endl; } return 0; } * What if we need to iterate on the inverse order? #include #include using namespace std; int main() { string str = "DCC204"; for (auto i = str.rbegin(); i != str.rend(); ++i) { cout << *i << endl; } return 0; } * How to convert a C++ string to a C style char array? #include #include using namespace std; int main() { string s = "DCC204"; int n = s.length(); char char_array[n + 1]; strcpy(char_array, s.c_str()); printf("%s\n", char_array); return 0; } * How to read a file with pairs of numbers? E.g.: $> cat file.txt 3 4 1 5 1 3 2 5 4 5 #include #include int main(int argc, char** argv) { std::ifstream infile(argv[1]); int a, b; while (infile >> a >> b) { printf("(%d, %d)\n", a, b); } } * What are the problems with this program? * What happens if we do not pass a file name in the input? * How to fix this program? #include #include #include int main(int argc, char** argv) { std::ifstream infile(argv[1]); if (!infile.is_open()) { std::cerr << "Failed to open file\n"; } int a, b; while (infile >> a >> b) { printf("(%d, %d)\n", a, b); } } * Can't you test if the argument is missing and report it back to the user? #include #include #include int main(int argc, char** argv) { if (argc != 2) { std::cerr << "Syntax: cmd \n"; return 1; } else { std::ifstream infile(argv[1]); if (!infile.is_open()) { std::cerr << "Failed to open file\n"; return 2; } int a, b; while (infile >> a >> b) { printf("(%d, %d)\n", a, b); } } return 0; } * How can you check the output of an executable file in Linux? $> ./a.out file.txt $> echo $? * How can you save the points in a struct? #include #include #include struct Point { int x; int y; }; int main(int argc, char** argv) { if (argc != 2) { std::cerr << "Syntax: cmd \n"; return 1; } else { std::ifstream infile(argv[1]); if (!infile.is_open()) { std::cerr << "Failed to open file\n"; return 2; } struct Point p; while (infile >> p.x >> p.y) { printf("(%d, %d)\n", p.x, p.y); } } return 0; } * Can you save the points in a data-structure, e.g., a list of points? #include #include #include #include struct Point { int x; int y; }; int main(int argc, char** argv) { if (argc != 2) { std::cerr << "Syntax: cmd \n"; return 1; } else { std::vector vec; std::ifstream infile(argv[1]); if (!infile.is_open()) { std::cerr << "Failed to open file\n"; return 2; } struct Point p0; while (infile >> p0.x >> p0.y) { vec.push_back(p0); } for (auto p1: vec) { printf("(%d, %d)\n", p1.x, p1.y); } } return 0; } * What are the advantages of vectors over arrays? * Why we get this warning when we compile the program? E.g.: $> clang++ centroid2.cpp centroid2.cpp:26:10: warning: 'auto' type specifier is a C++11 extension * How to avoid it? $> clang++ -std=c++11 centroid2.cpp * How to find the centroid of the points? #include #include #include #include struct Point { int x; int y; }; int main(int argc, char** argv) { if (argc != 2) { std::cerr << "Syntax: cmd \n"; return 1; } else { std::vector vec; std::ifstream infile(argv[1]); if (!infile.is_open()) { std::cerr << "Failed to open file\n"; return 2; } struct Point p0; while (infile >> p0.x >> p0.y) { vec.push_back(p0); } // Find the centroid: double center_x = 0.0; double center_y = 0.0; for (auto p1: vec) { center_x += p1.x; center_y += p1.y; } double size = vec.size(); printf("(%.3lf, %.3lf)\n", center_x/size, center_y/size); } return 0; } * Why are there so few data-structures in C?