Lecture 19 - Looping an Array | C/C++ Video Tutorials and Lectures for Beginners

preview_player
Показать описание

Transcription
=============

Welcome to this session of my video series!
An array is a collection of similar elements.
The elements are serially indexed starting from zero.
The very fact that there is a sequential indexing, opens a possibility of looping.
A for loop seems to be the best choice here.
The number of elements of an array is always known in advance.
And, as I told you in an earlier chapter, a for loop is the best in such cases.
This is an array of three int, and it is initialized to three values.
This loop runs for three cycles because the size of our array is three.
As x varies, the subscript also keeps changing from 0 to 2.
So, as the loop runs from start to finish each of the elements is printed.
The first item at index 0 is printed.
next to print is the item at index 1.
And, when the loop terminates, the last item at index 2 is also printed.
With these few lines of a for loop we can print an array of any size.
Hundreds of lines can be printed through this code.
Let us see how to write a program to meet this objective.
We have to create an array and store the characters of the English alphabet in it.
We'll see how a for loop can be used to solve this problem easily.
This is the code for this exercise.
Now I'll explain each of the steps one by one.
First of all we have declared an array of size 26.
This array is of the type char because we have to store characters of text.
This is our for loop that will move from 0 to 25.
It will help us cover all the characters of the English alphabet.
On each loop both x and y are incremented.
It serves two purposes.
Firstly, the array index is advanced by one.
And, secondly, the alphabet counter also advances to the next character.
We need a second variable for storing the characters.
We have chosen y as our second variable. It starts from the first alphabet.
The items of the array get initialized to succeeding characters.
The previous problem can be solved in another way also.
We need not use the second variable, y.
This is the alternative.
The loop is written with just one variable.
The other variable is not required.
On each loop this subscript will be incremented as usual.
This is the tricky part.
When x increases by 1, this expression also increases by 1.
The means that this expression also keeps advancing by one character each time.
Hence, the entire alphabet characters get stored one by one.
When x is zero, the element at index zero gets A.
When the value of x is 1, B is stored.
Similarly, C is stored the next time.
And like this the entire array is filled.
First we have to compare the element at index 2 with the element at index 3.
If it is larger, then we have to swap both the numbers.
Then we have to compare the element at index 2 with the element at index 4 and swap...
...if it is larger.
I will write a systematic C program for this.
The work can be split into smaller functions.
Each function should do a specific job.
First write this function to print an array.
This function receives two arguments. The first one is the array itself.
And the second one is the number of elements contained in the array.
Before going further let us first test our function.
Include all the required headers.
This is our code that tests the function.
This statement creates an array and gives it some initial values.
This statement calculates the number of elements in a modular way.
Here we call the function to print our array.
This is the output of our program so far.
We can see that it prints all the elements of the array.
Our next step is to add another function to swap two numbers.
We want the swapping to take place only if the first number is larger.
This function takes its parameters by reference.
The parameters are swapped only if the first is larger than the second.
This code is fairly easy to understand.
It is just the simple swapping algorithm.
We can now write the completed code.
These are the two functions we have written just now.
I have folded their code so that I can show the main function.
These marks indicate that I have hidden my code so that I can show more of the program.
This code prints the original array.
The array is printed before the processing.
This code compares the element at index 2 with the remaining two elements.
Swapping is done if the element at index 2 is larger.
Lastly, we are printing the array after doing all the processing.
The element at index 2 will bubble towards the right.
This code can be made a little better by putting these two statements in a loop.
It might not be a good idea to write a loop for two iterations.
But a loop is surely required if the number of iterations increases.
This is the code that when we use a for loop.
Рекомендации по теме
visit shbcf.ru