Professional Documents
Culture Documents
Arrays
Dr. Yousaf, PIEAS
Compute the average marks
What’s about if there are 30 students?
30 variables would be needed to store values of the
marks. marks1, marks2, … marks 30.
What’s about if there are 100 (or 500) students?
100 (or 500) variables would be needed to store
values of the marks. marks1, marks2, … marks 100
(or 500).
Is it practical approach? Can we handle it using just
ONE variable?
int marks[6]={36,78,29,36,7,99};
getchar();
return 0;
}
Dr. Yousaf, PIEAS
How to read values of Arrays?
#include<stdio.h>
int main()
{
int marks[6];
int i;
for (i = 0; i <6;i++)
{
printf(“Please enter the marks of students\n”);
scanf(“ %d",&marks[i]);
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
Single-Dimensional Arrays
Generic declaration:
typename variablename[size]
– typename is any type
– variablename is any legal variable name
– size of array
– For example
int a[10];
– Defines an array of ints with subscripts ranging from 0 to
9
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
• Format:
c[5]
c[6]
arrayname[ position number ] c[7]
– First element at position 0 c[8]
– n element array named c:
c[9]
c[10]
• c[ 0 ], c[ 1 ]...c[ n – 1 ] c[11]
Position number of
the element within
Dr. Yousaf, PIEAS
array c
Arrays
Array elements are like normal variables
c[0] = 3;
printf( "%d", c[ 0 ] );
– Perform operations inside brackets.
If x = 3, then the following comparisons are same.
c[5 - 2] or c[ 3 ] or c[ x ]
#include<stdio.h>
int main()
{
int marks[6]={36,78};
int i;
for (i = 0; i <6;i++)
printf(“%d\n",marks[i]);
getchar();
return 0;
} Dr. Yousaf, PIEAS
Important Points
//C arrays have no bounds checking
#include<stdio.h>
int main()
{
int marks[6]={10,20,30,40,50,60};
int i;
for (i = 0; i <8;i++)
printf(“%d\n",marks[i]);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Important Points
If more values are provided than the size of the array as:
#include<stdio.h>
int main()
{ This program
int marks[6]={10,20,30,40,50,60,70,80}; will give error
int i; and will not
for (i = 0; i <8;i++)
run.
printf(“%d\n",marks[i]);
Error: Too many
getchar(); intializers
return 0;
}
/* Complete It */
(Yourself)
Dr. Yousaf, PIEAS
Write a program that creates an integer array A of 5
elements, initialize the array with a list. The task is to
re‐order this array as specified by user. Ask the user to
enter new order for array elements one by one. For
example if the first number entered by user is 4 for
element 1, this means A[4] should now be the first
element in the array.
(Yourself)
Dr. Yousaf, PIEAS
Write program in which declare an array of size 10. Initialize
the elements from the user. Find largest and smallest numbers
in this array. After the smallest and largest numbers are found,
the program should store the largest number at the beginning
of the array and smallest number at the end of the array . For
example if the name of your array is A, A[0] should contain the
largest number and A[N] should contain the smallest number.
x = 5;
printf(“%d", x);
getchar();
return 0;
}
getchar();
return 0; }