Top-Rated Free Essay
Preview

Lab 7 Student

Satisfactory Essays
1242 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Lab 7 Student
Lab 7: Repetition Structures I
This lab accompanies Chapter 5 (pp. 163-183 and pp. 196-201) of Starting Out with Programming Logic & Design.

Name: ___________________________

Lab 7.1 –Condition Controlled with While and Do-While Loops: Pseudocode

Critical Review

A repetition structure causes a statement or set of statements to execute repeatedly.

Repetition structures are used to perform the same task over and over.

Repetition structures are commonly called loops

A condition-controlled loop uses a true/false condition to control the number of times that it repeats.

The general structure of a While loop with a condition-controlled statement is:

//Declare loop control variable
While condition Statement Statement Etc. //Ask Question that changes the loop control variable
End While

The general structure of a Do While loop with a condition-controlled statement is:

//Declare loop control variable
Do
Statement Statement Etc. //Ask Question that changes the loop control variable
While Condition

Help Video: Double click the file to view video

Step 1: Examine the following pseudocode main module from Lab 4.1. Loops are commonly used to call modules multiple times. The best design is to use a loop around the module calls in Main.

Module main () //Declare local variables
Declare String clientName = “ “
Declare Real feetUTP = 0
Declare Real subTotal = 0
Declare Real taxCost = 0
Declare Real totalCost = 0

//Module calls
Call inputData(feetUTP, clientName)
Call calcCosts(feetUTP, subTotal, taxCost, totalCost)
Call displayBill(clientName, totalCost)

End Module

Step 2: In the space provided, re-write the pseudocode for the main module with a condition controlled While or a Do-While loop so that the program runs multiple times. Create a loop control variable named keepGoing of the data type string and initialize this variable to “y”. Don’t forget to indent the contents of your loop. (Reference: Modularizing the Code in the Body of a Loop, page 172).

Module main () Declare String keepGoing = y While keepGoing == y
Declare String clientName =
Declare Real feetUTP = 0
Declare Real subTotal = 0
Declare Real taxCost = 0
Declare Real totalCost = 0

Call inputData (feetUTP, clientName)
Call calcCosts (feetUTP, subTotal, taxCost, totalCost)
Call displayBill (clientName, totalCost)
End While
End Module

Lab 7.2 –Condition Controlled with While and Do-While Loops: Flowcharts

Critical Review

In a while loop, the question is asked first. After the statements process, the control goes back above the condition.

In a do-while loop, the question is asked last. The statements always process at least one time.

Help Video: Double click the file to view video

Step 1: Examine the following main module from Lab 4.2. Loops are commonly used to call modules multiple times. The best design is to use a loop around the module calls in Main (Reference: Modularizing the Code in the Body of a Loop, p. 173).

Step 2: Redesign the main module with a condition controlled While or a Do-While loop so that the program runs multiple times, based on the pseudocode from Lab 7-1.

PASTE FLOWCHART HERE

Lab 7.3 –Count Controlled with While and Do-While Loops: Pseudocode

Critical Review

A count-controlled loop repeats a specific number of times.

The loop keeps a count of the number of times that it iterates, and when the count reaches a specified amount the loop stops.

A variable, known as a counter variable, is used to store the number of iterations that it has performed.

The three actions that take place are initialization, test, and increment.
Initialization: Before the loop begins, the counter variable is initialized to a starting value.
Test: The loop tests the counter variable by comparing it to a maximum value.
Increment: To increment a variable means to increase its value. This is done by adding one to the loop control variable.

Any loop can be used with a count-controlled loop.

The general structure of a While loop (although it is the same process with a Do-While loop) with a count-controlled statement is:

//Declare loop control variable
Declare Integer counter = 1
While condition Statement Statement Etc. //increment counter Set counter = counter + 1
End While

Help Video: Double click the file to view video

Step 1: Understanding how to raise a number to the power of 2 is very important to the field of networking as it relates to binary conversion. Convert the following algorithm to pseudocode using a count controlled do while or while loop:

1. Create three integer variables called toPower, number, and counter. Set counter to 0, and toPower and number to 2.
2. Write a do while loop or while loop that will run 7 iterations.
3. Inside the loop, set toPower equal to 2 to the power of the number variable.
4. Display “2 to the power of”, number, “ is”, toPower to the screen
5. Increment counter and number by 1

Module Main()
Declare integer toPower = 2
Declare integer number = 2
Declare integer counter = 0
While Counter < <7
Set toPower = 2^Number
Display 2 to the power of, number, is,toPower, Counter += 1
Number += 1
End While
End Module

Lab 7.4 –Count Controlled with While and Do-While Loops: Flowcharts

Critical Review

In a count controlled while or do-while loop, the flowchart is essentially the same as a condition controlled, except you must create a counter and manually increment the counter variable. The general flow using a while loop looks as follows:

Help Video: Double click the file to view video

Step 1: Using your pseudocode from lab 7-3, design either a while or a do while loop of raising 2 to the power of 2-8 to the screen.

