Schedule‎ > ‎

Assignment 5 Loopy Programs

Objectives

  • Apply counting loops to display data
  • Use indefinite loops to gather data
  • Develop the ability to find minimum and maximum values
  • Code nested loops


Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • Most importantly,  you undermine your own self confidence when you cheat,  because you know you didn't accomplish the assignment without cheating.
  • You may not give a copy of your code to anyone else.
  • You may not show your completed code to another person or look at another person's code until you complete and submit this assignment.
  • You may get help from people but only if they do not show or tell you the code to type.
  • Remember that the instructor performs similarity tests on programming project submissions, and plagiarized code is usually very easy to detect.


Grading Criteria 

For each part: 10 pts: Header Comment 1pt, Compiles 2 pts, Proper Formatting 1 pt, Works as Specified 6 pts
  1. loopychars.cpp (10pts)
  2. humpty.cpp (10pts)
  3. minimum.cpp (10pts)
  4. Extra Credit:  xcminmax.cpp (3pts)
Total Possible: 30 pts + Extra Credit 3pts


Project Specifications

Your solutions to these projects must only use techniques we have covered so far.

Programming Style

For all programs, remember to follow all the style rules we covered including the recent items:

  1. No magic numbers
  2. Indentation in while statements and placement of curly braces
  3. No tab characters in your code.

    You can remove tab characters by either setting up TextPad correctly (see here) or by running a program named astyle (see here).

  4. Meaningful variable names and consistent naming style (caps vs. underbars).



(Public Domain)

Project 1: Loopy Characters

Counting loops are very commonly used in programming to ... count things. Character data types are used to store and display individual letters and other symbols. In this project we explore both counting loops and characters in C++.

Project Specifications
  1. Start by downloading the worksheet: loopychars.cpp.

    Keep the same filename and add to the existing code to complete the project. Leave the existing code unchanged, except for comments as instructed.

  2. Add your name and the date to the file comment block at the top of the file where shown in the comments.
  3. User input is already coded into the worksheet, consisting of an integer number, myNum, and a single character, myChar.

    Do not add any other input commands or change the input order. Assume the user enters only valid data.

  4. Use a for-loop to print the character entered by the user the number of times specified by the integer number typed in. See the Example Run below for an example.

    Hint: see Exercise 5.1a step 3 for an example for-loop. 

  5. Use a second for-loop to print the character entered by the user followed by the subsequent myNum - 1 characters in the ASCII table. See the Example Run for an example.

    Hint: In this loop, we are adding the index value to the character typed in by the user. This is like when we added i to sum in lesson 5.1.3. We can add a number to myChar because of the ASCII character codes. To save the original value of myChar, declare another char variable or use casting (see lesson 4.1.6).

  6. Use a third for-loop to print the a character myNum  times.   On even counts of the loop print a '*'.  On odd counts of the loop print myChar  as shown in the Example Run.

    Hint: Use an if-statement to test for even or odd counts of the loop (5.2.1 Bar Charts)

  7. Use a fourth for-loop to print the character n times with a tick mark '+' substituted every fifth character as shown in the Example Run.

    Hint: Use an if-statement to test for the every fifth count of the loop. Start the for-loop count from 1, but check the end condition! Only print the number if nothing else is printed.

  8. Use yet another loop to print the previous loop myNum times. Thus, this problem should use two loops.

    Hint: put your for-loop from the previous challenge inside another for-loop that has a different counting variable. Print a newline character inside the outer loop after the inner loop completes.

  9. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    ** Loopy Characters! **
    
    Enter an integer between 1 and 20: 10
    Enter a single character: A
    
    #1. Printing A 10 times:
    AAAAAAAAAA
    
    #2. Printing starting with A and the following 9 ASCII characters:
    ABCDEFGHIJ
    
    #3. Printing A character 10 times substituting '*' on odd indexes:
    A*A*A*A*A*
    
    #4. Printing A character 10 times substituting (+) every fifth character:
    AAAA+AAAA+
    
    #5. Printing 10 lines of the previous loop:
    AAAA+AAAA+
    AAAA+AAAA+
    AAAA+AAAA+
    AAAA+AAAA+
    AAAA+AAAA+
    AAAA+AAAA+
    AAAA+AAAA+
    AAAA+AAAA+
    AAAA+AAAA+
    AAAA+AAAA+
    

    In the above example run, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

  10. After displaying the output, exit the program.
  11. Submit the source code file loopychars.cpp with the rest of the assignment.  Make sure you know which file is the correct working file.


