< Back

Lecture 1 - C++ Introduction


As usual, create a directory for today's lab:

$ cd ~/cs170/labs
$ mkdir lab1 
$ cd lab1 

C++ and Linux Command Line

It's time to learn a new language! Python is a great language, especially for learning programming concepts and for efficiency of creating programs. C++ might seem a little bulky in comparison. But that bulk brings some benefits with it. Some errors that Python would not find until executing the program can be found earlier, etc. Today we will begin with some simple C++ programs.


Lab Assignment 1
Grade Average

A lot of the general concepts we covered last semester still apply here. When you see something that seems like it should use a loop, we're going to use a loop. If you see something that seems like it should use a conditional, you will use a conditional. This is an activity that we very easily could have given you last semester. Let's practice our new C++ skills by writing a program to allow your professor to be very lazy!

Details

In a file called GradeAverage.cc, create a program that will prompt the user to enter a number n, followed by n numbers representing their grades for a course. Your program should then print the users grade average.

Example

  $ g++ -c GradeAverage.cc
  $ g++ -o grade GradeAverage.o
  $ ./grade
  How many grades? 3
  100
  90
  80
  Your grade average is: 90
  $ ./grade
  How many grades? 2
  100
  99
  Your grade average is: 99.5

Hint

  • You can use cin to read an integer from the terminal. It may seem obvious, but you need to declare your variable before you read into it.

    	    int someVariable;
    	    cin >> someVariable;
    	  

  • You can use a while loop to accomplish the repetition necessary in this program. The syntax for a while loop is very similar to that in Python. However, the parenthesis around the condition are not optional! In addition, the curly braces dictate what the body of the loop is. These are not optional either.

    	    while (some condition) {
    	    body of the loop
    	    }
    	  

  • cout is the mechanism used to print things to the terminal. It can print basically any datatype, but different datatypes should be separated by a <<.

    	    cout << "The answer to life, the universe, and everything? " << 42 << endl;
    	  

 

Challenge

Modify the above program so that it can process multiple students at once. The format of the input will need to change to accomplish this.

      $ ./grades
      Number of Students? 2
      Number of Grades? 2
      Scotty
      60
      50
      Scotty's average: 55
      Bouchard
      100
      99
      Bouchard's average: 99.5
    


Lab Assignment 2

Even datastructures we are aware of from Python at least have analogues in C++. For example, Strings. To use a String, we must include a separate library. However, this extra library gives a lot of extra power to the language.

Details

In a file called Palindromes.cc, create a program that will prompt the user to enter a string. Your program should output whether the input string is a palindrome or not. Recall that a palindrome is a string that is read the same forwards and backwards.

Example

  $ ./palindrome
  hello
  hello is not a palindrome
  $ ./palindrome
  bob
  bob is a palindrome

Hint

  • cin can be used to read in any datatype. it decides what it is reading based on the type of the variable it is being read into! It's really useful that way!

    	    string someVariable;
    	    cin >> someVariable;
    	  

  • A for loop is longer than the for loop in Python. It is essentially encoding a special form of a while loop.

    	    for(loop variable declaration; condition; increment) {
    	        body of the loop
    	    }
    	  
    So, a for loop that executes 10 times would be:
    	    for(int i; i < 10; i++) {
    	        body of the loop
    	    }
    	  

 

Challenge

A pangram is a sentence which contains all of the letters of the English alphabet. Create an additional program which will read in an entire sentence, and will check to see if it is a pangram.

You will need to use the getline function to read an entire line from the terminal.