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

Objective & Declaration

To represent list of data


CGPA of students in a class
List of salary of employees
Marks of Quiz 1

datatpye array_name[array_size];

float cgpa[35];
int nums[5] = {23,56,78,12,98}; // with initialization

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Array Index Index

0 1 2 3 4 5 6 7 8 9
23 56 78 12 98 … … … … 75

Index starts at 0 and ends at (size-1)

nums[3] = 34;
n = nums[5];
scanf(“%d”, &nums[2]);
printf((“%d”, nums[4]);
C Programming Language Slides, Prepared by Dr. Rajesh Palit
Input and Output
int main(void)
{
int list[5];
int i;
for (i=0; i<5; i++) {
scanf(“%d”, &list[i]);
}

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


printf(“%d ”, list[i]);
}
}

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Find the sum and average of all elements.

int main(void)
{
int list[5]= {12,34,56,78,90};
int i, sum = 0;
float avg;
for (i=0; i<5; i++) {
sum = sum + list[i];
}
avg = sum / 5.0;
printf(“%d %f\n”, sum, avg);
return 0;
}

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Display the items that are greater than
avg.
int main(void)
{
int list[5]= {12,34,56,78,90};

int i, sum = 0;
float avg;
for (i=0; i<5; i++) {
sum = sum + list[i];
}
avg = sum / 5;
for (i=0; i<5; i++) {
if (list[i]>avg) printf(“%d ”, list[i]);
}

return 0;
}

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Find the largest element in an array.
#define n 5

int main(void)
{
int list[n]= {12,34,56,78,90};
int i,m = 0;
for (i=1; i<n; i++) {
if (list[i]>list[m]) m = i;
}
printf(“Largest Element: %d\n”, list[m]);
return 0;
}

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Searching for an Item & Frequency
12 78 56 78 90

int nums[n] = {11,78,103,78,43};


int item, i, count;

scanf("%d", &item);
count = 0;
for (i=0; i<n; i++) {
if (nums[i]==item) {
count = count + 1;
}
}
if (count==0) printf("NOT Found\n");
else printf("Found: %d time(s).\n", count);

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Delete the element at index p from an array.
#define n 5

int main(void)
{
int list[n]= {12,34,56,78,90};
int i, p = 2;
for (i=p; i<n-1; i++) {
list[i] = list[i+1];
}
list[n-1] = -1; 12 34 56 78 90
return 0; 12 34 78 90 90
}
12 34 78 90 -1

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Insert an element, p at index, k in an array.
#define n 5

int main(void)
{
int list[n]= {12,34,78,90,-1};
int i, p = 1, k = 17;
for (i=n-1; i>p; i--) {
list[i] = list[i-1];
}
list[p] = k;
12 34 78 90 -1
return 0;
} 12 34 34 78 90

