0% found this document useful (0 votes)
7 views36 pages

Programing Fundamental

The document provides a comprehensive overview of computer languages, categorizing them into low-level (machine and assembly languages) and high-level languages (like BASIC, COBOL, FORTRAN, C, and C++). It explains the role of programming language translators such as compilers, interpreters, assemblers, and linkers, as well as the programming process, problem-solving techniques, and the structure of C++ programs. Additionally, it covers data types, identifiers, variables, and escape sequences in C++.

Uploaded by

Muhammad Hassaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views36 pages

Programing Fundamental

The document provides a comprehensive overview of computer languages, categorizing them into low-level (machine and assembly languages) and high-level languages (like BASIC, COBOL, FORTRAN, C, and C++). It explains the role of programming language translators such as compilers, interpreters, assemblers, and linkers, as well as the programming process, problem-solving techniques, and the structure of C++ programs. Additionally, it covers data types, identifiers, variables, and escape sequences in C++.

Uploaded by

Muhammad Hassaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Basic Definition:

• A language that is acceptable to a computer system is called a computer language or


programming language
• The process of creating a sequence of instructions in such a language is called
programming or coding.
• A program is a set of instructions, written to perform a specific task by the computer.
• A set of large programs is called software.

COMPUTER LANGUAGES:
Low Level Language:
The term low level highlights the fact that it is closer to a language which the machine
understands.
The low-level languages are classified as:
➢ Machine Language: This is the language (in the form of 0’s and 1’s, called binary
numbers) understood directly by the computer. It is machine dependent. It is difficult to
learn and even more difficult to write programs.
➢ Assembly Language: This is the language where the machine codes comprising of
0’sand 1’s are substituted by symbolic codes (called mnemonics) to improve their
understanding. It is the first step to improve programming structure. Assembly language
programming is simpler and less time consuming than machine level programming, it is
easier to locate and correct errors in assembly language than in machine language
programs. It is also machine dependent. Programmers must have knowledge of the
machine on which the program will run.
High Level Language:
Low level language requires extensive knowledge of the hardware since it is machine dependent.
To overcome this limitation, high level language has been evolved which uses normal English,
which is easy to understand to solve any problem. High level languages are computer
independent and programming becomes quite easy and simple. Various high level languages are
given below:
• BASIC (Beginners All Purpose Symbolic Instruction Code): It is widely used, easy to
learn general purpose language. Mainly used in microcomputers in earlier days.
• COBOL (Common Business Oriented language): A standardized language used for
commercial applications. ‘
• FORTRAN (Formula Translation): Developed for solving mathematical and scientific
problems. One of the most popular languages among scientific community.
• C: Structured Programming Language used for all purpose such as scientific application,
commercial application, developing games etc.
• C++: Popular object-oriented programming language, used for general purpose.
PROGRAMMING LANGUAGE TRANSLATORS:
Compiler: The software that reads a program written in high level language and translates it into
an equivalent program in machine language is called as compiler. The program written by the
programmer in high level language is called source program and the program generated by the
compiler after translation is called as object program.
Interpreter: it also executes instructions written in a high-level language. Both complier &
interpreter have the same goal i.e. to convert high level language into binary instructions, but
their method of execution is different. The complier converts the entire source code into machine
level program, while the interpreter takes 1 statement, translates it, executes it & then again takes
the next statement.
Assembler: The software that reads a program written in assembly language and translates it
into an equivalent program in machine language is called as assembler.
Linker: A linker or link editor is a computer program that takes one or more object files
generated by a compiler and combines them into a single executable file, library file, or another
object file.

Key Points:
Computers require both hardware and software to operate. Software consists of
instructions that control the hardware.
At the lowest level, the instructions for a computer program can be represented as a
sequence of zeros and ones. The pattern of zeros and ones determine the instructions
performed by the processor.
Two different kinds of processors can have different machine languages.
Application software can be written largely without regard to the underlying hardware. A
tool called a compiler translates the higher-level, abstract language into the machine
language required by the hardware.
Programmers develop software using tools such as editors, compilers, debuggers, and
profilers.
C++ is a higher-level programming language.
An IDE is an integrated development environment—one program that provides all the
tools that developers need to write software.
To Do Tasks:
1. What is a compiler?
2. How is compiled code different from source code?
3. What tool does a programmer use to produce C++ source code?
4. What tool(s) does a programmer use to convert C++ source code into executable machine
code? What does the linker do?
5. What does the preprocessor do to source code?
6. List several advantages developing software in a higher-level language has over
developing software in machine language.
7. How can an IDE improve a programmer’s productivity?

