L4 1 Array
L4 1 Array
Palash Dey
Department of Computer Science & Engg.
Indian Institute of Technology
Kharagpur
x is a 10-element one-
dimensional array
Output:
15 20 1107384350 3221224640
Garbage Address
Programming and Data Structure 7
Declaring Arrays
Array a
x x+k x+2k
min = a[0];
for (i=0; i<10; i++)
{
if (a[i] < min)
min = a[i];
}
printf (\n Minimum is %d, min);
}
main()
{
int a[size], i, min;
• You cannot
– use “=” to assign one array variable to another:
a = b; /* a and b are arrays */
– use “==” to directly compare array variables:
if (a == b) ………
– directly scanf or printf arrays:
printf (”……”, a);
int a[25];
……
for (j=0; j<25; j++)
scanf (%d, &a[j]);
printf (\n);
for (j=0; j<25; j++)
printf ( %d, a[j]);
main()
{
We can also write int n;
float list[100], avg;
float x[100]; :
But the way the function avg = average(n,list);
is written makes it :
general; it works with }
arrays of any size.
float average(int a, float x[])
{
:
sum = sum + x[i];
}
Output:
x=10 y=15
x=10 y=15
int a, *p;
p = &a; /* p is assigned address of ‘a’
(say, 2500) */
p++; /* p will become 2504 */
p = p – 10; /* p will become 2464 */
int x[ ] = {1,2,3,4,5,6,7,8,9,10};
int *p;
p = x + 3; /* Point to 4th element of x */
Output:
x=10 y=15
x=15 y=10