0% found this document useful (0 votes)
2 views

Arrays

The document provides an overview of arrays in programming, detailing their characteristics, initialization, and declaration syntax. It includes examples of C programs that demonstrate array operations such as summing elements, counting frequencies, and separating even and odd numbers. Additionally, it covers important points to remember about array indexing and includes multiple-choice questions related to arrays.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays

The document provides an overview of arrays in programming, detailing their characteristics, initialization, and declaration syntax. It includes examples of C programs that demonstrate array operations such as summing elements, counting frequencies, and separating even and odd numbers. Additionally, it covers important points to remember about array indexing and includes multiple-choice questions related to arrays.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Week -6

Arrays

1.Collection of elements of same data type

2.Elements can be stored under one variable name

3 Stored in separate contiguous memory locations

4 Derived data type

ARRAY INITIALIZATION-one way

• Must be initialized otherwise garbage value will be there

num[0] = 10; or 0[num]=10

num[1] = 20; or 1[num]=20

num[2] = 30; or 2[num]=30

num[3] = 40; or 3[num]=40

num[4] = 50; or 4[num]=50

num[0] num[1] num[2] num[3] num[4]

10 20 30 40 50

#include<stdio.h>

int main( ) {

int marks[5];

marks [0] = 80;

marks [1] = 90;

marks [2] = 79;

marks [3] = 85;

marks [4] = 96;

printf(“%d”, marks[0]);

printf(“%d”, marks[1]);

printf(“%d”, marks[2]);

printf(“%d”, marks[3]);

printf(“%d”, marks[4]);

return 0;
}

Initialization and Declaration

• SYNTAX:

data_type variable_name[size] = {list of values};

Example:

• To represent a set of 5 subject marks of a student by a variable

int marks[5] = {80, 90, 79, 85, 96};

marks[0] marks[1] marks[2] marks[3] marks[4]

80 90 79 85 96

Example 1:

int total[5] = {90, 85, 70};

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:

The size may be omitted if the array is initialized during declaration.

int counter[ ] = {1, 1, 1, 1};

It will declare the counter array to contain four elements with initial values 1.

Program 1:Program to find the sum of ‘n’ numbers given in an array

#include<stdio.h>

int main( )

{
int n,i,sum=0;

scanf(“%d”,&n);

int a[n];

for ( i = 0; i < n ; i++)

scanf(“%d”,&a[ i ] );

sum=sum+a[i];

printf(“%d”,sum);

return 0;

Program 2:Write a program to find the frequency of a particular

element in an given array.

Input : a[] = {0, 5, 5, 5, 4}

x=5

Output : The number 5 occurs 3 times

Input : a[] = {1, 2, 3}

x=4

Output : The number 4 occurs 0 times

#include<stdio.h>

int main()

int i, j, array[5], num,count=0;

for(j = 0; j < 5; j++)

scanf("%d", &array[j]);

scanf("%d", &num);

for(i = 0; i < 5; i++)

{
if(array[i] == num)

count = count + 1;

printf("The number %d occurs %d times", num, count);

return 0;

Program 3:Write a program to divide an array into two arrays of even

and odd elements

Input : a[] = {10, 5, 8, 11, 4}

Output : even array = {10, 8, 4}

odd array = {5, 11}

#include <stdio.h>

int main()

int j, i, array[5], even[5], odd[5];

int e = 0, d = 0;

for(j = 0; j < 5; j++)

scanf("%d", &array[j]);

for(i = 0; i < 5; i++)

if(array[i] % 2 == 0)

even[e] = array[i];

e++;

else

odd[d] = array[i];

d++;
}

printf("Even array: ");

for(i = 0; i < e; i++)

printf("%d ", even[i]);

printf(“\nOdd array: ");

for(i = 0; i < e; i++)

printf("%d ", odd[i]);

Program 5:Write the program to count frequency of each element of an

array. Frequency of a particular element will be printed once.

Input : a[] = {10, 5, 8, 10, 5}

Output : 10 occurs 2 times

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

• Array index / subscript always varies from 0 to size-1

• Index must be integer constants, integer variable, or expressions that yield integers

• The elements of the array are always stored in contiguous memory locations.

MULTIPLE CHOICE QUESTIONS

#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

Assuming int is of 4 bytes, what is the size of int arr[15];?

Ans: 60

#include <stdio.h>

int main()

int arr[] = { 1, 2, 3, 4, 5, 6 };

printf("%d", arr[10]);

return 0;

Ans: Garbage value

#include <stdio.h>

int main()

{
int a[2];

printf("%d ", a[3]);

printf("%d ", a[-2]);

return 0;

Ans: Garbage value

#include <stdio.h>

int main()

int i, a[3] = {10, 20, 30};

for(i=0; i < 3;i++){

printf(“%d %p”,a[i], &a[i]);

return 0;

Ans: 10 6040 20 6044 30 6048 (note: 2nd, 4th and 6th values are addresses, that may change)

Does it compile with out error

Ans: Yes
Ans: 4

Ans: 3 2 15

You might also like