Algorithm

This is a discussion on Algorithm within the C++ Programming forums, part of the General Programming Boards category; This is working algorithm version 1 when Exam is of type Integer Code: // program exits loop when Exam is ...

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Algorithm

    This is working algorithm version 1 when Exam is of type Integer
    Code:
    // program exits loop when Exam is valid or
    // when TERMINATION_VALUE = -99 is entered
    start loop
        output to screen "Enter a score"
        store input to Exam
        exit loop when ( 0 <= Exam <= 100 ) or 
                       when ( Exam = TERMINATION_VALUE)
        output "exam score is 0 .. 100"
    end loop
    Second version with Exam is of type Integer 1 .. 100
    Code:
    // program exits loop when Exam is valid or
    // when TERMINATION_VALUE = -99is entered
    start loop
       begin handler block
           output to screen "Enter a score"
           store input to Exam
           exit 
           exception when CONSTRAINT_ERROR       
               output "exam score is 0 .. 100"
    
       end handler block
    end loop
    Second version exits loop when Exam is valid, but then how to exit when Exam = TERMINATION_VALUE
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Use a do while() loop.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by cyberCLoWn
    Use a do while() loop.
    Not really the answer that I expected. My code could be implemented as do-while loop but when Exam = TERMINATION_VALUE, the CONSTRAINT_ERROR exception catches it (instead of breaking out of the loop) and it's back to looping again
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: Algorithm

    Originally posted by alphaoide
    Second version with Exam is of type Integer 1 .. 100
    Code:
    // program exits loop when Exam is valid or
    // when TERMINATION_VALUE = -99is entered
    start loop
       begin handler block
           output to screen "Enter a score"
           store input to Exam
           exit 
           exception when CONSTRAINT_ERROR       
               output "exam score is 0 .. 100"
    
       end handler block
    end loop
    Second version exits loop when Exam is valid, but then how to exit when Exam = TERMINATION_VALUE
    By your definition,
    1) exam can be only 0 -> 100 (or 1-> 100 depending on which information you actually code)
    2) TERMINATION_VALUE = -99
    since TERMINATION_VALUE is an illegal value, the exception is thrown.

    Try changing to:
    1) exam can be only -1 -> 100
    2) TERMINATION_VALUE = -1
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21