Lecture 10 - Arrays
Lecture 10 - Arrays
• Multi-line comments
/*
Addition Of Two Numbers
By Bill Gates
© Microsoft Corporation
*/
int a=(b*c + b*d)/b; int a = c + d;
However…..
Arrays
One variable many data
Problem:
Read 10 numbers from the keyboard and store them
100 Problem:
Read 10 numbers from the keyboard and store them
// solution #1
int a, b, c, d, e, f, g, h, i, j;
0 1 2 3 4 5 6 7 8 9
a 79 87 94 82 67 98 87 81 74 91
For example:
int a[10];
a is an array of 10 integers.
float prices[3];
char c[6];
c is an array of 6 characters.
How to assign values?
There are 3 ways.
How to assign values?
First way
• It is possible to initialize an array when it is declared:
int a[6];
a[0]=3;
a[1]=6;
How to assign values?
Third way:
• Use scanf to input in the array:
int a[6];
scanf(“%d”, &a[0]);
scanf(“%d”, &a[1]);
……..
scanf(“%d”, &a[5]);
int a[6];
scanf(“%d”, &a[i]);
}
Arrays: Some easy examples
• Example 1: Suppose an array has 5 students’
marks. Find average mark.
4 40 30 70 60 70 88 99 10 87 91 65
Large
r?
Initially assume first element is the maximum
max = a[0]
#define N 12
int main()
{
int a[N] = { 14, 21, 36, 14, 12, 9, 8, 22, 7, 81, 77, 10};
int i;
4 21 36 14 62 91 8 22 7 81 77 10
secL = 4 largest = 21
Third element
t = a[2]
4 4 6 7 7 7 8 9 10 10 10 10
t = a[0]
t == a[1] t != a[1]
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 4, 4, 6, 6, 7, 7, 7, 8, 9, 10, 10, 10, 10};
int i, counter;
counter = N;
for(i=0; i< N-1; i++){
if(a[i] == a[i+1])
counter--;
}
printf("The number of distinct elements in the array is
%d.\n\n", counter);
}
Some Harder Examples
4 6 6 4 7 8 10 7 8 10 4 9
t = a[0]
counter = N;
for(i=0; i< N-1; i++){
for (j = i+1; j < N; j++){
if(a[i] == a[j]){
counter--;
break;
}
}
}
printf("The number of distinct elements in the array is
%d.\n\n", counter);
}
Some Harder Examples
4 6 6 4 7 8 10 7 8 10 4 9
t = a[0]
Desired output:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
6 6 4 7 8 10 7 8 10 4 9 4
Code snippet
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 4, 6, 6, 4, 7, 8, 10, 7, 8, 10, 4, 9};
int i, t;
t = a[0];
for(i=0; i < N-1; i++)
a[i] = a[i+1];
a[N-1] = t;
printf(" Array elements after left rotation……..\n");
linear_search.c
Linear_Search.c
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 4, 21, 36, 14, 62, 91, 8, 22, 7, 81, 77, 10};
int i;
Target 19
21 > 19
Problem:
Find the index of a number in a
sorted array of integers
LinearSearch_InSortedArray.c
LinearSearch_InSortedArray.c
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N]= { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91};
int target = 62; //int target = 72;// Try this target next
int i, idx=-1;
for(i=0; i< N; i++)
{
if(a[i] == target)
{
idx=i;
break;
}
else if(a[i]>target)
break; // we can stop here
}
if(idx == -1)
printf("Target not found.\n\n");
else
printf("Target found at index: %d. \n\n", idx);
}
Analysis
int main()
{
int a[N]= { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91}; //sorted in increasing order
int i;
int target = 22; //int target = 72; // Try this target next
int idx=-1; // if the target is found its index is stored here
if(idx == -1)
printf("Target not found.\n\n");
else
printf("Target found at index: %d \n\n", idx);
}
Problem:
Find the all occurrences of a number in
an array and replace it with a new value.
search_and_replace.c
Linear_Search.c
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 4, 21, 36, 14, 62, 91, 8, 22, 7, 81, 62, 10};
int i;
int target = 62;
int newValue = 65;
int count=0;
int idx[5]; // a helper array that keeps the indexes of all entries == target value
int found=0;
if(found == 0)
printf("Not found!\n\n");
else
{
printf("Found it a total of %d times.\n", count);
for(i=0; i< count; i++)
printf("\t Found @ index %d \n", idx[i]);
}
// Now replace all found occurences with a nother number
for(i=0; i< count; i++)
a[ idx[i] ] = newValue;
system("pause");
}
Selection Sort
(Cards Example)
Initial Configuration
Sorted
Selection Sort
Example:
Selection
Sort
Example: SelectionSort.c
#include <stdio.h>
#define N 6
int main()
{
int a[N]= { 23, 78, 45, 8, 32, 56};
int i,j,tmp;
// Sort the array using Selection Sort
int idx,min;
for(i=0; i < N-1; i++)
{
min=a[i];
idx = i;
for(j=i+1; j < N; j++)
if(a[j] < min){
idx = j;
min = a[j];
}
tmp = a[i];
a[i] = min;
a[idx] = tmp;
}
for(i = 0; i < N; i++)
printf("%d\n",a[i]);
}
Questions?