Chengwei LEI, Ph.D.    Associate Professor

Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

 

Correctness of An Algorithm

 


 

Is your algorithm correct? Prove that to me!

 


 


 


 

 

Input test case: [13, 4, 24, 7]
Output: 24

maxNum ← -1
FOR num IN numbers {
  if (num > maxNum) {
    maxNum ← num
  }
}


 

 

Input test case: [13, 4, 24, 7]
Output: 24

Input test case: [-13, -4, -24, -7]
Output: -4

maxNum ← numbers[0]
FOR num IN numbers {
  if (num > maxNum) {
    maxNum ← num
  }
}