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

Data Structures and Algorithm

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

Data Structures and Algorithm

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

DATA STRUCTURES AND ALGORITHM

✔ Topic: Primitive Data Types – Week 1

Primitive Data Types


● Integer
● Float
● Character
● Boolean

Description: Primitive data types

● Integer:
An integer is a whole number without any fractional or decimal part. It can be
positive, negative, or zero. Integers are commonly used for counting,
indexing, and basic arithmetic operations.

Example: -10, 0, 45

● Float:
A float, or floating-point number, is a number that has a fractional or decimal
component. Floats are used to represent real numbers with precision and are
commonly employed in calculations that require fractions or large ranges of
values.

Example: 3.14, -0.001, 2.718

● Character:
A character represents a single alphanumeric symbol or punctuation mark. It
is usually enclosed in single quotes (') in many programming languages.
Characters are used to represent text or symbols.

Example: 'A', 'b', '1', '?'

● Boolean:
A Boolean data type represents one of two values: true or false. It is used to
represent logical states and is commonly employed in decision-making,
comparisons, and control flow.

Example: true, false

Primitive Data Types in the context of C++:


1. Integer (int)

● Description: Represents whole numbers, both positive and negative, without


any fractional or decimal part.
● Syntax:

int variableName = 42;

● Size: Typically, 4 bytes (32 bits) on most systems, with a range of -


2,147,483,648 to 2,147,483,647 (platform-dependent).
● Examples:

int age = 25;


int temperature = -10;

2. Float (float)

● Description: Represents real numbers with a fractional or decimal part. Used


for values that require precision.
● Syntax:

float variableName = 3.14;

● Size: Typically, 4 bytes, with 6-7 digits of precision.


● Examples:

float pi = 3.14159;
float temperature = -20.5;

3. Character (char)

● Description: Represents a single character, enclosed in single quotes (').


Characters are stored as ASCII values.
● Syntax:

char variableName = 'A';

● Size: Typically, 1 byte (8 bits), which can represent 256 different characters
(ASCII set).
● Examples:

char grade = 'B';


char symbol = '#';
4. Boolean (bool)

● Description: Represents a logical value: either true or false. Used in


decision-making and conditional expressions.
● Syntax:

bool variableName = true;

● Size: Typically, 1 byte, storing 0 (false) or 1 (true).


● Examples:

bool isPassing = true;


bool isComplete = false;
Summary Table:
Data Typical
Description Example Declaration
Type Size

int Whole numbers 4 bytes int number = 42;

Numbers with decimal


float 4 bytes float pi = 3.14;
points

char Single character 1 byte char letter = 'A';

Logical value (true or


bool 1 byte bool isReady = true;
false)

✔ Topic: Array using One-dimensional – Week 2-3


Array using One-dimensional array

An array in C++ is a collection of elements of the same data type stored in


contiguous memory locations. Arrays allow you to store multiple values in a single
variable, making it easier to work with collections of data.
Key Features of C++ Arrays

1. Fixed Size: The size of an array must be defined at the time of its
declaration and cannot be changed during runtime.
2. Homogeneous Data: All elements in an array must be of the same type
(e.g., all integers, floats, or characters).
3. Indexed Access: Elements in an array are accessed using zero-based
indexing (i.e., the first element is at index 0, the second at index 1, and so
on).
4. Efficient Memory Layout: Arrays use contiguous memory locations, which
allows for fast element access using indices.

Syntax
dataType arrayName[arraySize];
● dataType: The type of elements the array will store (e.g., int, float, char).
● arrayName: The name of the array.
● arraySize: The number of elements the array can hold (a positive integer).

Examples
Declaration and Initialization
int numbers[5]; // Declaration of an integer array of size 5
numbers[0] = 10; // Assigning a value to the first element
numbers[1] = 20; // Assigning a value to the second element

// Initialization during declaration


int scores[3] = {95, 87, 92};
Accessing Elements
#include <iostream>
using namespace std;

int main() {
int scores[3] = {95, 87, 92};
cout << "First score: " << scores[0] << endl; // Access first element
cout << "Second score: " << scores[1] << endl; // Access second element
return 0;
}
Iterating Over an Array
#include <iostream>
using namespace std;

int main() {
int scores[5] = {10, 20, 30, 40, 50};

for (int i = 0; i < 5; i++) {


cout << "Element at index " << i << ": " << scores[i] << endl;
}

return 0;
}

Characteristics

● Arrays can store any data type: int, float, char, or even custom types.
● If not explicitly initialized, an array's elements may contain garbage values.
● Multidimensional arrays (e.g., 2D arrays) can also be declared for more
complex data structures.

Advantages

1. Simplifies the management of related data.


2. Provides efficient indexed access to elements.
3. Reduces the need for multiple variables for similar data.

Limitations

1. The size is fixed and cannot be resized dynamically.


2. Arrays lack built-in methods for manipulation (e.g., sorting, inserting
elements).
3. Out-of-bounds access can lead to undefined behavior or runtime errors.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy