Schedule‎ > ‎

Lab 02: for loops and if statements


Objectives

  • Use the CS-11M Canvas Discussion
  • Start practicing C++ commands
  • Start declaring and using numerical variables
  • Display output to the Serial Monitor
  • Perform arithmetic operations to solve problems using C++
  • Debug errors in your code
  • Write your first complete C++ programs!

^ top

Academic Honesty

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

  • 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 others if you get stuck, but only if they do not show or tell you what to type.

^ top

Project Specifications

Note that this is a solo programming project and not a pair-programming project. Your solutions to these project must only use techniques we have covered so far.

Programming Style

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

  1. File block comments.
  2. Placement of curly braces and indentation within curly braces.
  3. Limiting line length to about 80 characters.

    TextPad, and most other text editors, tell you both the line and column location, which lets you check the line length.

  4. Spaces before and after operators.

Project 1:  CS-11M Discussion Group

Post a question or answer or comment about this class/exercises/homework to the CS-11M Discussion Group. 

Grade:  2 points

Project 2:   Multiply   (multiply.ino)

  1. In the Arduino IDE,  start a new file called multiply.ino
  2. You may want to setup a sample file which has your header comment, #include statements and a sample setup() and loop() that you can use as starter code for every assignment.
  3. In setup(), after Serial.begin(9600),   declare 1 integer variable named total
  4. Initialize total to 1.
  5. In the setup() function do the following:
  6.     Write some code that will loop 6 times using a for loop. 
  7.      Each time it loops,  multiply total by 2 and assign the answer to total.  Put this code in the setup() function.
  8.      Right after you assign the answer to total,  print the new total like this:    cout << "total is now:  "  <<   total  << endl;

Your output should look like this:

total is now: 2
total is now: 4
total is now: 8
total is now: 16
total is now: 32
total is now: 64


      8.    Submit multiply.ino with your other file from Project 3 to Canvas.  You need to submit all files for one canvas assignment at the same time.

Grade:  5 points

Project 3:   Add an IF statement  (multiply_and_if.ino)

  1. Start with your multiply.ino from Project 1.  Save it as  multiply_and_if.ino.
  2. After your cout print statement which prints the current total,   add an if statement inside your loop which checks to see if total is greater than 32.
  3. If total is greater than 32,  print "total is greater than 32"

Your output should now look like this:

total is now: 2
total is now: 4
total is now: 8
total is now: 16
total is now: 32
total is now: 64
total is greater than 32



    3.  Submit multiply_and_if.ino with your other files for this Lab to Canvas.  You need to submit all files for one canvas assignment at the same time.

Grade for this Project:  5 points

Project 4:  EXTRA CREDIT Factorial Fun!  (factorial.ino)

In math,  the factorial of a non-negative integer n,  denoted by n! is the product of all positive integers less than or equal to n. 

For example:

6! = 6 * 5 * 4 * 3 * 2 * 1 = 720

1.  Write a program which uses a FOR LOOP to compute 7!    

     There is no need to get input from the user.  Declare a variable:   int number = 7;

      Also,  you can use as many other variables as you want.

2.  Name your file factorial.ino

3.

Your output should look like this:

7 ! = 5040


4.  Note that you can not calculate 8! using an integer on the Arduino.  This is because the largest value that an Arduino integer (2 bytes=16bits) can hold is  32,767 or -32,767.  This is because an int is signed (can be negative or positive) and 1 bit is taken to indicate if the number is positive or negative.  so the normal formula 2^15 - 1 = 32767 applies. (not 2^16 - 1).

Since 8! is 40320,  it is too big to fit into an Arduino integer.


5.   Submit factorial.ino with your other files from this Lab to Canvas.  You need to submit all files for one canvas assignment at the same time.

Grade for this EXTRA CREDIT project:  3 points

Total grade possible for this Lab 02:  12 points + 3 points extra credit



^ top


COMPLETED CODE for multiply.ino
#include <ArduinoSTL.h>
using namespace std;
void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  int total = 1;
  for (int i = 0; i < 6; i++)  {
    total = total * 2;
    cout << "total is now: " << total << endl;
  }
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

COMPLETED CODE FOR multiply_and_if.ino

#include <ArduinoSTL.h>
using namespace std;
void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  int total = 1;
  for (int i = 0; i < 6; i++)  {
    total = total * 2;
    cout << "total is now: " << total << endl;
    if (total > 32) {
      cout << "total is greater than 32" << endl;
    }
  }
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

COMPLETED CODE FOR factorial.ino
#include <ArduinoSTL.h>
using namespace std;

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  int number = 7;
  int fact = 1;

  //for Loop Block
  for (int count = 1; count <= number; count++)  {
    fact = fact * count;
  }
  cout << number << " ! = " << fact;
}

void loop() {
  //does nothing

}
Comments