assign2 - Program Graphs Assignment Description The purpose...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Program Graphs Assignment Description The purpose ot‘this assignment is to reinforce the material on program graphs from lecture. (1) Draw the program graph and (2) compute the cyclomatic number for each problem below. You must use line numbers to label all nodes in the graph. Do not use the statements or statement fragments themselves as node labels. Problems 1. 2 void probleml() { 3 sll): 4 it‘(cl) { 5 for(inti= l:c2:i++){ 6 s2(): 7 l 8 s3() 9 } 10 else 1] s40: 12} 2. l4 void problem2(){ 15 sl(): lo t‘or(int i:1:cl:i++){ 17 52( )1 l8 it‘(c2){ 1‘) s30: 20 } 2l else 22 540: 23 } 24 55(): 25 if(c3){ 26 $6() 27 } 28 else 29 S70: 30} 3. 34 void problem3() { 35 SH): 36 while (Cl){ 37 520; 38 for (int i = 1; c2: i++){ 39 s30; 40 if(c3) { 4| s40; 42 } 43 else { 44 for (int k=lO;c4; k-—) { 45 s50: 46 } 47 s60: 48 } 49 570; 50 l 5] $8(): 52 } 5.» } 54 55 56 57 4. 58 void problem4(){ 59 if(Cl C2 c3){ 60 510: 61 if(c4 && 05){ 62 520: 63 } 64 else 65 530; ()6 54(3): 67 } 68 else -{ 69 5501 70 illc6 ll c7 && c8) { 71 $60: 72 } 73 else { 74 s7(): 75 } 76 s80: 77 :7 78 s90: 79 } 80 81 5. Below is a binary search program in Data Structure: 2 #include <iostream.h> 3 struct record 4 l int ke): 6 char namel80|z 7 int age: 8 ; 9 // binai‘yfisearcho: 10 // search the record with key k in a list fill. , fln] l l // returns the index ofthe record (0 if not found) 12 int binary_search(struct record *f. int n. int k) 13 { l4 intl=l.u:n.m: l5 int done : 0. i : 0: [6 while ((l<:u) && (!done)) 17 { 18 m = (l+u)/2: l9 it‘(k < flmlkey) 20 u I m- l: 21 else if(k > f'[m].key) 22 l : m+l; 23 else 24 i : m. done : l: 25 l 26 return i: 27 } 28 29 void insert(struct record r. struct record *list. int i) 30 { 3l intj I i: 32 while (r.key < list[i].key) 33 { 34 list[j+l] = listU]; 35 j——: 36 } 37 list[i+l] = r: 38 39 // sort a list ot‘size n into ascending order 40 vord insertion_sort(struct record *list. int n) 41 { 42 int j: 43 list[0].key : -32768: 44 for (i=2;j<:n:j++) 45 insert(list[ij. list._i-l ): 46 } 47 48 49 50 prinLlisqstruct record *1. int n) 5l 52 int i: 53 cout << "(": 54 for (i=1; i<:n: i++) 55 cout << l[i].key << "."; 56 cout << ")\n": 57 } 58 59 60 61 62 main(‘) 63 { 64 int i; 65 im num. v: 66 struct record studentH 1]: 67 f01‘(i:l; i<=10; i++y) 68 student[ij.key : lO-i; 69 cout << "The list before SORTing-——\n": 70 print list(&studem[0]. I0): 71 1nserfiongsort(&studcm[0]. 10): 72 cout << "The list AFTER SORTingu-m”: 73 printjist(&sludent[0]. 10); 74 cout << "Enter the key you want to search: ”: 75 cin >> num: 76 v : binaryisearch(&student[0]. 10. num); 77 00th << "v: " << v << '\n'; 78 } ...
View Full Document

This note was uploaded on 04/05/2010 for the course CS 299 taught by Professor Lopez during the Spring '10 term at Aarhus Universitet, Aarhus.

Page1 / 4

assign2 - Program Graphs Assignment Description The purpose...

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

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