Sei sulla pagina 1di 23

Arrays

•An array is a sequence of objects all of which have the same


type.

•The objects are called the elements of the array and are
numbered consecutively 0, 1, 2, 3, ...

•These numbers are called index values or subscripts of the array.

•To refer to an element


Specify array name and position number (index)
Format: array_name[ position number]
First element at position 0
Name of array (Note that
all elements of this array
have the same name, c)

c[0] -45
c[1] 6
c[2] 0
c[3] 72
c[4] 1543
c[5] -89
c[6] 0
c[7] 62
c[8] -3
c[9] 1
c[10] 6453
c[11] 78

Position number of the element


within array c
Declaring Arrays
• When declaring arrays, specify
– Name
– Type of array
• Any data type
– Number of elements
– type arrayName[ arraySize ];
int c[ 10 ]; // array of 10 integers
float d[ 32 ]; // array of 32 floats

• Declaring multiple arrays of same type


– Use comma separated list, like regular
variables
int b[ 100 ], x[ 27 ];
Example Definitions
Suppose
const int N = 20;
const int M = 40;
const int MaxStringSize = 80;
const int MaxListSize = 1000;
Then the following are all correct array definitions
int A[10]; // array of 10 ints
char B[MaxStringSize]; // array of 80 chars
double C[M*N]; // array of 800 floats
int Values[MaxListSize]; // array of 1000 ints
Rational D[N-15]; // array of 5 Rationals
Suppose
int A[10]; // array of 10 ints A[0], … A[9]
To access individual element must apply a subscript to list name A
 A subscript is a bracketed expression also known as the index

 First element of list has index 0

A[0]
 Second element of list has index 1, and so on

A[1]
 Last element has an index one less than the size of the list

A[9]
 Incorrect indexing is a common error

A[10] // does not exist


Array Elements

• Suppose
int A[10]; // array of 10 uninitialized ints

A -- -- -- -- -- -- -- -- -- --
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

• To access an individual element we must


apply a subscript to list name A
Array Element Manipulation
• Consider
int i = 7, j = 2, k = 4;
A[0] = 1;
A[i] = 5;
A[j] = A[i] + 3;
A[j+1] = A[i] + A[0];
A[A[j]] = 12;

A -- -- -- -- -- -- -- -- -- --
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Ex
• #include<stdio.h>
• #include<conio.h>
• int main()
• {
• int A[21];

• int i = 7, j = 2, k = 4;
• A[0] = 1;
• printf("%d",A[0]);
• printf("\n");
• A[i] = 5;
• printf("%d",A[i]);
• printf("\n");
• A[j] = A[i] + 3;
• printf("%d",A[j]);
• printf("\n");
• A[j+1] = A[i] + A[0];
• printf("%d",A[j+1]);
• printf("\n");
• A[A[j]] = 12;
• printf("%d",A[8]);
• printf("\n");

• getch();
• return 0;
• }
Ex write c program to output array
value
• #include<stdio.h>
• #include<conio.h>
• int main()
• {
• int i;
• int A[10];
• int x[10]={1,2,3,4,5,6,7,8,9,10};
• for( i=0;i<10;i++)
• {
• printf(" you index %d",i);
• printf(" you entered %d",x[i]);
• printf("\n");
• }
• getch();
• return 0;
• }
Ex write c program to output months
• #include <stdio.h>

• void main(void)
• {
• int days[12] = {31, 28, 31, 30,
• 31, 30, 31, 31,
• 30, 31, 30, 31};
• int count;
• for ( count = 0; count < 12; count++)
• {
• printf( "Month %d " , (count + 1));
• printf(" has\n");
• printf("days %d", days[count]);
• printf("\n");
• }
• getch();

• }
Ex write c program to output months

• #include <stdio.h>

