POP Unit 3 Part 2
POP Unit 3 Part 2
PROGRAMMING IN C
22CS2ESPOP - UNIT 3 PART 2
Prof. SNEHA S BAGALKOT
Assistant Professor, Dept. of CSE
UNIT – 3: Functions and Arrays
▪ Arrays: Introduction
▪ Declaration of Arrays
▪ Accessing the elements of an Array
▪ Storing values in Arrays
▪ Operations on Arrays (Insertion, Deletion, Searching)
▪ Two-Dimensional Arrays
▪ Transpose of a Matrix
▪ Example Programs.
Arrays: Introduction
• To process large amounts of data, we need a data structure known as Array.
• The elements of the array are stored in consecutive memory locations and
are referenced by an index (also known as the subscript).
Assign values to
individual elements
Initializing Arrays during Declaration
• Arrays are initialized by writing,
type array_name[size]={list of values};
• Example: int marks[5]={90, 82, 78, 95, 88};
• When initializing the array at the time of declaration, size of the
array can be omitted.
• Example: int marks[ ]={90, 82, 78, 95, 88};
int marks[5]={90, 82};
int marks[5]={0};
Inputting Values from the Keyboard
Code for inputting each element of the array
int i, marks[10];
for(i=0; i<10; i++)
scanf(“%d”, &marks[i]);
Assigning Values to Individual Elements
Code to copy an array at the individual element level
Second Dimension
Declaring Two-dimensional array
• A two-dimensional array is declared as:
data_type array_name[row_size][column_size];
1. Row sum
2. Column sum
3. Diagonal sum
Example Programs
Merging
#include <stdio.h> printf("\n The unique elements found in the array are: \n");
for (i = 0; i < n; i++)
void main()
{
{ ctr = 0;
int arr1[100], n, ctr = 0; for (j = 0, k = n; j < k + 1; j++)
{
int i, j, k; if (i != j)
printf(“\n Input the number of elements to be {
stored in the array: "); if (arr1[i] == arr1[j])
{
scanf("%d", &n); ctr++; // Increment the counter when the search value is duplicate
printf("Input %d elements in the array :\n", n); }
}
for (i = 0; i < n; i++)
}
{ if (ctr == 0)
printf("element - %d : ", i); {
printf("%d ", arr1[i]); // Print the unique element
scanf("%d", &arr1[i]); }
} }
}
Example Programs
1. Write a C- Program to read a 1D - array with n elements and to print the same.
2. Write a C- Program to find the sum and average of all the elements in the
array.
3. Write a C- Program to find the largest element and its position in the array.
4. Write a C- Program to generate the Fibonacci series using arrays.
5. Write a C- Program to find the sum of odd elements and even elements stored
in an array.
6. Write a C- Program to print the twin primes up to specified limit.
7. Write a C- Program to generate 100 random integers in the range 1-100, store
them in an array and print the average.
8. Write a C- Program to print the average of the given numbers and also the
numbers greater than the average.
9. Write a C- Program to convert the decimal value to binary value.
10. Write a C- Program to convert the binary value to decimal value.
Example Programs
11. Write a C- Program to find the smallest and largest element in the array.
12. Write a C- Program to evaluate the Polynomial using a0x0+a1x1+a2x2+….+anxn Horner’s method.
13. Write a C- Program to swap the content of two arrays.
14. Write a C- Program to compute the following:
C[0]=a[0]+b[9]
C[1]=a[1]+b[8]
C[2]=a[2]+b[7]
C[3]=a[3]+b[6]
C[4]=a[4]+b[5]
C[5]=a[5]+b[4]
C[6]=a[6]+b[3]
C[7]=a[7]+b[2]
C[8]=a[8]+b[1]
C[9]=a[9]+b[0]