Jump to content
xisto Community
Sign in to follow this  
jayron

C Programming: Arrays A Clear Description of Arrays

Recommended Posts

C programming provides a capability that enables a user to design a set of similar data types, called Arrays.

 

For understanding the arrays properly, let us consider the following program

main(){int x;j=5j=10;printf("\nj= %d",x);}
No doubt, this program will print the value of j as 10. This is because when a value 10 is assigned to j, the earlier value of j, i.e. 5, is lost. Thus, ordinary variables are capable of holding only one value at a time. However, there are situations in which we would want to store more than one value at a time in a single variable.

 

For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order. In such case we have two options to store these marks in memory.

 

1) Construct 100 variables to store percentage marks obtained by 10 differ students, i.e. each variable containing one student;s marks.

 

2)Construct one variable (called Array or subscripted variable) capable of storing or holding all the hundred values.

 

Obviously, the second alternative is better. A simple reason for this is, it would be much easier to handle one variable than handling 100 different variables. Moreover, there are certain logics that cannot be dealt without the use of an array. An array is a collective name given given to a group of 'similar quantities'. These similar quantities could be percentage marks of 100 students, or salaries of 300 employees, or age of 500 employess. What is important is that the quantity must be 'similar'. Each member in the group is referred to by its position in the group. For example, assume the following group of numbers which represent percentage marks obtained by five students.

perc = {48, 88, 34, 23, 96};

If we want to refer to the second number of the group, the usual notation used is perc(2) similarly, the fourth number of the group is referred as perc(4). However, in C, the fourth number is referred as perc[3]. This is because in C the counting of elements begin with 0 and not with 1. Thus, in this example perc[3] refers to 23 and perc[4] refers to 96. In general, the notation would be perc, where, i can take a value 0,1,2,3 or 4, depending on the position of the element being regerred. Here perc is the subscripted variable (array), whereas 1 is its subscirpt.

 

Thus, an array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc. Usually, the array of characters is a called a string, whereas an array of ints or floats is simply called an array. Remember that all elements of any given array must be the same type, i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.

Edited by jayron (see edit history)

Share this post


Link to post
Share on other sites

Array Declaration

 

To begin with, like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. An example of declaration is:

int marks[30];
Here int specifies the type of variable, just as it does with ordinary variables and the word 'mark' specifies the name of the variable. the [30] however is something to stress on. The number 30 tells how many elements of the type int will be in our array. This number is often called the 'dimension' of the array. The brackets ( [] ) tells the compiler that we are dealing with an array.

 

Accessing Elements of an Array

 

Once an array is declared, let us see how individual elements in the array can be referred. This is done with subscript, the number in the brackets following the array name. This number specifies the elements position in the array. All the array elements are numbered, starting with 0. Thus marks[2] is not the second element of the array, but the third. In our program we are using variable I as a subscript to refer to various elements of the array. This variable can take different values and hence can refer to the different elements in the array in turn.. This ability to use variables as subscripts is what makes arrays so useful.

 

Entering Data into an Array

Here is the section of code that places data into an array:

 

for(i=0;i<=29;i++)  {	 printf(\nEnter Marks);	 scanf(%d,&marks[i]);  }

The for loop cause the process of asking for and receiving a students marks from the user to be repeated 30 times. The first time through the loop, I has a value 0, so the scanf() statement will cause the value type to be stored in the array element marks[0], the first element of the array. The process will be repeated until I becomes 29. This is the last time though the loop, which is a good thing, because there is no array element like marks[30].

 

In the scanf() statement, we have used the address of operator (&) on the element marks of the array. In so doing, we are passing the address of this particular array element to the scanf() function, rather than its value, which is what scanf() requires.

Share this post


Link to post
Share on other sites
A better terminologyC Programming: Arrays

Your post about arrays considers the name of the array as a variable. Further, you indicate that variables can only hold a single value.  I believe these views are inconvenient in the computer science world.  First, multivalued variables exists all the time, both, in mathematics and in systems programming.  In fact, we use one all the time known as $PATH.  The $PATH environment variable has a list of values separated by a field demarcation character.  It is called a multivalued variable.  In complex variables, the square root of  any number is a multivalued variable.

A better way to view arrays is  to consider them as a special case of a "grouping" name.  The C/C++ language offer other examples of "grouping names" such as, the names of "enum" declarations and struct/class declarations. 

In the case of C/C++, the name of the array stands for a group of objects (not just variables).  Furthermore, in C/C++ the array name refers to a single object/variable: a pointer that contains the memory address of the first object/variable of the array.  Consequently, unlike the arrays of traditional computer languages, the arrays in C/C++ are elaborated constructs instead of being a single primitive of the computer language. Many important implications result from this point. 

First, an array implemented by pointers need not be contiguously allocated in memory; thereby, providing extraordinary flexibility to memory management strategies. 

Second, there are no bound checks--no "subscript out-of-range errors".  Consequently, the programmer is responsible for accessing the data correctly and doing its own range checks.  This flexibility empowers the programmer to gain execution speed by avoiding redundant range checks. Consider the case of five arrays of the same size that are accessed using the same set of subscripts.  In the traditional languages, the compiler emits code into the executable to perform five range checks.  In C/C++ using pointers, the programmer need only do one instead of five  range check of the subscripts; thereby, substantially increasing the speed of execution.

Third, accesses may be carried out bypassing the storage map equations, which are very slow in multidimensional situations.

Lastly, the array may be broken down as it is not atomic.  Consequently, more efficient access methods may be implemented by re-casting the pointer.

 In summary, it is best to consider an array name as the name of a single block of objects/variables, where each object/variable may still be accessed individually.

-reply by Rodolfo

 

Share this post


Link to post
Share on other sites
programmingC Programming: Arrays

You are required toWrite a program that takes character values in two different arrays as inputFrom user. After getting input, make a third array and merge both these arrayIn third array. Output array must be sorted in ascending order.

 Detailed Description:

1.  Declare three character type arrays.

2.  Use two arrays to take input from user.

o Take input as char type.

o Take 10 values in each array.

3.  Merge both these input arrays.

o If an input character is in both arrays, thenIt should appear once in resulted array.  

4.  Store result in third array.

5.  Sort resulted array in ascending order.

o Ascending order means string values startingFrom ?a? will come first, and then starting from ?b? and so on.

6.  Display them after sorting

Sample Output

Enter values inFirst array:  a h b c u v I j k e

Enter values inSecond array: y u d f g k I w q a

Merged array:  a y h u b d c f g v k I j w q e

Sorted Array inAscending order:  a b c d e f g h I j k qU v w y

solve it 

-reply by amir

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.