0% found this document useful (0 votes)
2 views

Advanced Data Structure & Algorithm_1_1724990220299

This document provides an introduction to C++ programming, covering fundamental concepts such as variables, constants, flow control, functions, arrays, and classes. It explains the differences between C and C++, the syntax of C++, and various programming constructs like loops and conditional statements. Additionally, it discusses algorithm design and analysis, emphasizing the importance of algorithms in programming.

Uploaded by

karan01ksh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Advanced Data Structure & Algorithm_1_1724990220299

This document provides an introduction to C++ programming, covering fundamental concepts such as variables, constants, flow control, functions, arrays, and classes. It explains the differences between C and C++, the syntax of C++, and various programming constructs like loops and conditional statements. Additionally, it discusses algorithm design and analysis, emphasizing the importance of algorithms in programming.

Uploaded by

karan01ksh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

UNIT-I: Introduction to C++ and Algorithms

Introduction to C++: Variables, Constants,


Flow of control, Functions, Arrays, Strings,
Pointer, Classes, Memory management.
Algorithm Specification: Introduction to the
algorithm, Algorithm Design, and Data
Structures, Analyzing an algorithm, Algorithm
Design Approach, Pseudo code Conventions

Introduction to C++
1. What is C++?
 C++ is a cross-platform language that can be used to create
high-performance applications.
 C++ was developed by Bjarne Stroustrup, as an extension to
the C language.
 C++ gives programmers a high level of control over system
resources and memory.
 The language was updated 4 major times in 2011, 2014, 2017,
and 2020 to C++11, C++14, C++17, C++20

2. Why Use C++


 C++ is one of the world's most popular programming languages.
 C++ can be found in today's operating systems, Graphical User
Interfaces, and embedded systems.
 C++ is an object-oriented programming language which gives a
clear structure to programs and allows code to be reused,
lowering development costs.
 C++ is portable and can be used to develop applications that can
be adapted to multiple platforms.
 As C++ is close to C, C# and Java, it makes it easy for
programmers to switch to C++ or vice versa.

3. Difference between C and C++


 C++ was developed as an extension of C, and both languages
have almost the same syntax.
 The main difference between C and C++ is that C++ support
classes and objects, while C does not.

4. Applications
 C++ is used to create computer programs, and is one of the most
used language in game development.

5. C++ Syntax
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Explanation:
 The #include is a preprocessor directive used to include files
in our program. The above code is including the contents of
the iostream file.
 #include <iostream> is a header file library that lets us work
with input and output objects, such as cout
 This allows us to use cout in our program to print output on
the screen
 using namespace std means that we can use names for
objects and variables from the standard library.
6. C++ Statements
 A computer program is a list of "instructions" to be "executed"
by a computer.
 In a programming language, these programming instructions are
called statements.

cout << "Hello World!";


It is important that you end t he statement with a semicolon ;
6.1 : New Lines
To insert a new line, you can use the \n character:
Type 1:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World! \n";
return 0;
}

Type 2:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << "\n";
return 0;
}
Type 3:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
6.2: horizontal tab
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\t";
cout << "I am learning C++";
return 0;
}
Output: Hello World! I am learning C++
6.3: backslash character
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\\";
cout << "I am learning C++";
return 0;
}
Output: Hello World!\I am learning C++
6.4: double quote character
#include <iostream>
using namespace std;
int main()
{
cout << "I am learning \"C++\"";
return 0;
}
Output: I am learning "C++"
a list of the single character escape codes:
7. Comments
7.1 Single-line Comments

Single-line comments start with two forward slashes (//).


Example:
// This is a comment
cout << "Hello World!";

7.2 Multi-line Comments

Multi-line comments start with /* and ends with */.


Example:
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";

8. Variables and types

variable as a portion of memory to store a value. Each variable needs


a name that identifies it and distinguishes it from the others.
Int a=10;
Int a;
Float pi=3.14;
String name=”RSCOE”;
Char div=’A’;
8.1 Declaration of variables
// operating with variables
int data type:
#include <iostream>
using namespace std;
int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
cout << result;
// terminate the program:
return 0;
}
// initialization of variables
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value: 5
int b(3); // initial value: 3
int c{2}; // initial value: 2
int result; // initial value undetermined
a = a + b;
result = a - c;
cout << result;
return 0;
}
String Data type:
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is a string";
cout << mystring;
return 0;
}

all initialization formats are valid with strings:


string mystring = "This is a string";
string mystring ("This is a string");
string mystring {"This is a string"};

9. Constants
Constants are expressions with a fixed value.
9.1 Literals
Literals are the most obvious kind of constants. They are used to
express particular values within the source code of a program. for
example, when we wrote: a = 5;
The 5 in this piece of code was a literal constant
9.2 Typed constant expressions
Sometimes, it is just convenient to give a name to a constant value:
const double pi = 3.1415926;
const char tab = '\t';
We can then use these names instead of the literals they were defined
to:
#include <iostream>
using namespace std;

const double pi = 3.14159;


const char newline = '\n';

int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * pi * r;
cout << circle;
cout << newline;
}

10. Basic Input/Output


stream Description
cin standard input stream
cout standard output stream
cerr standard error (output) stream
clog standard logging (output) stream

10.1 standard output stream (cout)


 cout is used together with the insertion operator, which is
written as << (i.e., two "less than" signs).
Ex: cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x;
 Multiple insertion operations (<<) may be chained in a single
statement:
cout << "This " << " is a " << "single C++ statement";
 mix literals and variables in a single statement:

cout << "I am " << age << " years old and my zipcode is " <<
zipcode;

Here age & zipcode is variable.


10.2 Standard input stream (cin) :
 cin is used together with the extraction operator, which is
written as >> (i.e., two "greater than" signs).
int age;
cin >> age;
 cin & string:
11. Statements and flow control
If-else, switch, For loop, while loop, Do-while loop, break
statement, continue statement, goto statement
a.Selection statements: if and else
The if keyword is used to execute a statement or block, if, and only if,
a condition is fulfilled.
-if statement
-if-else statement
-nested if statement
-if-else-if ladder
Its syntax is:
ex:1
#include <iostream>
using namespace std;
int main () {
int num = 11;
if (num % 2 == 0)
{
cout<<"It is even number";
}
else
{
cout<<"It is odd number";
}
return 0;
}
Output: It is odd number
b.Switch Statement:
The C++ switch statement executes one statement from multiple
conditions. It is like if-else-if ladder statement in C++.
#include <iostream>
using namespace std;
int main () {
int num;
cout<<"Enter a number to check grade:";
cin>>num;
switch (num)
{
case 10: cout<<"It is 10"; break;
case 20: cout<<"It is 20"; break;
case 30: cout<<"It is 30"; break;
default: cout<<"Not 10, 20 or 30"; break;
}
}
c. For Loop
The C++ for loop is used to iterate a part of the program several
times. If the number of iterations is fixed, it is recommended to use
for loop than while or do-while loops.
#include <iostream>
using namespace std;
int main() {
for(int i=1;i<=10;i++){
cout<<i <<"\n";
}
}
Iteration statements (loops)
The while loop is simplest kind of loop is the while-loop. Its syntax
is:
while (expression) statement
ex: #include <iostream>
using namespace std;
int main ()
{
int n = 10;
while (n>0)
{
cout << n << ", ";
--n;
}
cout << "liftoff!\n";
}
The do-while loop
A very similar loop is the do-while loop, whose syntax is:
do statement while (condition);
ex:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
do {
cout << "Enter text: ";
getline (cin,str);
cout << "You entered: " << str << '\n';
} while (str != "goodbye");
}

You might also like