Array Data Structure
Array Data Structure
Array Data Structure
▪ In computer science, an array data structure is a basic idea whereby a collection of elements is
kept in a contiguous block of memory. It is extensively applied in programming for data
organization and manipulation and provides effective access to elements utilizing indices.
▪ Every element in an array has a 0-based index. Every element in an array is reachable via its index.
▪ An array is defined as a set of finite number of homogeneous elements or same data items.
▪ The first example declares two arrays named xNum and yNum
of type int. Array xNum can store up to 20 integer numbers
while yNum can store up to 50 numbers.
For example:
int Arr[]={1,3,5,6,8};
printf(“%d\t%d\n”,Arr[1],Arr[2]);
Output: 3 5
A R R AY
EXAMPLE
Take 10 integer input from user and store then in an
array and find the sum of all numbers stored in array.
#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;
}
Wr i te a pro g ra m t h a t f i n d s o u t
Problem i f a n I n te g e r i s re p e a te d m o re
#include<stdio.h> t h a n o n c e i n l i s t o f 1 0 i n te g e r s
int main(){
int i,j,x,arr[10];
a u s e r i n s e r te d .
printf("Enter 10 integer Enter 10 integer number
number\n"); for(i=0;i<10;i++){ 23
scanf("%d",&arr[i]); 34
5
}
6
for(i=0;i<9;i++){ 7
x=arr[i]; 7
for(j=i+1;j<10;j++ 6
){ 34
9
if(x==arr[j]) 8
printf("%d number appeared more than 34 number appeared more than once
once\n",x); 6 number appeared more than once
7 number appeared more than once
}
} --------------------------------
return 0; Process exited after 7.535 seconds with return value 0
Press any key to continue . . .
}
Pro b le m
A six faced die is rolled 600 times. Find the frequency of the occurrence of
each face?
➢ This value is generally used as a unique seed because it changes // Generate and print five random
numbers
every second, ensuring that the random number sequence generated
for (int i = 0; i < 5; i++) {
by rand() is different each time the program is run. printf("%d\n", rand());
}
➢ srand(time(0)) seeds the random number generator with the current
time. return 0;
}
➢ This makes the sequence of numbers generated by rand() different
each time the program is executed.
#include<stdio.h> 1 came out for 108 times
2 came out for 96 times
3 came out for 96 times
int main(){ 4 came out for 90 times
int i,j,arr[7]={0}; 5 came out for 109 times
srand(time(0)); 6 came out for 101 times
for (i=0;i<600;i++){
j=rand()%6; --------------------------------
Process exited after 0.07046 seconds with return value 0
j=j+1; Press any key to continue . . .
arr[j]++;
}
for(i=1;i<=6;i++)
printf("%d came out for %d times\n",i,arr[i]);
return 0;
}
Problem
Remove an element from a specific position in the array and shift
the remaining elements accordingly
#include <stdio.h>
// Function to remove an element from a specific position
// Shift the elements to the left from the position
// Reduce the size of the array
// Print the updated array
int main() {
// Input the size of the array
// Input the elements of the array
// Input the position of the element to remove
// Call the function to remove the element
return 0;
}
Problem
Remove an element from a specific position in the array and shift
the remaining elements accordingly
#include <stdio.h>
// Print the updated array
printf("Array after removing the element at position %d:\n", pos);
// Function to remove an element from a specific position
for (int i = 0; i < n; i++) {
void removeElement(int arr[], int n, int pos) {
printf("%d ", arr[i]);
// Check if the position is valid
}
if (pos < 0 || pos >= n) {
printf("\n");
printf("Invalid position!\n");
}
return;
}
int main() {
// Shift the elements to the left from the position int n, pos;
for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1]; // Input the size of the array
} printf("Enter the size of the array: ");
scanf("%d", &n);
// Reduce the size of the array
n--; int arr[n];
Pro b le m
Remove an element from a specific position in the array and shift
the remaining elements accordingly
return 0;
}
Assignment 1
▪ For examples,
⚫ int xInteger[3][4];
⚫ float matrixNum[20][25];
int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };
OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second
dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }
LIST OF STUDENTS AND THEIR S U B J E C T
MARKS
Marks
10 23 31 11
Students 20 43 21 21
12 22 30 13
30 31 26 41
13 03 41 15
int main(void)
{
int i, j;
double total;
int marks[ROW][COL]= { 10, 23, 31, 11, 20, 43, 21, 21,12,
22, 30, 13, 30, 31, 26, 41,13, 03, 41, 15 };
for(i = 0; i < ROW ; i++)
{
total = 0.0;
for (j=0; j<COL; j++)
total+=marks[i][j];
printf("Average of student %d is %f \n", i, total/4.0);
}
}
▪ For array storing string
#include<stdio.h>
int main(){
int
i,j,k,x[][2][3]={{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<3;k++){
printf("x[%d,%d,%d]=%d\n",i,j,k,x[i][j][k]);
}
return 0;
}
Sa mple P ro b le ms
Print Transpose of a Matrix
scanf("%d", &a[i][j]);
}
scanf("%d",&b[i][j]);
}
⚫ //Initializing all elements of result matrix to 0
⚫ for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
result[i][j] = 0;
}
⚫ result[i][j]+=a[i][k]*b[k][j];
}
⚫ //Displaying the result
⚫ printf("\nOutput Matrix:\n");
⚫ for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
printf("%d ", result[i][j]);
}
⚫ return 0;
}
Thanks!