Week 1 (C)

Topics

  • C
  • Variables
  • Types
  • Loops
  • Conditionals

This week can also be an opportune time to introduce a discussion of design and style. We’ve outlined a few sample questions about design and style to help students think about these topics below.

Programming Exercises

Below are a few example programming exercises that engage students in hands-on practice on the week’s topics. Keep in mind that class might not be long enough to include all exercises, so be sure to pick just a few!

  • Sum
    • Write a program sum.c that asks the user to provide ten integers as input and computes the sum.
    • Topics
      • Getting user input
      • Loops
      • Variables
    • Sample Usage
      $ ./average
      Number: 1
      Number: 2
      Number: 3
      Number: 4
      Number: 5
      Number: 6
      Number: 7
      Number: 8
      Number: 9
      Number: 10
      Sum: 55
      
    • Sample Solution
      #include <cs50.h>
      #include <stdio.h>
      
      int main(void)
      {
          int sum = 0;
      
          for (int i = 0; i < 10; i++)
          {
              sum += get_int("Number: ");
          }
      
          printf("Sum: %i\n", sum);
      }
      
  • Contacts
    • Write a program, contacts.c, that asks the user for a name, an age, and a phone number (as well as one other value of the student’s choice). contacts.c should print the values back to the user as confirmation.
    • Topics
      • Getting user input
      • Format Codes
      • Printing
      • Variables
    • Sample Usage
      $ ./contacts
      Name: Harry
      Age: 11
      Phone Number: 1-800-MAGIC
      Location: Number 4, Privet Drive
          
      New contact: Harry, 11, lives at Number 4, Privet Drive and can be reached at 1-800-MAGIC.
      
    • Sample Solution
      #include <cs50.h>
      #include <stdio.h>
      
      int main(void)
      {
          string name = get_string("Name: ");
          int age = get_int("Age: ");
          string phone = get_string("Phone number: ");
          string address = get_string("Address: ");
      
          printf("\nNew Contact: %s, %i, lives at %s and can be reached at %s.\n", name, age, address, phone);
      }
      

Discussion Questions

One technique to promote participation in class is a quick, 2–3 minute discussion question. Letting students posit their own reasoning, even if they’re not entirely sure of an answer, can help reinforce material from lecture. One model for introducing these questions is the “Think, Pair, Share” approach, in which students take 30 seconds to think of their own answer and 60 seconds to share their answer with a partner. Afterwards, you can call on random pairs to share their thinking with the larger group. It’s also best to follow up with your own answer, too!

  • Why does C require us to specify a data type when we declare a variable?
  • What makes a good variable name?
  • When should you leave comments on your code? When might you leave too many comments?

Annotated Sample Slides

Here you’ll find sample slides to adopt or adapt when teaching Week 1.

Some slides contain speaker notes to illustrate why the sample slides take a certain approach to illustrating a concept or leading an exercise. You’re welcome to modify these slides as you see fit, though do try to keep some of the same elements of active learning that have been included in these samples.

Past versions of sample slides are also available under the “Additional Slide Resources” dropdown.