HowToProgramC : Lesson 15


Back Home Next



The Keyword ‘const’:

To declare an array, we need its data type, name and size. We use simple integer for the size like 10 or 100. While using arrays in loops, we use the size a lot. Suppose if we have to change the size of the array from 10 to 100, it will have to be changed at all the places. Missing a place will lead to unexpected results. There is another way to deal this situation i.e. keyword construct. The keyword const can be used with any data type and is written before the data type as:

const int arraySize = 100;

This statement creates an identifier arraySize and assigns it the value 100. Now the arraySize is called integer constant. It is not a variable. We cannot change its value in the program. In the array declaration, we can use this as:

int age [arraySize];

Now in the loop condition, we can write like this:

for ( i = 0; i < arraySize ; i ++)

If we have to change the size of the array, we only have to change the value of arraySize where it is declared. The program will work fine in this case. This is a good programming practice to use const for array size.

Sample Code:

Problem Statement:

Write a program which reads positive integers from the user and stores these ones in an array. User can enter a maximum of 100 numbers. Stop taking input when user enters -1.

Solution:

We have to declare an integer array of size 100 to be used to store the integers. We used a loop to get the input from the users. There are two conditions to terminate the loop i.e. either user has entered 100 numbers or user entered -1. 'For' and 'while' loops can execute zero or more times whereas ‘do-while’ may execute one or more times. By analyzing the problem, the loop will be executed at least once so do-while loop logically fits in this problem. We take an integer z to get the input from the user and i as the counter so the condition will be as ( z != -1 && i < 100 ). && is used to enforce that both the conditions are true. If any of the two conditions becomes false, the loop will be terminated. The loop counter is less than 100 because the index of the array will be from 0 to 99. We will read a number from the user and store it at some particular location of the array unless user enters -1 or 100 numbers are entered. In the loop, we will use the if statement
whether the number entered by user is -1 or not. If the number entered is not -1, then we will store it in the array. The index of the array will also be incremented in each repetition. We can assign some value to array element as:

c[ 3 ] = 33;

In an assignment statement, we cannot use expression on the left hand side. Here c[3] is used as a variable which represents the 4th element of the array. The complete code of the program as under:

/* This program reads the input from user and store it into an array and stop at -1.*/
#include <iostream.h>
main( )
{

const int arraySize = 100;
int c [
arraySize ] ;
int i, z;
do
{
int z , i = 0 ;
cout << “Please enter the number (-1 to end input) “ << endl;
cin >> z ;
if ( z != -1 )
{
c[ i ] = z ;

}
i ++ ;
} while ( z != -1 && i <
arraySize ) ;
cout<<“The total number of positive integers entered by user is “<<i-1;
}

The above code shows that the assignment statement of the array is inside the if block. Here the numbers will be assigned to the array elements when the 'if statement' evaluates to true. When the user enters -1, the if statement will evaluate it false. So the assignment statement will not be executed and next i will be incremented. The condition in the 'while loop' will be tested. As the value of z is -1, the loop will be terminated. Now we have to calculate how many positive numbers, the user has entered. In the end, we have incremented i so the actual positive integers entered by the users is i -1. The above example is very useful in terms of its practical usage. Suppose we have to calculate the ages of students of the class. If we don’t know the exact number of students in the class, we can declare an array of integers of larger size and get the ages from the user and use -1 to end the input from the user.

A sample out put of the program is as follow.

Please enter the number (-1 to end input) 1
2
3
4
5
6
-1
The total number of positive integers entered by user is 6



Back Home Next


Google




learntoknow@yahoo.com
© All rights Reserved.