Problem Solving Technique

WHAT IS PROBLEM SOLVING


Problem solving is the act of
• defining a problem
• determining the cause of the problem
• identifying, prioritizing, and selecting alternatives for a solution
• implementing a solution.

THE PROBLEM-SOLVING PROCESS


1. Define the problem
2. Generate alternative solutions
3. Evaluate and select an alternative
4. Implement and follow up on the solution

Define the problem


1. Distinguish between facts and opinions.

2. Identify the root causes of the issue.


3. Gather information from each involved faction.
4. Clearly articulate the specific problem.
5. Identify the violated standard or expectation.
6. Determine the process in which the problem originates.
7. Refrain from attempting to solve the problem without sufficient data.

Generate alternative solutions

• Postpone evaluating alternatives initially


• Include all involved individuals in the generating of alternatives
• Specify alternatives consistent with organizational goals
• Specify short- and long-term alternatives
• Brainstorm on others' ideas
• Seek alternatives that may solve the problem

Evaluate and select an alternative

• Evaluate alternatives relative to a target standard


• Evaluate all alternatives without bias
• Evaluate alternatives relative to established goals
• Evaluate both proven and possible outcomes
• State the selected alternative explicitly

Implement and follow up on the solution

• Plan and implement a pilot test of the chosen alternative


• Gather feedback from all affected parties
• Seek acceptance or consensus by all those affected
• Establish ongoing measures and monitoring
• Evaluate long-term results based on final solution

Different problem-solving techniques are as follow:

Algorithm
Flow Chart
Program

Algorithm:
A sequence of precise instructions that leads to a solution.
Example:
1. Get the list of names.
2. Get the name being checked
3. Set a counter to zero.
4. Do the following for each name on the list:
Compare the name on the list to the name being checked, and if the names are the same, then add
one to counter.
5. Announce that the answer is the number indicated by the counter.

Program:
An algorithm expressed in a language the comp can understand.

Programming Process
1. Program Design Process:
Problem solving process:
Result is an algo that solves the problem.
Implementation Phase:
Result is the algo translated into a programming language.

2. Be certain the task is completely specified:


✓ What is the input?
✓ What information is in the output?
✓ How is the output organized?

3. Develop the algo before implementation:


✓ Experience shows this saves time in getting your program to run.
✓ Test the algo for correctness.

4. Implementation Phase:
• Translate the algo into a programming language.
• Compile the source code: Locates error in using the programing language.
• Run the program on sample data: Verify correctness of results
• Results may require modification of algorithm and program.
Example:
Step 1: Problem Analysis
• Purpose: To describe in details a solution to a problem by providing the needed
information for the problem.
• How: First, understand and study the problem.
• Identify: input to the problem, Required output, relevant process. For example, scientific
formula or appropriate theories if any.

Problem: write a program that get 3 numbers as input from user. Find the avg and
display the numbers and the avg.
Problem Analysis:
Input: 3 numbers.
Process:
1. Add the numbers.
2. Divide the total with 3.
Output: The 3 numbers and average.

Example:

Input: 7, 21, 32
Process:
1. 7 + 21 + 32
2. 7 + 21 + 32 / 3
Output: 20

Step 2: Program Design


Definition: It defines the framework or the flow or the problem solution.

1. Algorithm:
• Algo is a sequence of instruction to solve a problem written in a human
language and problem can be solved if follow the correct procedure.
• A programmer writes the solution in the form of an algorithm before
coding it into computer language.
Example of algorithm to calculate the avg of 3 numbers:
1. Set total= 0, average= 0;
2. Input 3 numbers
3. Total up the 3 numbers (Total= total/3)
4. Calculate average (avg= total/3)
5. Display 3 no. and avg

Flowchart:
A graphical representation of data, info and process or an orderly step- by-step solution to a
problem.

