GAME OF CODE’22 — DAY 2

Arrays in C++

In this tutorial, we will learn how to work with arrays, strings, loops, and switch statements.

First, we will learn to declare, initialize, and access array elements in C++ programming with the help of examples.

Introduction To Array

The array is defined as a collection of homogenous data types having fixed-size elements of a similar type. An array can have duplicate values of the same data type. All arrays have contiguous memory locations. There are two types of arrays

1.) One dimensional Array
2.) Multi-dimensional Array

For example:

Suppose a team has 12 players, and we need to store the scores of all of them. Instead of creating 12 separate variables, we can simply create an array:

double score[12];

Here, score is an array that can hold a maximum of 12 elements of double type.

In C++, the size and type of array are fixed after its declaration.

Declaration of Array

In C++, while declaration of an array we need to specify type and the number of the elements followed by [] brackets.

dataType arrayName[arraysize];

FOR EXAMPLE,

int x[10],

here;

int = type of the element
x = name of the array
10 = size of the array

Initialization of array

In C++ we can initiate an array by the following 2 methods

1.) In the first method we will declare the size of an array in [] brackets. For example,

//declare and initialize an array
int x[6] = {22, 13, 17, 6, 26, 4}

2.) In the second method we will not be declaring the size of an array the computer will automatically by default evaluate and compile. For example,

//declare and initialize an array
int x[ ] = {22, 13, 17, 6, 26, 4}

Accessing Array Elements

In C++ in an array each element is linked with a number or in other words it is known as an index. Arrays can be easily accessed from those specific indices. Let us take an example to know more about accessing an array,

//syntax to access an array element
array[index];

let us consider an array x

Array Members — x[0], x[1], x[2], x[3], x[4]
Array Indices — 0, 1, 2, 3, 4

All array indices start from 0. Therefore x[0] will be considered as FIRST ELEMENT.

Strings

Introduction to strings

The string is a type of variable which are used for storing text or contains a collection of characters surrounded by double quotes.
Strings can easily be manipulated during run time. Let us create our first-string together,

Input || Output

Accessing Elements of String

1.) Using the [] operator
The purpose of using the [] operator is to get access to a particular element at a given index just like we use an array.
For Example- Input || Output

2.) Using at () Function

This function also works upon the indices. It ensures whether the element in a particular index is in range or not. If it is not in the given index, it would probably throw an exception.

For Example- Input || Output

Input Functions

  1. getline ()
    This function helps to input the desired sentences or lines into a particular string. It is similar to “cin” function but the only difference is it allows to write more than just a word.
    For Example — Input || Output

2. push_back()
This function allows you to append characters in your string.
For Example- Input || Output

Switch Statement

Switch statement contains an expression of which the value of this executed expression is compared to each case that executes a particular block of code in a program.

For instance, let us consider the following code,
Input

Case 1
In this case, if we enter the digit 10 the output will tell us that we have entered ten. Otherwise, will break the statement and will move on to the next one.

Case 2
In this case, if we enter the digit 20 the output will tell us that we have entered twenty. Otherwise, will break the statement and will move on to the next one.

Case 3
In this case, if we enter the digit 30 the output will tell us that we have entered thirty. Otherwise, will break the statement and will move on to the next one.

Case 4
In this case, if we enter digits other than 10,20, and 30. The output will tell us that we have not entered the required digits and the default statement inside the switch block will be executed.

Loops

In C++ there are three types of loops,

1.) For loop
2.) While loop
3.) Do While loop

1.) FOR LOOP
For loop is an iterative method that allows the programmer to run a section of code a fixed number of times. The for loop runs as long as the test condition remains True.
It is used when the user knows the number of times he wants to execute the statement to get a certain type of output.

Input || Output

2.) WHILE LOOP
While loop is an iterative method that allows the programmer to run a section of code a number of times (not fixed). The while loop runs as long as the test condition remains True.

Input || Output

3.) DO WHILE LOOP
Do while loop is an extended part of a while loop. The loop will execute after the code block is executed once, and then the condition is checked, it will repeat the loop as long as the condition becomes false.

Input || Output

THANK YOU!

Blog Credits : Amandeep Kaur

Post Credits: Vandana chauhan

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store