Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
•The objects are called the elements of the array and are
numbered consecutively 0, 1, 2, 3, ...
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
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
• 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]
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;