• void main(void)
• {
• int days[12];
• int count;
• days[0] = 31; // January
• days[1] = 28; // February
• days[2] = 31; // March
• days[3] = 30; // April
• days[4] = 31; // May
• days[5] = 30; // June
• days[6] = 31; // July
ex
• days[7] = 31; // August
• days[8] = 30; // September
• days[9] = 31; // October
• days[10] = 30; // November
• days[11] = 31; // December
• for (count = 0; count < 12; count++)
• {
• printf( "Month %d" , (count + 1) );
• printf("has \t");
• printf("%d days ", days[count]);
• printf("\n");

• }
• getch();
• return 0;
• }
Program Output
Month 1 has 31 days.
Month 2 has 28 days.
Month 3 has 31 days.
Month 4 has 30 days.
Month 5 has 31 days.
Month 6 has 30 days.
Month 7 has 31 days.
Month 8 has 31 days.
Month 9 has 30 days.
Month 10 has 31 days.
Month 11 has 30 days.
Month 12 has 31 days.
Ex Ex write c program to compute
average
• #include <stdio.h>
• #include<conio.h>
• int main()
• {
• const int N = 10;

• int marks[N], i, n, sum = 0, average;


• printf("Enter n: ");
• scanf("%d", &n);
• for(i=0; i<n; ++i)
• {
• printf("Enter number%d: ",i+1);
• scanf("%d", &marks[i]);
• sum += marks[i];
• }
• average = sum/n;

• printf("Average = %d", average);


• getch();
• return 0;
• }
Ex write c program to compute

maximum of collection number
#include <stdio.h>
• #include<conio.h>
• int main()
• {
• int array[100], maximum, size, c, location = 1;

• printf("Enter the number of elements in array\n");
• scanf("%d", &size);

• printf("Enter %d integers\n", size);

• for (c = 0; c < size; c++)
• scanf("%d", &array[c]);

• maximum = array[0];

• for (c = 1; c < size; c++)
• {
• if (array[c] > maximum)
• {
• maximum = array[c];
• location = c+1;
• }
• }

• printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
• getch();
• return 0;
• }
ex
Ex write c program to compute search
• #include <stdio.h>
• #include<conio.h>
• int main()
• {
• int array[100], search, c, n;

• printf("Enter the number of elements in array\n");
• scanf("%d", &n);

• printf("Enter %d integer(s)\n", n);

• for (c = 0; c < n; c++)
• scanf("%d", &array[c]);

• printf("Enter a number to search\n");
• scanf("%d", &search);

• for (c = 0; c < n; c++)
• {
• if (array[c] == search) /* If required element is found */
• {
• printf("%d is present at location %d.\n", search, c+1);
• break;
• }
• }
• if (c == n)
• printf("%d isn't present in the array.\n", search);
• getch();
• return 0;
• }
ex
Ex write c program to insert element in
array
• #include <stdio.h>
• #include<conio.h>
• int main()
• {
• int array[100], position, c, n, value;
• printf("Enter number of elements in array\n");
• scanf("%d", &n);
• printf("Enter %d elements\n", n);
• for (c = 0; c < n; c++)
• scanf("%d", &array[c]);
• printf("Enter the location where you wish to insert an element\n");
• scanf("%d", &position);
• printf("Enter the value to insert\n");
• scanf("%d", &value);
• for (c = n - 1; c >= position - 1; c--)
• array[c+1] = array[c];
• array[position-1] = value;
• printf("Resultant array is\n");
• for (c = 0; c <= n; c++)
• printf("%d\n", array[c]);
• getch();
ex
Ex write c program to Delete element
in array
• #include <stdio.h>
• #include<conio.h>
• int main()
• {
• int array[100], position, c, n;
• printf("Enter number of elements in array\n");
• scanf("%d", &n);
• printf("Enter %d elements\n", n);
• for ( c = 0 ; c < n ; c++ )
• scanf("%d", &array[c]);
• printf("Enter the location where you wish to delete element\n");
• scanf("%d", &position);
• if ( position >= n+1 )
• printf("Deletion not possible.\n");
• else
• {
• for ( c = position - 1 ; c < n - 1 ; c++ )
• array[c] = array[c+1];
• printf("Resultant array is\n");
• for( c = 0 ; c < n - 1 ; c++ )
• printf("%d\n", array[c]);
• }
• getch();
• return 0;
• }
ex
tasks
1
• 12
• 123
• 1234
• 12345
2- write c program that convert from binary to decimal

• 3-Write c program that sort array.


• 4- write c program that update array
• 5- search about array two dimensional

Potrebbero piacerti anche