Humpty Dumpty

Image source: Public Domain

Project 2: Humpty Dumpty

In this project we use a for loop to print parts of a well-known nursery rhyme.

Humpty Dumpty sat on a wall,
Humpty Dumpty had a great fall.
All the king's horses and all the king's men
Couldn't put Humpty together again.

Project Specifications
  1. Create a source code file named humpty.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  2. Asks the user for a single maximum number, and no other input, as shown in the Example Run.
  3. Write a program single for-loop that prints numbers from 1 to an upper limit, each on their own line, but substituting parts of the rhyme for certain numbers as specified below.
  4. When the loop count reaches a multiple of five print "Humpty" instead of the number.

    Hint: put an if-statements inside the braces of the for-loop.

  5. When the loop count reaches a multiple of seven print "Dumpty" instead of the number.
  6. For numbers which are multiples of both five and seven print "Had a Great Fall" instead of the number.
  7. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the input is different.
    Enter a positive number for the ending loop count: 35
    1
    2
    3
    4
    Humpty
    6
    Dumpty
    8
    9
    Humpty
    11
    12
    13
    Dumpty
    Humpty
    16
    17
    18
    19
    Humpty
    Dumpty
    22
    23
    24
    Humpty
    26
    27
    Dumpty
    29
    Humpty
    31
    32
    33
    34
    Had a great fall
    

    In the above example run, the user entered the value shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

  8. Submit your humpty.cpp with the rest of the assignment.  Make sure you know which file is the correct working file.
Hints:
  • Put several if-else statements inside the braces of the single for-loop.
  • Test for numbers that are multiples of both 7 and 5 first.




Project 3: Minimum Value

In this program you will find the minimum value of a list of values that the user enters.

  1. Look at Section 5.3.4 in Lecture Notes 5 to refresh yourself on finding Max and Min values.
  2. Save your file as minimum.cpp
  3. Ask the user to enter a positive number  (0 to stop)
  4. Using a while loop,  check to see if the latest value is the minimum.  If it is,  save it in a separate variable you have declared.
  5. Print out "Lowest value so far is: " with the current minimum value
  6. When the user enters 0 to exit,  print out:  "Minimum value is: " with the minimum value
  7. See the Example Run below:

  8. Submit your minimum.cpp with the rest of the assignment.  Make sure you know which file is the correct working file.

Extra Credit: Minimum and Maximum Value (3 pts)

In this program you will find both the minimum & maximum value of a list of values that the user enters
  1. Save your file as xcminmax.cpp
  2. Ask the user to enter a positive number  (0 to stop)
  3. Using a while loop,  check to see if the latest value is the minimum or maximum.  If it is,  save it in an appropriate variable.
  4. Print out "So far, the lowest value is __ and the highest value is __" 
  5. When the user enters 0 to exit,  print out:  "Minimum value is ____ and maximum value is ___"

SOLUTION CODE:
Project 1:  loopychars

/**
    CS-11 Assn 5, loopychars.cpp
    Purpose: Loops and characters.

    @author Sharon Strelitz
    @version 1.0 01/21/2018
*/
#include <iostream>
using namespace std;