Symbols:
Example:
Pseudocode:
Steps in problem solving written in certain of programming code and certain in human language.
Example: Some part use C++ language code and some part use English like phrases.
Example:
Start
Input a, b, c
Total = a + b + c
Avg = total / 3
Output a, b, c
Output avg
END
Differences between algo pseudocode and program
Algorithm and Pseudocode are the two related terms in computer programming. The basic
difference between algorithm and pseudocode is that an algorithm is a step-by-step procedure
developed to solve a problem, while a pseudocode is a technique of developing an algorithm
Algorithm, pseudo, and C++ program for add 3 numbers and take average them.
Algorithm:
1. Start
2. Input three numbers (num1, num2, num3)
3. Sum = num1 + num2 + num3
4. Average = Sum / 3
5. Output the sum and average
6. End
Pseudo Code:
// Input
Input num1
Input num2
Input num3
// Process
Sum = num1 + num2 + num3
Average = Sum / 3
// Output
Output "Sum: ", Sum
Output "Average: ", Average

Program:
BASIC STRUCTURE OF C++ LANGUAGE:
This is a simple structure for a C++ program. Key elements include:
1. Comments: These are optional lines that provide information about the program. They
are ignored by the compiler.
2. Header Files: These include necessary libraries or header files. In the example,
<iostream> is included for input and output operations.
3. Namespace: The using namespace std; statement is used to avoid having to prefix
standard C++ library elements with std::.
4. Main Function: The main function is the entry point of every C++ program. Execution
begins here.
5. Variable Declaration and Initialization: Variables used in the program are declared
and, if needed, initialized.
6. Input: The cin statement is used to get input from the user.
7. Processing: This section contains the main logic of the program, where calculations or
other operations are performed.
8. Output: The cout statement is used to display the result to the user.
9. Return Statement: The return 0; statement indicates that the program executed
successfully.
Example:
// Comments: These are optional but provide information about the program.
// They are ignored by the compiler.

// Header Files: Include necessary header files for input/output, etc.


#include <iostream>

// Namespace: Specify the namespace to be used.


using namespace std;

// Main Function: Every C++ program must have a main function.


int main () {
// Declaration and Initialization of Variables: Define variables if needed.
int num1, num2, sum;
// Input: Get input from the user.
cout << "Enter the first number: ";
cin >> num1;

cout << "Enter the second number: ";


cin >> num2;

// Processing: Perform calculations or other operations.


sum = num1 + num2;

// Output: Display the result.


cout << "Sum: " << sum << endl;

// Return Statement: Indicate the end of the program.


return 0;
}

DATA TYPES:
A data type is used to indicate the type of data value stored in a variable. All C compilers
support a variety of data types. This variety of data types allows the programmer to select the
type appropriate to the needs of the application as well as the machine. ANSI C supports the
following classes of data types:
1.Primary (fundamental) data types.
2.Derived data types.
3.User-defined data types
Primary data types:
1. integer data type
2. character data type
3. float point data type
4. Boolean data type
5. void data type
Integer data type: -
This data type is used to store whole numbers. These numbers do not contain the decimal part.
The size of the integer depends upon the world length of a machine (16-bit or 32-bit). On a 16-
bit machine, the range of integer values is - 32,768 to +32,767. integer variables are declared by
keyword int.
Boolean data type: -
Boolean or logical data type is a data type, having two values (usually denoted true and false),
intended to represent the truth values of logic and Boolean algebra.
Void type:
The void type has no values. This is usually used to specify the return type of functions. The
type of the function said to be void when it does not return any value to the calling function. This
is also used for declaring general purpose pointer called void pointer.
Derived data types
Derived datatypes are Arrays , pointer and references are examples for derived data types.
User-defined data types: they are data types defined by the user are known as the user-defined
data types. They are structure, union, class and enumeration.

C++ Tokens IDENTIFIERS:


Identifiers are the names given to various program elements such as variables, functions and
arrays. These are user defined names consisting of sequence of letters and digits.
Rules for declaring identifiers:
1. The first character must be an alphabet or underscore.
2. It must consist of only letters, digits and underscore.
3. Identifiers may have any length but only first 31 characters are significant.
4. It must not contain white space or blank space.
5. We should not use keywords as identifiers.
6. Upper- and lower-case letters are different. Example: ab Ab aB AB are treated differently
Examples of valid identifiers: a, x, n, num, SUM, fact, grand total, sum_of_digits, sum1
Examples of Invalid identifiers: $amount, ³num´, grand-total, sum of digits, 4num. $amount:
Special character is not permitted grand-total:
hyphen is not permitted. sum of digits: blank spaces between the words are not allowed.
4num: should not start with a number (first character must be a letter or underscore)
Variables:
A named memory location is called variable.
OR
It is an identifier used to store the value of particular data type in the memory.
Since variable name is identifier, we use following rules which are same as of identifier
Rules for declaring Variables names:
The first character must be an alphabet or underscore.
1. It must consist of only letters, digits and underscore.
2. Identifiers may have any length but only first 31 characters are significant.
3. It must not contain white space or blank space.
4. We should not use keywords as identifiers.
5. Upper- and lower-case letters are different.
6. Variable names must be unique in the given scope
Ex:
• int a,b,a;//is in valid
• Int a,b;//is valid Variable declaration:
The declaration of variable gives the name for memory location and its size and specifies the
range of value that can be stored in that location.
Syntax:
Data type variable name;
Ex:
int a=10;
float x=2.3;

Escape Sequence:
Escape sequences in C++ are special sequences of characters that are used to represent non-
printable characters or to perform certain actions within a string. Here are some commonly used
escape sequences in C++:
These escape sequences can be used within strings to format output or represent special
characters. Remember that the actual effect may depend on the terminal or system where the
program is executed.
OPERATORS AND EXPRESSIONS
An operator is a symbol which represents a particular operation that can be performed on data.
An operand is the object on which an operation is performed. By combining the operators and
operands we form an expression. An expression is a sequence of operands and operators that
reduces to a single value. Operators can be classified as
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment or Decrement operators
6. Conditional operator

ARITHMETIC OPERATORS :
All basic arithmetic operators are present in C++ operator meaning
➢ Add +
➢ Subtract -
➢ Multiplication*
➢ Division /
➢ Modulo division(remainder) %
An arithmetic operation involving only real operands(or integer operands) is called real
arithmetic(or integer arithmetic). If a combination of arithmetic and real is called mixed mode
arithmetic.
/*C++ program on Integer Arithmetic Expressions*/
#include void main()
{
int a, b;
cout<"Enter any two integers";
cin>>a>>b;
cout<<"a+b"<< a+b; cout<<"a-b"<< a-b;
cout<<"a*b"<< a*b;
cout<<"a/b"<< a/b;
cout<<"a%b"<< a%b;
}
OUTPUT:
a+b=23
a-b=17
a*b=60
a/b=6
a% b=2 2.
RELATIONAL OPERATORS:
We often compare two quantities and depending on their relation take certain decisions for that
comparison we use relational operators. operator meaning
➢ <
➢ >
➢ <=
➢ >=
➢ ==
➢ !=

is less than is greater than is less than or equal to is greater than or equal to is equal to is not
equal to
/* C++ program on relational operators*/
#include void main()
{
int a, b;
clrscr();
cout<<"Enter a, b values:";
cin>>a>>b;
cout<<"a>b"<< a>b;
cout<<"a>=b"<< a>=b;
cout<<"a<b"<< a<<"a<=b"<< a<=b;
cout<<"a==b"<< a==b;
cout<<"a!=b"<< a!=b;
}
OUTPUT:
Enter a, b values: 5, 9
a>b: 0 //false
a=a: 1 //true
a<=b: 1 //true
a==b: 0 //false
a!=b: 1 //true
LOGICAL OPERATORS:
Logical Data: A piece of data is called logical if it conveys the idea of true or false. In C++ we
use int data type to represent logical data. If the data value is zero, it is considered as false. If it is
non -zero (1 or any integer other than 0) it is considered as true. C++ has three logical operators
for combining logical values and creating new logical values:

INCREMENT (++) AND DECREMENT (--) OPERATORS:


The operator ++ adds one to its operand where as the operator - - subtracts one from its operand.
These operators are unary operators and take the following form: Both the increment and
decrement operators may either precede or follow the operand.
Postfix Increment/Decrement :( a++/a--)
In postfix increment (Decrement) the value is incremented (decremented) by one. Thus the a++
has the same effect as a=a+1; a--has the same effect as a=a-1. The difference between a++ and
a+1 is, if ++ is after the operand, the increment takes place after the expression is evaluated. The
operand in a postfix expression must be a variable.
Ex1:
int a=5;
B=a++;
Here the value of B is 5. the value of a is 6.
Ex2:
int x=4;
y=x--;
Here the value of y is 4, x value is 3
Prefix Increment/Decrement (++a/ --a)
In prefix increment (decrement) the effect takes place before the expression that contains the
operator is evaluated. It is the reverse of the postfix operation. ++a has the same effect as a=a+1.
--a has the same effect as a=a-1.
Ex:
int b=4;
A= ++b;
In this case the value of b would be 5 and A would be 5.
The effects of both postfix and prefix increment is the same: the variable is incremented by 1.
But they behave differently when they used in expressions as shown above. The execution of
these operators is fast when compared to the equivalent assignment statement.
If statement
"if statement" is a control flow statement that allows you to conditionally execute a block of
code. It enables you to specify a condition, and if that condition is true, the code inside the "if"
block is executed; otherwise, it is skipped. The basic syntax of an if statement in C++ looks like
this:
Syntax
if (condition) {
// Code to be executed if the condition is true
}
Here, condition is an expression that evaluates to either true or false. If the condition is true, the
code inside the curly braces will be executed; otherwise, it will be skipped.
Code:

