Main

Week 1 Notes and Exercises

Logistics

Fill out this short form, including your Bitbucket username: https://goo.gl/forms/Nd6pMXw6H5aJHfnf2

0. Getting started with git

Revision control Intro (PDF), example transcript of exercises below.

  1. Connect to a Unix shell, e.g., ssh to ix-dev, create a top-level working directory, e.g., name it topdir
  2. In the topdir directory, create a Git repository called myrepo (use git init --bare myrepo)
  3. In the topdir directory, create a new clone of myrepo, called myrepo1
  4. In the myrepo1 local repository directory
    • Add a file test.txt
    • Commit and push your changes
  5. In the topdir directory or on a different machine, create a new clone of myrepo, called myrepo2
  6. In the myrepo2 local repository directory
    • Change the file you created in step 4, commit and push the changes.
  7. Go back to myrepo1 and pull all changes

For the next set of exercises, you need to be able to login to ix-dev.cs.uoregon.edu. If you have not yet established access, pair up with someone who has, then use pairs programming style to complete the work.

1. Connecting to ix-dev

 ssh cisusername@ix-dev.cs.uoregon.edu

To see instructions on using any Unix command, use the man command, for example try

man man
man intro
man ssh

To check whether a program is available and where it's installed, use which, e.g., try

which ssh

2. Cloning a remote Git repository

If you are working on your own machine, make sure it's connected to the network, then do:

 git clone https://bitbucket.org/brnorris03/cis330w19 \
      cis330-exercises

If you have set up your ssh keys on Bitbucket, then you can clone with this command:

 git clone git@bitbucket.org:brnorris03/cis330w19.git \
      cis330-exercises

3. Working with local repository, committing, and pushing to remote

After cloning, go to your local repository copy:

cd cis330-exercises

View the contents of the directory

ls

Go to the week1 subdirectory and create a new subdirectory -- make sure it's a unique name that nobody else in class may be using, i.e., you can use your DuckID, CIS username, or bitbucket username if you have one already (any other unique string will do).

 
cd week1
mkdir brnorris03

4. Simple C program

First, we will create, compile, and run the smallest legal C program, containing just a main subroutine (why do we need main?).

vi tiny.c
gcc tiny.c
./a.out

Now let's add the code to the repository, and commit and push the changes to the master.

git add tiny.c
git commit -m "the smallest C program"
git push

5. I/O, C Strings, pointers

Let us add some useful code to the minimal program we wrote last time. We will prompt the user for some input, then based on their input, we will select a different string to output.

Start with tiny.c which you created in the previous lecture exercises. Or, if you wish, create a new file, tiny2.c for this code. First, let's accept a single character as input and if it is 'y', then print a positive response, otherwise, commiserate.

  1. #include <stdio.h>   /* Needed for printf and getchar */
  2.  
  3. int main()
  4. {
  5.     char userInput;
  6.  
  7.     printf("Are you having a nice day? (y/n)? ");
  8.  
  9.     userInput = getchar();
  10.  
  11.     if (userInput == 'y') {
  12.         printf("That's wonderful, so am I!\n");
  13.     } else {
  14.         printf("That sucks, just try again tomorrow.\n");
  15.     }
  16.  
  17.     return 0;
  18. }

In the main subroutine, declare a string variable, which in C is either an array of char elements or a pointer, char *.

What are C pointers?

Pointers are special variables that can be used to store the address of another variable.

Example:


In the first version, we will declare the userInput variable as a static array of characters.

  1. #include <stdio.h>   /* Needed for printf and fgets */
  2.  
  3. int main()
  4. {
  5.     /* Maximum size of string for storing user input */
  6.     const int maxBufSize = 5;
  7.  
  8.     /* String variable to store user input */
  9.     char userInput[maxBufSize];                              
  10.  
  11.     printf("Are you having a nice day? (yes/no)? ");
  12.     fgets ( userInput, maxBufSize, stdin );          
  13.  
  14.     if (strcmp(userInput,"yes\n") == 0) {
  15.         printf("That's wonderful, so am I!\n");
  16.     } else {
  17.         printf("Oh well, just try again tomorrow.\n");
  18.     }
  19.  
  20.     return 0;
  21. }

Finally, instead of having a static array of chars, let's declare our string variable as a char pointer, or char * and manage its memory explicitly.

First, we will do an illustrative exercise in class (I will need some volunteers!).

  1. #include <stdio.h>   /* Needed for printf and fgets */
  2. #include <stdlib.h>   /* Needed for malloc */
  3.  
  4. int main()
  5. {
  6.     /* Maximum size of string for storing user input */
  7.     const int maxBufSize = 5;
  8.  
  9.     /* String variable to store user input */
  10.     char *userInput = (char *) malloc (maxBufSize * sizeof(char));
  11.  
  12.     printf("Are you having a nice day? (yes/no)? ");
  13.     fgets ( userInput, maxBufSize, stdin );    
  14.  
  15.     if ( strcmp(userInput,"yes\n" ) == 0) {
  16.         printf("That's wonderful, so am I!\n");
  17.     } else {
  18.         printf("Oh well, just try again tomorrow.\n");
  19.     }
  20.  
  21.     /* Remember to free memory that has been allocated dynamically! */
  22.     free(userInput);
  23.     return 0;
  24. }

Green Marinee theme adapted by David Gilbert, powered by PmWiki