by Dinesh Thakur Category: Array

In the HSC examination, there are six subjects each having a maximum of 100 marks. To pass the examination, a student has to score at least 35 marks in each subject. If a student passes, total marks and percentage marks are calculated and a class is awarded based on the percentage marks (perce)as follows:

 
by Dinesh Thakur Category: Array

C language permits the use of multidimensional arrays. It is easy to visualize arrays up to three dimensions. However, we may have difficulty in visualizing a four, five or in general, an n-dimensional array.

 
by Dinesh Thakur Category: Array

When we declare a pointer, it does not point to any specific variable. We must initialize it to point to the desired variable. This is achieved by assigning the address of that variable to the pointer variable, as shown below.

 
by Dinesh Thakur Category: Array

In case of large arrays, the method of comparing every array element with key as described above is rather inefficient. However, if an array is a sorted one, the search process can be considerably shortened by using binary search method. For the application of binary search the array should be first sorted.

 
by Dinesh Thakur Category: Array

Each element of an array is a variable of the type declared for the array. Thus, if an array is declared as int Array [n],all its n elements are of type int, and all the operations that can be done on variables of type int may be carried out on the elements of this array. The same holds for arrays of types float, double, and char. In Program integer array elements are subjected to arithmetic operations.

 
by Dinesh Thakur Category: Array

We can pass an array (one-, two- or multidimensional) as an argument to a function. The C language allows us to define functions that have one or more arrays as parameters. These parameters can be of different types and sizes. Moreover, we can mix scalar and array parameters. A function that uses a single one-dimensional array as a parameter can be defined as shown below.

 
by Dinesh Thakur Category: Array

The procedure of passing more than one arrays to a function is similar to the case of passing a single array. This is illustrated in Program.

 
by Dinesh Thakur Category: Array

As we know, a pointer to type T is analogous to an array of type T.A pointer can be used to represent a vector, as illustrated in Fig. When a pointer is declared, the memory is allocated only for the pointer variable. The memory for the array elements is usually allocated separately using dynamic memory allocation functions, namely, malloc or calloc.

 
by Dinesh Thakur Category: Array

Program sorts the integers in ascending order by using insertion method. The process of sorting starts with picking the second element which is arranged with respect to the first in the specified order (ascending or descending order).

 
by Dinesh Thakur Category: Array

If we desire that the elements of a vector should not be modified, we can declare that vector as a const vector. However, we must initialize this vector when it is declared, as it is not possible to modify it subsequently. Thus, a const vector can be declared and initialized as shown below.

 
by Dinesh Thakur Category: Structure And Union

Structures and Unions, Giving values to members, Initializing structure, Functions and structures, Passing structure to elements to functions, Passing entire function to functions, Arrays of structure, Structure within a structure and Union.

 
by Dinesh Thakur Category: Structure And Union

Consider the problem of comparison of two valid dates d1 and d2. There are three possible outcomes of this comparison: d1 == d2 (dates are equal), d1 > d2 (date d1 is greater, i.e., occurs after d2) and d1 < d2(date d1 is smaller, i.e., occurs before d2). Let us write a function that accepts two dates as structures dl and d2 of type struct date and returns 0 if the dates are equal, 1 if d1 is later than d2 and -1 if date dl is earlier than d2.

 
by Dinesh Thakur Category: Structure And Union

A structure is a user-defined data type containing a collection of logically related data which may be of different types such as int, double, char, and so on. All of them are encapsulated (packed) in a single unit with a single name. The classes of C++ are in fact generalization of C-structures. A class in C++ with only public members is similar to a C-structure. All members of a structure in C are by default public. However, in a class declaration in C++, if there is no access specifier, the members are private by default. We know that arrays can be used to represent a group of data items that belong to the same type, such as int or float. This restriction is not there in structures.

 
by Dinesh Thakur Category: Structure And Union

The C language permits structures to be nested, in which a structure can contain structure members. A structure may be declared inside another structure. Also, a structure may be a data member of another structure. The important thing to note is how to initialize the data members of inner structures and how to access the data members of inner structures. The use of such nested structures facilitates representation and processing of complex data.

 
by Dinesh Thakur Category: Structure And Union

A program to determine the number of words and average word length is given in Program. It uses the strtok function to separate the words in a given string. The program given below uses the same technique to separate the words in a given string and determine and print the frequency of these words.

 
by Dinesh Thakur Category: Structure And Union

The C language allows an array to be used as a structure member. This enables us to group related structure members and is also useful for declaring character strings as structure members. This section explains how we can effectively use such structures. This includes

 
by Dinesh Thakur Category: Structure And Union

Since structures are multivalue, multitype data objects, they can be considered to form a useful tool in manipulation of quantities such as vectors, complex variables, etc. For this we define functions of the type struct, i.e., the return value of the function is a structure. The function may have arguments of type struct besides other arguments. Let us define a structure with three components of vector as its data members as shown below.

 

Page 13 of 16



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.