1. Junior Member BizHat Newbie
Join Date
Aug 2006
Posts
12

## C Programming: Arrays

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=5
j=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[i], 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.

2. Junior Member BizHat Newbie
Join Date
Jul 2006
Location
Posts
28
Double dimension Array is also just like normal arry,

here is the syntax for that.

int a[][];

//will create double dimension array

you can just simply assign values by using

a[0][0] = 10;
a[0][1] = 20;
a[1][0] = 30;
a[1][1] = 40;

and so on.

So please try this two dimension array also.
If you get any problem then please let me know i will check it out.

3. Junior Member BizHat Newbie
Join Date
Nov 2006
Posts
21
THINKING

Fighting :D

4. Junior Member BizHat Newbie
Join Date
Nov 2006
Location
philippines
Posts
5

## yeah..what is the difference of C and C++?

what is the difference between C and C++..?

5. Junior Member BizHat Newbie
Join Date
Dec 2006
Posts
1
C is a structured programming language but C++ is object oriented language. You can write C programs in C++ but you can not write object oriented programs in C.

6. Junior Member
Join Date
Dec 2006
Posts
4

## Arrays :: Declaration and Syntax

* A simple array of 5 ints would look like:

int ia[5];

This would effectively make an area in memory (if availble) for ia, which is 5 * sizeof(int). We will discuss sizeof() in detail in Dynamic Memory Allocation. Basically sizeof() returns the size of what is being passed. On a typical 32-bit machine, sizeof(int) returns 4 bytes, so we would get a total of 20 bytes of memory for our array.
* How do we reference areas of memory within the array? By using the [ ] we can effectively "dereference" those areas of the array to return values.

printf("%d ", ia[3]);

This would print the fourth element in the array to the screen. Why the fourth? This is because array elements are numbered from 0.
* Note: You cannot initialize an array using a variable. ANSI C does not allow this. For example:

int x = 5;
int ia[x];

This above example is illegal. ANSI C restricts the array intialization size to be constant. So is this legal?

int ia[];

No. The array size is not known at compile time.
* How can we get around this? By using macros we can also make our program more readable!

#define MAX_ARRAY_SIZE 5
/* .... code .... */

int ia[MAX_ARRAY_SIZE];

Now if we wanted to change the array size, all we'd have to do is change the define statement!
* But what if we don't know the size of our array at compile time? That's why we have Dynamic Memory Allocation. More on this later...
* Can we initialize the contents of the array? Yes!

int ia[5] = {0, 1, 3, 4};
int ia[ ] = {0, 2, 1};

Both of these work. The first one, ia is 20 bytes long with 16 bytes initialized to 0, 1, 3, 4. The second one is also valid, 12 bytes initialized to 0, 2, 1. (Examples on a typical 32-bit machine).

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•