cp_1_lect_35 - Index Range Checking C does not check the...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
5/10/10 Index Range Checking C does not check the boundary of an array. It is programmers responsibility to check that all references are valid and within the range. Using an invalid index could cause unpredictable results. for (i = 1; i <= 9; i++) scanf (“%d”, &score [i]); main() { int num [40]; Erroneously started at 1 in place of 0 Suicidal: may hang or give unpredictable results
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5/10/10 Example1 /* Read a number series and print it reversed */ #include <stdio.h> int main ( ) { int readNum; int numbers[50]; printf(“Enter up to 50 integers:\n"); printf("How many would you? "); scanf ("%d", &readNum); if (readNum > 50) readNum = 50; printf("Numbers reversed are: \n"); for ( int i=readNum-1,numPrinted=0; i>= 0; i-- ) { printf("%3d", numbers[i]); if (numPrinted < 9) numPrinted++; else { printf("\n"); numPrinted = 0; } } return 0; } Results: Enter up to 50 integers: How many would you? 12 Enter your numbers: 1 2 3 4 5 6 7 8 9 10 11 12 Numbers reversed are: 12 11 10 9 8 7 6 5 4 3 2 1
Background image of page 2
5/10/10 3 Linear search Each item in the list is examined in the order in which it occurs in the list until the desired item is found or the end of the list is reached Algorithm // precondition : N>=1 1. Read a array of N elements named List[0… N-1] and search element key 2. Repeat for i=0 to i=N-1 1. If key equals to List[i] 1. Display element found and stop 3. Display element not found
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5/10/10 44 #include<stdio.h> main() // Linear search implementation { int List[10],index,key; printf("Enter Array elements:"); for(index = 0; index<10; index++) scanf("%d",&List[index]); printf("Enter search element"); scanf("%d", &key); for(index=0;index<10; index++) if(key==List[index]) { printf("Element found at %d location",index); return; }
Background image of page 4
5/10/10 55 Binary search Algorithm // Pre condition: List must be sorted The desired item is first compared to the element in the middle of the list If the desired item is equal to the middle element: No further searches are required If the desired item is greater than the middle element:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

cp_1_lect_35 - Index Range Checking C does not check the...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online