IF-ELSE STATEMENT
if (condition)
{
// Code to be executed if the condition is true
}
Else
{
// Code to be executed if the condition is false}
Example with Pseudo Code, Algorithm, and C++ Code:
Pseudo Code:
1. Start
2. Input a number
3. If the number is greater than 0
4. Display "The number is positive."
5. Else
6. Display "The number is non-positive."
7. End

Algorithm:
START
1. Read number
2. IF number > 0 THEN
3. Display "The number is positive."
4. ELSE
5. Display "The number is non-positive."
6. END
Code:

If else if:

if-else-if is a conditional statement structure used to execute different blocks of code based on
multiple conditions. It allows you to test multiple conditions and execute different blocks of code
depending on which condition is true.

if (condition1) {
// Code block executed if condition1 is true
}
else if (condition2) {
// Code block executed if condition1 is false and condition2 is true
}
else {
// Code block executed if neither condition1 nor condition2 is true
}
In this structure, each if statement is followed by an optional else block, which contains another
if statement. This allows for a cascade of conditions to be checked. When one of the conditions is
met, the corresponding block of code associated with that condition is executed, and the rest of
the conditions are not evaluated. If none of the conditions are true, the code inside the else block
(if present) is executed.

Code

Nested If:
The syntax of a nested if statement varies slightly depending on the programming language, but
generally follows a structure where one if statement is nested within another. Here's a generic
syntax for nested if statements:
Syntax
if condition1:
# code block 1
if condition2:
# code block 2
else:
# code block 3
else:
# code block 4
In this syntax:
condition1 is the condition checked by the outer if statement.
condition2 is the condition checked by the inner if statement.
code block 1 is executed if condition1 is true.
code block 2 is executed if both condition1 and condition2 are true.
code block 3 is executed if condition1 is true but condition2 is false.
code block 4 is executed if condition1 is false.
Example:
Switch Statement:
The switch statement allows us to execute a block of code among many alternatives.
You can do the same thing with the if...else statement. However, the syntax of the switch
statement is much easier to read and write.

switch (expression) {
case constant1:
// code to be executed if
// expression is equal to constant1;
break;

case constant2:
// code to be executed if
// expression is equal to constant2;
break;
.
.
.
default:
// code to be executed if
// expression doesn't match any constant
}

How does the switch statement work?


The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding code after the matching label is executed. For example, if
the value of the variable is equal to constant2, the code after case constant2: is executed until
the break statement is encountered.
If there is no match, the code after default: is executed.

LOOP
There may be a situation, when you need to execute a block of code several number of times. In
general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general from of a loop statement in most of the programming languages Loop
Types: C++ programming language provides the following type of loops to handle looping
requirements

For LOOP
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
Syntax The syntax of a for loop in C++ is –
for (initialization; condition; increment/decrement)
{statement(s);}
Here is the flow of control in a for loop –
The initialization step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as long as a
semicolon appears.
• Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the
body of the loop does not execute and flow of control jumps to the next statement just after the
for loop.
• After the body of the for loop executes, the flow of control jumps back up to the increment
statement. This statement can be left blank, as long as a semicolon appears after the condition.
• The condition is now evaluated again. If it is true, the loop executes and the process repeats
itself (body of loop, then increment step, and then again condition). After the condition becomes
false, the for loop terminates.

While Loop
A while loop statement repeatedly executes a target statement as long as a given condition is true.
Syntax
The syntax of a while loop in C++ is
while(condition)
{ statement(s); }
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value. The loop iterates while the
condition is true. When the condition becomes false, program control passes to the line
immediately following the loop.
For Loop
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while
loop checks its condition at the bottom of the loop. A do...while loop is similar to a while loop,
except that a do...while loop is guaranteed to execute at least one time.
Syntax
The syntax of a do...while loop in C++ is
do {
statement(s);
}
while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the
loop execute once before the condition is tested. If the condition is true, the flow of control
jumps back up to do, and the statement(s) in the loop execute again. This process repeats until
the given condition becomes false.

Nested Loop
A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting.
Syntax
The syntax for a nested for loop statement in C++ is as follows –
for ( init; condition; increment )
{ for ( init; condition; increment )
{ statement(s); }
statement(s); // you can put more statements. }
The syntax for a nested while loop statement in C++ is as follows –
while(condition) {
while(condition) {
statement(s); }
statement(s); // you can put more statements. }
The syntax for a nested do...while loop statement in C++ is as follows –
do { statement(s); // you can put more statements.
do { statement(s); }
while( condition ); }
while( condition );

How do we know which loop is used in which situation in c++?


for loop:
• Use when you know the exact number of iterations.
• Typically used for iterating over a range of values or elements in an array.
• Can initialize variables, test conditions, and update variables within the loop control
statement.
while loop:
• Use when the number of iterations is not known beforehand or when the loop condition
depends on runtime conditions.
• Typically used when looping until a certain condition becomes false.

do-while loop:
• Similar to a while loop, but it guarantees that the loop body is executed at least once
before the condition is checked.
• Use when you want to ensure the loop body executes at least once, regardless of the
condition.
Arrays
An array is a data structure that is used to store multiple values of similar data types in a
contiguous memory location.
For example, if we have to store the marks of 4 or 5 students then we can easily store them by
creating 5 different variables but what if we want to store marks of 100 students or say 500
students then it becomes very challenging to create that numbers of variable and manage them.
Now, arrays come into the picture that can do it easily by just creating an array of the required
size.

Properties of Arrays in C++


• An Array is a collection of data of the same data type, stored at a contiguous memory
location.
• Indexing of an array starts from 0. It means the first element is stored at the 0th index, the
second at 1st, and so on.
• Elements of an array can be accessed using their indices.
• Once an array is declared its size remains constant throughout the program.
• An array can have multiple dimensions.
• The number of elements in an array can be determined using the sizeof operator.
• We can find the size of the type of elements stored in an array by subtracting adjacent
addresses.
Array Declaration in C++
In C++, we can declare an array by simply specifying the data type first and then the name of
an array with its size.
Syntax:
data_type array_name[Size_of_array];
int arr[5];
• int: It is the type of data to be stored in the array. We can also use other data types such as
char, float, and double.
• arr: It is the name of the array.
• 5: It is the size of the array which means only 5 elements can be stored in the arra

Initialization of Array in C++


In C++, we can initialize an array in many ways but we will discuss some most common ways to
initialize an array. We can initialize an array at the time of declaration or after declaration.
Initialize Array with Values in C++
We have initialized the array with values. The values enclosed in curly braces ‘{}’ are assigned
to the array. Here, 1 is stored in arr[0], 2 in arr[1], and so on. Here the size of the array is 5.
int arr[5] = {1, 2, 3, 4, 5};

You might also like