chapter2_5
chapter2_5
* (indirection/dereferencing operator)
• Returns a synonym/alias of what its operand points to
• *yptr returns y (because yptr points to y)
• * can be used for assignment
• Returns alias to an object
*yptr = 7; /* changes y to 7 */
• Dereferenced pointer (operand of *) must be an lvalue (no
constants)
Example
The value of a is 7
The value of *aPtr is 7
BaseType Id [ SizeExp ] ;
double X [ 100 ] ;
Restrictions
• Suppose
const int N = 20;
const int M = 40;
const int MaxStringSize = 80;
const int MaxListSize = 1000;
• Input data
• Print the list
• Find smallest (greatest) value, indexes of smallest
elements of the list
• Search the list with a key
• Sort the list in ascending (descending) order
• Insert an element
• Delete an element
18
Input data into an array
int A[10];
do {
scanf("%d",&n);
while (n<1||n>10) ;
//Input elements
for(i=0;i<n;i++)
scanf("%d",&A[i]);
} }
}
19
Display an array
21
Smallest Value
• Problem
• Find the smallest value in a list of integers
• Input
• a value indicating the number of elements and a list of
number
• Output
• Smallest value in the list
• Note
• List remains unchanged after finding the smallest value!
Necessary Information
• Information to be maintained
• Number of values in array
• Array with values to be inspected for smallest value
• Index of current element being considered
• Smallest value so far
A More Detailed Design
• Solution
• Initialize smallest value so far to first element
• For each of the other elements in the array in turn
• If it is smaller than the smallest value so far, update the
value of the smallest value so far to be the current
element
• Print smallest value
Program
int min;
min=A[0];
for(i=1;i<n;i++)
if(A[i]<min)min=A[i];
printf("\nThe smallest value in the list
is:%d",min);
Indexes of smallest elements
26
Searching
• Problem
• Determine whether a value key is one of the element
values.
• Two possible answer: Found/Not Found
• Does it matter if
• Element values are not necessarily numbers
• Element values are not necessarily unique
• Elements may have key values and other fields
Sequential Searching
• Problem
• Arranging elements so that they are ordered according to
some desired scheme
• Standard is non-decreasing order
• Why don't we say increasing order?
• Major tasks
• Comparisons of elements
• Updates or element movement
Common Sorting Techniques
• Bubble sort
• Iteratively pass through the list and examining adjacent
pairs of elements and if necessary swap them to put them
in order. Repeat the process until no swaps are necessary
• Selection sort
• Sorts an array by repeatedly finding the minimum element
(considering ascending order) from unsorted part and putting it
at the beginning
Selection sort
32
Sorting (ascending order) Selection sort algorithm
3 3
2
1 1 1 1 1
5 5 3
2
5 2 2 2
2 2
3 5
3 5
3 3 3
6 6 6 6 6
5 5
1 1
2 3
2 3
5 5
6 6
33
Example of selection sort
34
Selection Sort
• Insertion sort
• On ith iteration place the ith element with respect to the i-
1 previous elements
• Quick sort
• Divide the list into sublists such that every element in the
left sublist to every element in the right sublist. Repeat
the Quick sort process on the sublists
Insert an element into the array at specified position
Example
int disp [10][10]
40
Multiple dimensional array declaration
Example
int disp [10][10]
41
Example: Read and display elements of a matrix
#include<stdio.h> //Displaying array elements
int main(){
printf(“The matrix:\n");
/* 2D array declaration*/
for(i=0; i<m; i++) {
int M[5][5];
for(j=0;j<n;j++)
int m,n; // actual size of matrix
printf("%4d ", M[i][j]);
int i,j; //Counter variables for the loop
//Reading the matrix printf("\n");
printf("Number of rows:");
scanf("%d",&m); }
printf("Number of columns:"); return 0;
scanf("%d",&n);
}
for(i=0; i<m; i++) {
for(j=0;j<n;j++) {
printf(“M[%d][%d]=", i, j);
scanf("%d", &M[i][j]);
}
}
42