12 17 34 78 90
C Programming Language Slides, Prepared by Dr. Rajesh Palit
Passing Array to a function
int cumulativeFreq(int a[], int z)
{
int i;
for (i=1;i<z; i++) {
a[i] = a[i] + a[i-1];
}
return 0; 3 7 4 8 11
}
3 10 14 22 33

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Reversing Array Elements
void reverse(int x[], int n)
{
int i=0, j=n-1, t;
while (i<j) {
t=x[i]; x[i]=x[j]; x[j]=t;
i = i + 1;
j = j - 1;
}
return;
}

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Sorting - Arranging Array Elements
• Selecttion Sort 23 42 4 16 8 15
• Bubble Sort
• Insertion Sort
4 8 15 16 23 42
• Merge Sort
Ascending order
• Shell Sort
• Quick Sort 42 23 16 15 8 4
• Radix Sort Descending order

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Selection Sort 0 1 2 3 4 5
6 3 1 9 7 2
1. Select largest element
2. Place it in first position 1 3 6 9 7 2
3. Select 2nd largest element
4. place it in 2nd position 1 2 6 9 7 3
5. and so on
1 2 3 9 7 6
for (i=0;i<z; i++) {
Select candidate
1 2 15
3 6 7 9
Place the candidate
}
1 2 3 6 7 9

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Sorting – Selection Sort
int selection_sort(int a[], int z)
{
int i,j;
for (i=0;i<z; i++) {
m = i;
find the
for (j=i+1; j<z; j++) { smallest
if (a[j]<a[m]) m = j; item
}
t = a[i]; a[i] = a[m]; a[m] = t; swapping
}
23 42 4 16 8 15
} 4 42 23 16 8 15

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Sorting – Bubble Sort
int bubble_sort(int a[], int z)
{
int i,j;
for (i=0;i<z; i++) {
for (j=0; j<z-1-i; j++) {
if (a[j]>a[j+1]) {
t = a[j]; a[j] = a[j+1]; a[j+1] = t;
}
}
}
}

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Two Dimensional Array
2 1 4 8
6 9 7 3
5 8 2 5
2D array with 3 rows and 4 columns
row index 1, column index 2
So, list[1][2]
int m[3][4];
Row by row, it’s called row major
int list[3][4] = {{2,1,4,8},{6,9,7,3},{5,8,2,5}};
int xyz[][4] = {{2,1,4,8},{6,9,7,3},{5,8,2,5}};

row 0 row 1 row 2

[0][0] [0][1] [0][2] [0][3] [1][0] [1][1] [1][2] [1][3] [2][0] [2][1] [2][2] [2][3]

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Input & Output – 2D Array
int main(void) int main(void)
{ {
int i, j; int i, j;
int list[r][c]; int list[r][c];

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


for (j=0; j<c; j++) { for (j=0; j<c; j++) {
scanf(“%d”, &list[i][j]); printf(“%d ”, list[i][j]);
} }
} printf(“\n”);
return 0; }
} return 0;
}

C Programming Language Slides, Prepared by Dr. Rajesh Palit


2D Array Manipulation
int main(void)
{
• Subtraction
int i, j; • Matrix multiplication
int a[r][c], b[r][c];
int m[r][c]; • Transpose
• Filling a square matrix above and
for (i=0; i<r; i++) {
for (j=0; j<c; j++) {
below diagonal
m[i][j] = a[i][j]+b[i][j]; • Exchanging two rows or columns
}
}
return 0;
}

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Matrix Transpose
a
int i, j;
1 5 3
/* Transfer values to the
4 2 6 transpose matrix. */

b for(i=0; i<NROWS; i++) {


1 4 for(j=0; j<NCOLS; j++) {
b[j][i] = a[i][j];
5 2 }
}
3 6 return;

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Matrix Multiplication

1 2 3 7 8

4 5 6 9 10

A => r1 X c1 11 12
B => r2 X c2
1 x 7 + 2 x 9 + 3 x 11 1 x 8 + 2 x 10 + 3 x 12
4 x 7 + 5 x 9 + 6 x 11 4 x 8 + 5 x 10 + 6 x 12

C => r1 X c2

C Programming Language Slides, Prepared by Dr. Rajesh Palit


Matrix Multiplication

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


for (j=0; j<c2; j++) {
sum = 0;
for (k=0; k<c1; k++ ) { // or, k<r2
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
}
}
C Programming Language Slides, Prepared by Dr. Rajesh Palit
Passing 2D arrays to a function
void print_m(int m[3][4], void print_m(int r, int c, int m[r][c])
int r, int c) {
int i,j;
for (i=0; i < r; i++) {
void print_m(int m[][4], for (j=0; j < c; j++)
int r, int c) printf("%.5d ",m[i][j]);
{ printf("\n");
int i,j; }
for (i=0; i < r; i++) { printf("\n");
for (j=0; j < c; j++) return;
printf("%.5d ",m[i][j]); }
printf("\n");
}
printf("\n");
return;
}
C Programming Language Slides, Prepared by Dr. Rajesh Palit

You might also like