PASTE FLOWCHART HERE
Lab 7.5 –While and Do While Loops: Visual Basic Challenge I

Critical Review

Visual Basic supports both While and Do While loops. The general syntax for a condition controlled loop is as follows:

While loop:

While keepGoing = "yes"

End While

Do While loop:

Do While keepGoing = "yes" Loop

Visual Basic will automatically end your While with an End While, and end your Do While with the Loop.

Count controlled loops are structured the same way, but you must declare a counter variable as an Integer as loops only count in whole increments. Additionally, do not forget to increment or decrement your counter variable.

Help Video: Double click the file to view video

Write either the Pseudocode OR Flowchart AND the Visual Basic Code

Design a program that will use your pingMe() module from Lab 4-4. There should be a condition controlled loop in Main to run the program multiple times if the user wants. Additionally, in the pingMe() module, there should be a count controlled loop that will ping your system after a count down of 5 (Reference p. 199, Decrementing). You may use while or do while loops. Your output may look as follows:

PASTE FLOWCHART OR PSEUDOCODE HERE

PASTE VISUAL BASIC CODE HERE
Lab 7.6 –While and Do While Loops: Visual Basic Challenge II

Based on Lab 7-3 and 7-4, code the flowchart/pseudocode by converting 2 to the power of a number from 2 to 8. You should have a main module that calls a module called displayPower() as many times as the user types yes (using a condition controlled loop). The displayPower() module should use a count controlled loop that raises the toPower variable to 2 to the power of the number, such as toPower = 2 ^ number. The output should look as:

Help Video: Double click the file to view video

PASTE VISUAL BASIC CODE HERE

