1D - Arrays
1D - Arrays
1D - Arrays
TOO
MANY TOO MANY
LINES OF MISTAKES
CODE
WHAT CAN
BE DONE?
ARRAYS
WHAT ARE
ARRAYS?
1 Collection of elements of same data type
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