Array
Array
1/20
ARRAYS
An array is a collection of elements of the same type that
are referenced by a common name.
int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998, stuMark999,
studMark1000;
…
…
return 0;
}
By using an array, we just declare like this,
int studMark[1000];
Datatype name[size];
This will reserve 1000 contiguous memory locations for
storing the students’ marks.
//Arrayname[index]; --0
Graphically, this can be depicted as in the following
figure.
This absolutely has simplified our declaration of the
variables.
array_element_data_type
array_name[array_
size];
char cName[30];
Which can be depicted as follows,
int Arr[]={1,3,5,6,8};
printf(“%d\t%d\n”,Arr[1],Arr[2]);
Output: 3 5
#include<stdio.h>
int main(){
int i,sum=0,arr[10];
for(i=0;i<10;i++)
scanf(“%d”,&arr[i]);
for(i=0;i<10;i++)
sum+=arr[i];
printf(“Sum of
input integers is
%d\n”,sum);
return 0;
ASSIGNMENT
Read from user ages of all students in class
(atleast 7) and save them in an array which can
store floating point and find average, minimum
and maximum age.
ASSIGNMENT
int main(){
int i,j,x,arr[10];
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<9;i++){
x=arr[i];
for(j=i+1;j<10;j++){
if(x==arr[j])
printf("%
d
number
appeared
more
than
once\
What is the output of the following code?
int main() {
int arr[3] = {1, 2, 3};
print_array(arr);
return 0;
}
Arrange elements of an array on the
basis of n , such as elements less than n
will be towards left side and greater than
n will be at right side.
#include <stdio.h> else if (arr[j] >= n) {
j--;
void swap(int *a, int *b) { }
else {
int temp = *a; swap(&arr[i], &arr[j]);
*a = *b; }
*b = temp; }
} int main() {
int arr[] = {5, 1, 8, 3, 9, 4, 6, 2};
int n = 5;
void arrange_array(int arr[], int n, int int size = sizeof(arr) / sizeof(arr[0]);
size) { arrange_array(arr, n, size);
int i, j; printf("Array after arranging: ");
for (i = 0, j = size - 1; i < j;) { for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
if (arr[i] < n) {
}
i++; return 0;
} }
• Write a C program to remove duplicate
elements from an array.
#include <stdio.h>
int main()
printf("\nArray Before Removing
Duplicates: ");
{
for (int i = 0; i < n; i++)
int n, count = 0; printf("%d ", arr[i]);
printf("Enter number of elements in the array: ");
scanf("%d", &n);
int arr[n], temp[n];
if(n==0)
{
printf("No element inside the array.");
exit(0);
}
printf("Enter elements in the array: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// To store unique elements in temp printf("\nArray After Removing Duplicates: ")
after removing the duplicate for (int i = 0; i < count; i++)
elements
printf("%d ", temp[i]);
for (int i = 0; i < n; i++)
{ return 0;
int j; }
for (j = 0; j < count; j++)
{
if (arr[i] == temp[j])
break;
}
if (j == count)
{
temp[count] = arr[i];
count++;
}
}
Memory and arrays
• Output: 13 17
What is the output of the following code
snippet?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr+2));
What is the output of the following code
snippet?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[3];
printf("%d", *ptr);
What is the output of the following code
snippet?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *ptr++);
printf("%d", *++ptr);
What is the output of the following code
snippet?
• int arr[5] = {1, 2, 3, 4, 5};
• int *ptr = arr+2;
• printf("%d", *(ptr-1));
Passing array in a function as pointer
int main()
{