Arrays
Arrays
Arrays
10 20 30 40 50
#include<stdio.h>
int main( ) {
int marks[5];
printf(“%d”, marks[0]);
printf(“%d”, marks[1]);
printf(“%d”, marks[2]);
printf(“%d”, marks[3]);
printf(“%d”, marks[4]);
return 0;
}
• SYNTAX:
Example:
80 90 79 85 96
Example 1:
It will initialize the first three elements to 90, 85, and 70 and the remaining two elements to zero.
i.e. total[0] = 90
total[1] = 85
total[2] = 70
total[3] = 0
total[4] = 0
Example 2:
It will declare the counter array to contain four elements with initial values 1.
#include<stdio.h>
int main( )
{
int n,i,sum=0;
scanf(“%d”,&n);
int a[n];
scanf(“%d”,&a[ i ] );
sum=sum+a[i];
printf(“%d”,sum);
return 0;
x=5
x=4
#include<stdio.h>
int main()
scanf("%d", &array[j]);
scanf("%d", &num);
{
if(array[i] == num)
count = count + 1;
return 0;
#include <stdio.h>
int main()
int e = 0, d = 0;
scanf("%d", &array[j]);
if(array[i] % 2 == 0)
even[e] = array[i];
e++;
else
odd[d] = array[i];
d++;
}
5 occurs 2 times
8 occurs 1 times
#include<stdio.h>
int main()
int n,x,count;
scanf("%d",&n);
int a[n],visit[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
visit[i]=-1;
for(int i=0;i<n;i++)
count=1;
if(visit[i]==0)
continue;
for(int j=i+1;j<n;j++)
if(a[i]==a[j])
count++;
visit[j]=0;
visit[i]=count;
Points to Remember
• Index must be integer constants, integer variable, or expressions that yield integers
• The elements of the array are always stored in contiguous memory locations.
#include <stdio.h>
int main()
int arr[4] = { 1, 2, 3, 4, 5 };
printf("%d", arr[0]);
return 0;
Ans : 1
#include <stdio.h>
int main()
{
int a[3] = { 2, 5, 1 };
printf("%d", a[a[0]]);
return 0;
Ans: 1
#include <stdio.h>
int main()
int arr[1] = { 5 };
printf("%d", 0[arr]);
return 0;
Ans: 5
Ans: 60
#include <stdio.h>
int main()
int arr[] = { 1, 2, 3, 4, 5, 6 };
printf("%d", arr[10]);
return 0;
#include <stdio.h>
int main()
{
int a[2];
return 0;
#include <stdio.h>
int main()
return 0;
Ans: 10 6040 20 6044 30 6048 (note: 2nd, 4th and 6th values are addresses, that may change)
Ans: Yes
Ans: 4
Ans: 3 2 15