You are on page 1of 15

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Computer Programming
(CS F111)

Lecture No. 18 – Arrays

8/30/2019 1
C program
/* Program to find the maximum of n (n < 50) numbers using arrays */
#include <stdio.h>
int main()
{
int i, n; double arr[50];
printf("Enter total number of elements(1 to 50): ");
scanf("%d", &n);
printf("\n");
/* Stores number entered by the user */
for(i = 0; i < n; ++i) {
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]); }
/* Loop to store largest number at arr[0] */
for(i = 1; i < n; ++i) {
if(arr[0] < arr[i]) Exercise
arr[0] = arr[i]; } Program to find the maximum of n
printf("Largest element = %.2f", arr[0]); numbers using arrays
return 0;
}
C Program to find largest & 2nd largest
include<stdio.h>
#include<conio.h> Check if current array element is
void main() { greater than large,
int a[50]; then make largest element as second
largest and current array element as
int n,i,large,s_large;
largest.
printf("\n Enter number of elements: ");
scanf("%d",&n); Else if the current array element is
printf("\n Enter the elements: "); greater than s_large but less than large
for(i=0;i<n;i++) { then make current array element as
scanf("%d",&a[i]); } second largest i.e. s_large = arr[i]
large=s_large=a[0]; Output
for(i=1;i<n;i++) { Enter number of elements: 5
if(large<a[i]) { Enter the elements: 21 3 66 8 19
s_large=large; First largest = 66
Second largest = 21
large=a[i]; }
else if(s_large<a[i] && a[i] < large)
{s_large=a[i]; } } Exercise
printf("First largest = %d\n", large); Program to find smallest & 2nd
smallest number
printf("Second largest = %d", s_large);
return (0); }
Exercise
C Program to check if there are two numbers in a sequence
whose sum equals a given number k.
Exercise
C program to find if there exists a number in a sequence which is
greater than the sum of all other numbers in the sequence.
C program to print prime numbers in the range 1 to n
Sieve of Eratosthenes
Ancient algorithm for finding all prime numbers up to any given limit.
It follows the following steps to get all the prime numbers from up to n:
• Make an array of all numbers from 2 to n.
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ……., n]

• Starting from 2, delete all of its multiples in the array, except itself.
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,……., n]

• Repeat the step 2 till square root of n.


For 3 – [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20……., n]
For 5 – [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20……., n]
Till sqrt(n)
The remaining array only contains prime numbers.
Sieve of Eratosthenes – C Program
Sieve of Eratosthenes
/* Printing the primes */
int n; int prime[100];
printf("Prime numbers are: \n");
printf("Enter n value: ");
for(int i=2; i <= n; i++)
scanf("%d", &n);
{
/*Loading the array with numbers from 1 to n */
if(prime[i] != -1)
for(int i = 2; i <= n; i++)
{
{
printf("%d ", i);
prime[i] = i;
}
}
}
/*Start with least prime number, which is 2*/
/*No need to check for numbers greater
than square root of n.*/
for(int i = 2; i*i <= n; i++)
{ Input and output for the
if(prime[i] != -1) above program is as follows:
{ Enter n value : 20
/* Mark all the multiples of i as -1 */ Prime numbers are :
for(int j = 2*i; j <=n ; j += i) 2 3 5 7 11 13 17 19
{
prime[j] = -1;
}
}
}
Sieve of Eratosthenes – Running Time
In each iteration, we will mark all the numbers which can be
multiples of current number as NOT-prime
Then running time will be proportional to
N / 2 + N / 3 + N / 5 + N / 7 + N / 11 + ...
< N + N / 2 + N / 3 + N / 4 + N/5 + N/6 + ...
= N(1+ 1/2 + 1/3 + 1/4 + ...)
≈ NlogN = O(NlogN)
With more work can prove that in fact it is O(Nlog(logN))
Array Reversal
Problem
Rearrange the elements in an array so that they
appear in reverse order.
Input Array

1 2 3 4 5 6 7

Output Array
7 6 5 4 3 2 1
Array Reversal – Solution 1
C Program – Array Reversal
/*Printing reverse array*/
#include <stdio.h> printf("Reverse array\n");
int main() { for (i = 0; i < n; i++)
int a[100], b[100], n, i, j; {
printf("Enter the number of elements : "); printf("%d ", b[i]);
scanf("%d", &n); }
/* Input array elements */ return (0); }
for (i = 0; i < n; i++)
{
Output
printf("Enter element %d : ", i + 1);
Enter the number of elements : 6
scanf("%d", &a[i]);
Enter element 1 : 12
} Enter element 2 : 10
/* Copying elements into array b Enter element 3 : 9
starting from end of array a*/ Enter element 4 : 3
for (i = n - 1, j = 0; i >= 0; i--, j++) Enter element 5 : 6
{ Enter element 6 : 5
b[j] = a[i]; Reverse array
} 5 6 3 9 10 12
This method requires additional array of the size of input array size
Question : Can we do better ? (in respect to space requirement)
Array Reversal
Observe we get the following set of exchanges

In terms of suffixes
the exchanges are
a[0] a[6]
1 2 3 4 5 6 7 a[1] a[5]
a[2] a[4]
a[3] a[3]

We observe suffixes on the left are increasing by 1 while at the same time the
suffixes on the right are decreasing by 1.
Increasing suffix can be i & the decreasing suffix can be n - i
Array Reversal – Solution 2
C Program – Array Reversal
/*Printing reverse array*/
#include <stdio.h> printf("Reverse array\n");
int main() { for (i = 0; i < n; i++)
int arr[100], n, i, j, temp; {
printf("Enter the number of elements : "); printf("%d ", arr[i]);
scanf("%d", &n); }
/* Input array elements */ return (0); }
for (i = 0; i < n; i++)
{
Output
printf("Enter element %d : ", i + 1);
Enter the number of elements : 6
scanf("%d", &arr[i]);
Enter element 1 : 12
}
Enter element 2 : 10
/* exchange step*/ Enter element 3 : 9
for (i = 0, j = n - 1; i < n / 2; i++, j--) Enter element 4 : 3
{ Enter element 5 : 6
temp = arr[i]; Enter element 6 : 5
arr[i] = arr[j]; Reverse array
arr[j] = temp; } 5 6 3 9 10 12

This method requires only single additional variable called temp


Linear Search
Problem (Also Called Sequential Search)
Find whether a given number is present in an array and if it
is present then at what location it occurs.

1 2 3 4 5 6 7

k=4
Output: 4
1 2 3 4 5 6 7

k=8
Output: Unsuccessful search
Linear Search – C Program
C Program – Linear Search
/*Unsuccessful Search*/
#include <stdio.h> if (i == n)
int main() { printf("%d isn't present in the array.\n", k);
int arr[100], n, i, k;
printf("Enter the number of elements : "); return (0);
scanf("%d", &n); }
/* Input array elements */
for (i = 0; i < n; i++) Output
{ Enter the number of elements : 6
printf("Enter element %d : ", i + 1); Enter element 1 : 12
scanf("%d", &arr[i]); Enter element 2 : 10
} Enter element 3 : 9
printf("Enter a number to search\n"); Enter element 4 : 3
scanf("%d", &k); Enter element 5 : 6
Enter element 6 : 5
/* search step*/
Enter a number to search
for (i = 0; i < n; i++)
3
{
3 is present at location 4
if (arr[i] == k) /* If required element is found */
{
printf("%d is present at location %d.\n", k, i+1);
break;
}
}
Linear search for multiple occurrences

• Linear search for multiple occurrences


Print all the locations at which required element is found
and also the number of times it occur in the list.

Home Work

You might also like