Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

One Dimensional Arrays

#include < stdio.h >


int main() I want to nd the
HOW? { average mark of my
int m1, m2, m3, …………, m50; class for PL subject
scanf(“%d”,&m1);
scanf(“%d”,&m2);
.
.
scanf(“%d”,&m50);
int avg;
avg=(m1+m2+…….m50)/50;
return 0; Class has 50
IMPLEMENT? } students
TIME TOO MANY
CONSUMIN VARIABLES
G DECLARATION

TOO
MANY TOO MANY
LINES OF MISTAKES
CODE
WHAT CAN
BE DONE?
ARRAYS
WHAT ARE
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


HENCE TO STORE..
ROLL NOS MARK 1 EMPLOYEE ID SALARY
170301061 89 CS01 50000
170301062 87 CS02 45000
170301063 67 CS03 25000
170301064 54 CS04 10000
170301065 34 CS05 20000
170301066 56 CS06 45000
170301067 78 CS07 25000
170301068 90 CS08 50000
ARRAY DECLARATION marks[-1]?
SYNTAX:
data_type variable_name[size];
Example:
To represent a set of 5 subject marks of a student by a variable
int marks[5];

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


Value Garbage Garbage Garbage Garbage Garbage
Value Value Value Value Value
Memory 2000 2004 2008 2012 2016
Address (base
address)
Array index / subscript always starts from 0 to size-1
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
Program to Store 5 subject marks of a student and print the same
#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]
marks[0] marks[1] =marks[2]
{80, 90, 79,marks[3]
85, 96};marks[4]
80 90 79 85 96
Examples
Example 1:
int total[5] = {90, 85, 70};
It will initialize the rst 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
Examples
Example 2:
The size may be omi ed 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 Store 5 subject marks of a student and print
the same
#include<stdio.h>
int main( ) {
int i;
int marks[5] = {80, 90, 79, 85, 96};
for ( i = 0; i < 5 ; i++) {
printf(“%d”, marks [ i ] );
}
return 0;
}
Program 2:Program to nd the sum of ‘n’ numbers
#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 3:Write a program to nd 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
/*to nd the frequency of a particular element in an array.*/
#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 4: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}

/*to divide an array into two arrays of even and odd elements*/
#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++;
}
Program 5:Write a program to nd the rst repeated element in an
array of size 5
Input : a[] = {10, 5, 8, 10, 5}
Output : 10 repeated at index 3
Input : a[] = {1, 5, 10, 4, 50}
Output : There is no repeated element
/*to nd the rst repeated element in an array of size 5*/
#include <stdio.h>
int main()
{
int arr[5];
int i, j n = 5,index=-1,ele;
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for(i = 0; i < n-1; i++)
{
for(j = i + 1; j < n; j++)
{
if(arr[i] == arr[j])
{
ele = arr[j];
index = j;


break;
}
}
}
if(index != -1)
printf("%d repeated at index %d\n",ele, index);
else
printf("There is no repeated element\n");
return 0;
}
Program 6: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
Write the program
/*to count frequency of each element
for(intofi=0;i<n;i++)
an array. Frequency
of a particular element will be printed
{ once.*/
#include<stdio.h> count=1;
int main() if(visit[i]==0)
{ continue;
int n,x,count; for(int j=i+1;j<n;j++)
scanf("%d",&n); {
int a[n],visit[n]; if(a[i]==a[j])
for(int i=0;i<n;i++) {
{ count++;
scanf("%d",&a[i]);
visit[j]=0;
visit[i]=-1;
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;
}
A. 1
B. 2
C. No output
D.Answer:
Compilation
D error
MULTIPLE CHOICE QUESTIONS
#include <stdio.h>
int main()
{
int a[3] = { 2, 5, 1 };
printf("%d", a[a[0]]);
return 0;
}
A. 0
B. 1
C. 2
D. Compilation
Answer: B error
MULTIPLE CHOICE QUESTIONS
#include <stdio.h>
int main()
{
int arr[1] = { 5 };
printf("%d", 0[arr]);
return 0;
}
A. 0
B. 1
C. 5
D. SyntaxC error
Answer:
MULTIPLE CHOICE QUESTIONS
Assuming int is of 4 bytes, what is the size of int arr[15
];?
A. 15
B. 19
C. 11
D. 60
Answer: D
MULTIPLE CHOICE QUESTIONS
#include <stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
printf("%d", arr[10]);
return 0;
}
A. Garbage value
B. 0
C. 1
D.Answer:
Compiler
A dependent
MULTIPLE CHOICE QUESTIONS
#include <stdio.h>
int main()
{
int a[2];
printf("%d ", a[3]);
printf("%d ", a[-2]);
return 0;
}
A. Compile time Error
B. Run time Error
C. 0 0
D. garbage
Answer: D garbage
MULTIPLE CHOICE QUESTIONS
#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;
}
A. Syntax Error
B. 10 6040 20 6044 30 6048
C.Answer:
10 6040B 20 6048 30 6030
WHAT WILL BE THE OUTPUT?
#include <stdio.h>
int main()
{
int array[26], i;
for (i = 0; i<= 25; i++)
{
array[i] = 'A' + i;
printf("%d %c\n", array[i], array[i]);
}
return 0;
}
Answer : Yes
Answer : B
Answer : D
Answer : A
Answer : C
Answer : D

You might also like