int main() {
    int myNum; // the integer number
    char myChar; // the single character
    cout << "** Loopy Characters!**\n\n";
    cout << "Enter an integer between 1 and 20: ";
    cin >> myNum;
    cout << "Enter a single character: ";
    cin >> myChar;
    cout << endl;

    // Repeating myChar myNum times with a for-loop.
    cout << "#1. Printing " << myChar << " " << myNum << " times:\n";
    for (int i = 0; i < myNum; i++)  {
      cout << myChar;    
    }
    cout << endl << endl;

    // Printing starting with myChar and the following myNum ASCII chars.
    cout << "#2. Printing starting with " << myChar << " and the following " << myNum - 1
         << " ASCII characters:\n";
    for (int i = 0; i < myNum; i++)  {
        cout << (char) (myChar + i);
    }
        
    cout << endl << endl;

    // Repeating the myChar myNum times with stars on odd indexes.
    cout << "#3. Printing " << myChar << " character " << myNum
         << " times substituting '*' on odd indexes:\n";
    for (int i = 1; i <= myNum; i++)  {
        if (i % 2 == 0)  {//even
            cout << '*';
        }  else {
            cout << myChar;
        }
    }
    cout << endl << endl;

    // Repeating myChar myNum times with tick marks (+) every 5 chars
    cout << "#4. Printing " << myChar << " character " << myNum
         << " times substituting (+) every fifth character:\n";
    for (int i = 1; i <= myNum; i++)  {
        if (i % 5 == 0)  {//even
            cout << '+';
        }  else {
            cout << myChar;
        }
    }
    cout << endl << endl;

    cout << "#5. Printing " << myNum << " lines of the previous loop:\n";
    // Hint: put your for-loop from the previous challenge inside another
    // for-loop that has a different counting variable.
    // Put your code here
    for (int j = 0; j < myNum; j++) {
        for (int i = 1; i <= myNum; i++)  {
            if (i % 5 == 0)  {//even
                cout << '+';
            }  else {
                cout << myChar;
            }
        }
        cout << endl;
    }
    cout << endl << endl;

    return 0;
}

Project 2: humpty
 
/**
    CS-11 Assn 5, humpty.cpp
    Purpose: Practice loops with Humpty Dumpty rhyme.

    @author Sharon Strelitz
    @version 1.0 01/21/2018
*/
#include <iostream>
using namespace std;

int main() {
    int loopCount = 0;
    cout << "Enter a positive number for the ending loop count: ";
    cin >> loopCount;
    for (int i = 1;  i <= loopCount; i++)  {
        if (i % 5 == 0 && i % 7 == 0)  {
            cout <<  "Had a great fall" << endl;
        } else if (i % 5 == 0) {
            cout << "Humpty" << endl;
        }  else if (i % 7 == 0) {
            cout << "Dumpty" << endl;
        }  else {
            cout << i << endl;
        }
    }
    return 0;
}

Project 3: minimum

/**
    CS-11 Assn 5, minimum.cpp
    Purpose: Find the minimum value from a list the user enters.

    @author Sharon Strelitz
    @version 1.0 01/21/2018
*/
#include <iostream>
using namespace std;

int main() {
    double newNum = 0.0, minimum = 0.0;
    cout << "Enter a positive number (0 to stop): ";
    cin >> newNum;
    minimum = newNum;
    while (newNum > 0) {
        if (newNum < minimum) {
           minimum = newNum;
        }
        cout << "Lowest value so far: " << minimum << endl;
        cout << "Enter the next positive value (0 to exit): ";
        cin >> newNum;
    }
    if (minimum > 0) {
        cout << "Minimum value is: " << minimum << endl;
    } else {
        cout << "No data!\n";
    }
    return 0;
}

Extra Credit:  xcminmax

/**
    CS-11 Assn 5 Extra Credit minmax.cpp
    Purpose: Find the minimum and maximum values from a list the user enters.

    @author Sharon Strelitz
    @version 1.0 01/21/2018
*/
#include <iostream>
using namespace std;

int main() {
    double newNum = 0.0, minimum = 0.0, maximum = 0.0;
    cout << "Enter a positive number (0 to stop): ";
    cin >> newNum;
    minimum = newNum;
    maximum = newNum;
    while (newNum > 0) {
        if (newNum < minimum) {
           minimum = newNum;
        } else if (newNum > maximum) {
           maximum = newNum;
        }
        cout << "So far, the lowest value is " << minimum 
             << " and the highest value is " << maximum << endl;
        cout << "Enter the next positive value (0 to exit): ";
        cin >> newNum;
    }
    if (minimum > 0) {
        cout << "Minimum value is: " << minimum 
             << " and maximum value is " << maximum << endl;
    } else {
        cout << "No data!\n";
    }
    return 0;
}



Subpages (1): loopychars
Comments