C Tutorial/Array/Array Introduction

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Addition of the elements of the list

<source lang="cpp">#include<stdio.h>

  1. include<conio.h>

void main() {

  int a[5],i = 5,sum=0;
  printf("Enter the elements of list \n");
  int j;
  for(j=0;j<i;j++)
    scanf("%d",&a[j]);
  fflush(stdin);
  printf("The list elements are \n");
  for(j=0;j<i;j++)
     printf("%d ",a[j]);
  printf("\n");
  for(i=0;i<5;i++)
  {
     sum+=a[i];
  }
  printf("The sum of the elements of the list is %d\n",sum);

}</source>

Enter the elements of list
1
2
3
4
5
The list elements are
1 2 3 4 5
The sum of the elements of the list is 15

Address of each element in an array

Array elements occupy consecutive memory locations.


<source lang="cpp">#include <stdio.h> main() {

   int a[5];
   int i;
   for(i = 0;i<5;i++)
   {
       a[i]=i;
   }
   for(i = 0;i<5;i++)
   {
       printf("value in array %d\n",a[i]);
   }
   for(i = 0;i<5;i++)
   {
       printf("value in array %d and address is %16lu\n",a[i],&a[i]);
   }

}</source>

value in array 0
      value in array 1
      value in array 2
      value in array 3
      value in array 4
      value in array 0 and address is           631656
      value in array 1 and address is           631660
      value in array 2 and address is           631664
      value in array 3 and address is           631668
      value in array 4 and address is           631672

Arrays

An array is a data structure process multiple elements with the same data type.

Array elements are accessed using subscript.

The valid range of subscript is 0 to size -1.


<source lang="cpp">#include <stdio.h> main() {

   int a[5];
   int i;
   for(i = 0;i<5;i++)
   {
      a[i]=i;
   }
   for(i = 0;i<5;i++)
   {
       printf("value in array %d\n",a[i]);
   }

}</source>

value in array 0
      value in array 1
      value in array 2
      value in array 3
      value in array 4