You May Also Find These Documents Helpful

  • Satisfactory Essays

    Unit 7 Lab And Assignment

    • 311 Words
    • 2 Pages

    Complementary Metal Oxide Semiconductor, or CMOS, is a widely used type of semiconductor. CMOS semiconductors use both NMOS(negative polarity) and PMOS(positive polarity) circuits. Since only one of the circuit types is on at any given time, CMOS chips require less power than chips using just one type of transistor. This feature makes them convenient for use in battery-powered devices such as laptops. Personal computers also contain a small amount of battery-powered CMOS memory to hold the date, time, and the system setup parameters. To access the CMOS on most computers, press the delete key as the computer is booting.…

    • 311 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    IT210 Week 3

    • 756 Words
    • 4 Pages

    Resources: Review the example in Appendix E and the additional examples on pgs 80-83 of Prelude to Programming…

    • 756 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    Unit 59 Assignment 3

    • 645 Words
    • 3 Pages

    Here below is a screenshot of a program created in flow code that uses macros (meaning that you don’t need to repeat yourself) along with a simulation/real hardware to show that this code works.…

    • 645 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    Cosc-2436-73426 Project 2

    • 306 Words
    • 2 Pages

    return -1 // if the while loop exit without retuning a value it means the value…

    • 306 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Lab 1

    • 414 Words
    • 2 Pages

    4. Why would you use a tool like DevManView while performing a computer forensic investigation?…

    • 414 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    All data-input and data-display operations (cin and cout) should be done in the function main() test program.…

    • 477 Words
    • 2 Pages
    Good Essays
  • Good Essays

    Producer vs Consumer

    • 590 Words
    • 3 Pages

    Comments are available in the code that will provide a full understanding of the program itself; however abstraction of the problem will be featured in this lab report. In the abstraction of this problem I had to reference…

    • 590 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Repetition Structure

    • 885 Words
    • 3 Pages

    In Visual Basic the loop structure will allow you to run one or more lines of code repetitively. In the loop structure you have the ability to repeat statements until the condition is true, false, a specified number of times, or once for each element in a collection. In Visual Basic several problems require repetition capability, in which the sequence or calculation of instructions is repeated over and over using different sets of data. Some examples would include continual checking of user data entries until an acceptable entry i.e a valid password is made. With computer programs the power comes from being able to repeat the same calculation or sequence of instructions several times over each time using different data. It uses different data without having to rerun the program for each new set of data values. Visual Basic has three different types of repetition structures, do while structures, for structures, and do/loop until structures. If it is true the code will be executed and if it is false the cold will not be executed. Statements that initially sets the condition is it must always be placed before the condition is first evaluated to ensure the correct loop execution the first time. Statements within the repeating section of code that allows the condition to become false.…

    • 885 Words
    • 3 Pages
    Good Essays
  • Powerful Essays

    This chapter covers fundamentals of MATLAB programming. With lots of examples, this chapter offers the essence for beginners to get a handle on programming in MALTAB. Starting from introducing various MATLAB windows and on-line help facilities, the fundamentals of MATLAB programming including data types, statements and matrix representation are explained first followed by matrix manipulations, such as algebraic computation, logical and relationship expressions and data conversion. Then, flow charts in MATLAB programming is illustrated, including loop structures, conditional structures, switches and trial structures. MATLAB function programming and pseudo code processing are covered together with two-dimensional and three-dimensional graphics and visualization techniques. MATLAB graphical user interface (GUI) techniques are explained so that the readers will gain new GUI programming skills to design user-friendly interfaces. Finally, programming skills for delivering high speed, high efficiency codes are introduced with special emphasis on commonly used tips, vectorized programming…

    • 2168 Words
    • 10 Pages
    Powerful Essays
  • Good Essays

    Robot Palletizing

    • 1495 Words
    • 6 Pages

    PROC main() MoveJ home,v1000,fine,tool0; pallet; MoveJ home,v1000,fine,tool0; ENDPROC ENDMODULE PROC pallet() FOR y FROM 0 TO 3 STEP1 DO pickup; Place y; ENDFOR ENDPROC PROC pickup() MoveJ a_pick,v1000,z10,tool0; open_gripper; MoveL pick,v1000,fine,tool0; close_gripper; MoveJ a_pick,v1000,z10,tool0; ENDPROC…

    • 1495 Words
    • 6 Pages
    Good Essays
  • Better Essays

    Arduino Handbook

    • 1883 Words
    • 8 Pages

    The loop function follows next and includes the code to be executed continuously – reading inputs, triggering outputs, etc. This function is the core of all Arduino programs and does the bulk of the work.…

    • 1883 Words
    • 8 Pages
    Better Essays
  • Better Essays

    4.1 Three Structured Constructs All programs can be constructed using only the three basic constructs. It is the concept of structured programming that a program should be developed using only these three basic constructs. It is not wrong to use the GOTO structure but it is discouraged, because programs with the GOTO structure are often classified as unstructured. The three basic constructs are Sequence Selection Iteration/Repetition 4.1.1 Sequence Construct In a sequence construct, the way the statements are placed implies the order in which the computer will execute the statements. The computer will interpret the statements in a left to right, top to bottom fashion. Consider the following Calculate-Average Module DO Get Two Numbers Calculate Sum Determine Average Print Average ENDDO The sequence for the above module is straightforward. You get the two numbers, calculate the sum, determine the average and print out the result. If the statements are not in that sequence, the result obtained will not be the answer intended Figure 4-1 Simple sequence The Calculate Average module is not a separate module away from the rest of the four modules below it. But it actually contains all the four modules. Each process (a rectangle) represents a component with their function/action stated in the function list. If the component does not contain or form other constructs (i.e. other sequences, selections or iterations), it is regarded as an elementary component. The diagram should be interpreted in a top-down, left to right manner. The Calculate-Average Module has four elementary components. Each time it is activated, it will activate the Input Number module, Calculate Sum module, Determine Average module and the Print Result module in that order. Consider another example Figure 4-2 Complex sequence The sequence of activation for the above will be A, B, E, F, C, D, G, I, J, H 4.1.2 Selection Construct The selection construct consists of condition(s) and one or…

    • 2133 Words
    • 5 Pages
    Better Essays
  • Good Essays

    Main program is given below whereas subroutines aregiven after this program ends. inp = randn(1,502); p = [0.26 0.93 0.26]; snr = input('enter snr \n'); x=rand(10,30); for i=1:10 for j=1:30 if x(i,j)>0.5 x(i,j)=1; else x(i,j)=0; end end…

    • 819 Words
    • 4 Pages
    Good Essays
  • Satisfactory Essays

    Programming in C#

    • 606 Words
    • 3 Pages

    Limitation comes in the form of whether you and others can read and follow your code…

    • 606 Words
    • 3 Pages
    Satisfactory Essays
  • Powerful Essays

    C++ Notes

    • 2751 Words
    • 12 Pages

    Computer Programming – 1 Details of Syllabus: Sr. No 1 1.1 1.2 Structure Programming using C++ C++ as a superset of C programming language C++ fundamental : Character set, Identifiers & Keywords, data types, Constants & Variables 1.3 Declaration : Operators & Expressions, Library function statements, Symbolic constants, Preprocessor directives 2 2.1 Data Input and Output & Control Statements getchar( ), putchar( ), scanf( ), printf( ), gets( ), puts( ),cin, cout, setw( ), endl, 2.2 If-else, while, do-while, goto, for, nested control structures, switch, break, continue statements, comma operator 3 3.1 Function and Arrays Function prototypes, passing arguments to a function by value and by reference, recursion overloading functions, storage classes 3.2 Defining processing array, passing array to functions, introduction to multidimensional array and strings 4 4.1 Pointers, Structure and Unions Declarations. Referencing & dereferencing passing pointer functions , pointer to functions , pointer to arrays 4.2 4.3 5 5.1 Structure & Unions. Defining and processing a structure Creation and manipulation of linked list Object Oriented Programming using C++ Classes, Objects, Data encapsulation, access specifiers, private, public and protected inheritance in details, operator overloading of unary and binary arithmetic operators, virtual functions, pure virtual functions 6 6.1 Late binding, friend function, object as function parameter, overriding functions, overload constructors, copy constructors, static class member 6 8 5 7 6 5 Details Hrs…

    • 2751 Words
    • 12 Pages
    Powerful Essays

Related Topics