C++ for Competitive Programming PDF

Summary

This document provides a basic introduction to C++ programming language, suitable for competitive programming. It covers data types, decision-making (if-else, switch), loops and functions, the C++ standard library, code shortening methods, and setting up a C++ programming environment. Exercises are also included, focusing on implementing if-else and switch statements and loops.

Full Transcript

Programming Language C++ for Competitive Programming < The Fundamentals > Prepared by: DR. SITI ZAHIDAH ABDULLAH Table of Contents 1. Data types & variables < Boolean, characters, integer, float > 2. Decision Making, Loops, & Function < If statement, switch...

Programming Language C++ for Competitive Programming < The Fundamentals > Prepared by: DR. SITI ZAHIDAH ABDULLAH Table of Contents 1. Data types & variables < Boolean, characters, integer, float > 2. Decision Making, Loops, & Function < If statement, switch statement > 3. C++ Standard Library and Syntax for CP < Vector, List, Deque, Stack, Queue > 4. C++: Methods of code shortening in CP < Types name, Macros > 5. Setting up a C++ CP Environment < C++ IDE > 6. Coding Exercise The Basic C++ Features of C++ Mind-Level Simple; Programming Portability; Dynamic Memory Allocation; Rich Library; Recursion; Case Sensitive; Object-Oriented Programming; Compiler Based; Fast; Program in C++ {Container} Header file 01 #include //input/output using space std; Main Function 02 int main () { cout 5; bool b = 10 == 10; bool c = 10 !=5; The Basic C++ Operators in C++ {Example} // Logical operators; bool d = a && b; // Logical AND bool e = d I I e; // Logical OR bool f = !=a; // Logical NOT The Basic C++ If Statement in C++ {Example} // if else statement; if (temperature >39) { cout > endl; } else is (temperature < 38) { cout > endl; } else { } Switch Statement in C++ {Example} // switch statement; switch (menu) { case 1 : // …. break; } case 2: // ….. break; default: } CLASS EXERCISE // switch statement; {If Else Statement} Exercise 1: Write a program that takes an input of age and print if an adult or not. Condition: >= 18 yes, “not eligible for job” b) If age >= 18, print --> “eligible for job” c) If age >= 55 and age “eligible for job, but retirement soon,” d) If age > 57, print --> “retirement time” CLASS EXERCISE // switch statement; {Switch Statement} Exercise 4: Take the day number and print the corresponding day; for 1 print Monday, for 2 print Tuesday an so on for 7 print Sunday. Loops Four types of Loops; For loops; Useful when we know ahead of time how many times we want to repeat something. While loops; Often used when we don’t Range-based for know ahead of time how many loops; times we need to repeat Useful when iterating over a something. list of items (eg an array or a string). Do-while loops; It repeat the statement till the given condition is true. Loops in C++ {Example} // For loop; for (int i = 0; 1 < 5; i++) cout

Use Quizgecko on...
Browser
Browser