Work

Work 8: The Prime Directive

posted Oct 19, 2018, 7:17 AM by JonAlf Dyrland-Weaver   [ updated ]

No, not that prime directive.

Use the Sieve of Eratosthenes to write a function that will find the nth prime number.

You can use the following driver file, especially if you plan on entering the contest of champions later next week: https://github.com/mks65/sieve/blob/master/main.c

Regardless, you must provide your sieve and associated code (I see you makefile!) on GitHub:

https://github.com/mks65/sieve.git

Kontest Requirements:
  • Submit a link to your repo here: (ONLY if you want to enter the competition)
  •  You must make a repo and place sieve.h and sieve.c inside that directory, not a subdirectory:
  • Your sieve.h should have the function:
    • int sieve(int targetPrime);
  • There should be no printed output when sieve() is called!
  • I will use a driver of mine.
  • I will compile with the -lm flag so you can use math.h.
  • Optionally:
    • In your makefile, you can pass args to the program:
    • run:
      • ./primetest $(args)
  • Then you can use the shell as follows: 
  • make run args="100000 10"

Work 7: Enlist your C skillz.

posted Oct 8, 2018, 9:52 PM by JonAlf Dyrland-Weaver

Write a simple linked list program. You will need a define a node struct with the following data members:
  • A piece of data (your choice)
  • A pointer to the next node
  • Here's an example of what the struct could look like:
    • struct node { int i; struct node *next };
Create the following functions:
  • void print_list(struct node *);
    • Should take a pointer to a node struct and print out all of the data in the list
  • struct node * insert_front(Struct node *, int);
    • Should take a pointer to the existing list and the data to be added, create a new node and put it at the beginning of the list.
    • The second argument should match whatever data you contain in your nodes.
    • Returns a pointer to the beginning of the list.
  • struct node * free_list(struct node *);
    • Should take a pointer to a list as a parameter and then go through the entire list freeing each node and return a pointer to the beginning of the list (which should be NULL by then).  
  • Your list functions should be in a .c/.h library, with a separate .c file used for testing.              
Don't forget to test out these various functions to ensure they work correctly. (Just to be clear, when you run the program and get "Segmentation fault" that means it's not working.)

Github link:
https://github.com/mks65/list.git

Work 06: Con-struct-ion

posted Oct 2, 2018, 7:28 AM by JonAlf Dyrland-Weaver   [ updated Oct 2, 2018, 1:20 PM ]

This is due 8am Thursday (10/4) morning.

With your table buddy:
  • Create a struct that has at least 2 data members, one must be a string.
    • You can provide struct prototypes outside of functions.
    • Don't just combine random things, make it meaningful (like food and price, athlete and associated statistic, Pokemon and something about Pokemon ..)
  • Write a function that returns an example of your struct when run.
    • This should not return the same struct every time.
  • Write a function that prints out variables of your structs type in a reasonable way.
  • Write a function that modifies values of your struct's type.
  • Write a main function that tests all these things.
    • Have meaningful output.
  • DONT FORGET A MAKEFILE
GitHub clone link: https://github.com/mks65/construction.git

Work 05: If you like it then you should've put a string on it.

posted Sep 27, 2018, 5:50 AM by JonAlf Dyrland-Weaver   [ updated Sep 27, 2018, 7:53 AM ]

This will be due Monday, 10/1

You will be working with a partner on this assignment, please name your submodules as follows:

lastFf-lastFf e.g. brooksPe-holmesDa

First, acquaint yourself with the basic string functions: strlen, strcpy, strcat, strncat and strncpy. You can look at the man pages for each if you are unclear as to their uses. I encourage you to play around with each so you are comfortable with how to use them. Then write your own versions of the following:
  • Required (use the function headers I provide, they may differ from the standard headers).
    • strlen [ int strlen( char * ) ]
    • strcpy/strncpy (choose one)  [ char * strcpy( char *dest, char *source )  char * strncpy( char *dest, char *source, int n)  ]
    • strcat/strncat (choose one)  [ char * strcat( char *dest, char *source )  char * strncat( char *dest, char *source, int n)  ]
    • strcmp  [ int strcmp( char *s1, char *s2 ) ]
      • Your version does not need to return the same exact value as the system version, as long as it returns -, + or 0 when it should.
    • strchr  [ char * strchr( char *s, char c ) ]
    • You must test every function you write, you will not get full credit for an untested functions
    • You do not need to write 7 functions, only 5, but you need to write 1 of the "n" versions. 
      • (So if you do strcpy then you have to do strncat)
    • A makefile that includes a run target. Do not automatically call run in the first target.
  • Extra
    • strstr [ char * strstr( char *s1, char * s2 ) ]
    • The other strn/str version of the functions you wrote above
  • When you test your functions,  run them alongside the regular ones so you can see if they behave the same way or not.
  • Remember you should call them something other than the regular names. 
Github link
https://github.com/mks65/string.git

Here's an example of what your code might look like when run:
s1: [?
?W?]
s2: [hello]
s3: [goodbye]

Testing strlen(s2):
[standard]: 5
[mine]: 5

Testing strcpy(s1, s2):
[standard]: [hello]
[mine]: [hello]

Testing strncpy(s1, s3, 3):
[standard]: [goolo]
[mine]: [goolo]

Testing strcat(s1, s3):
[standard]: [goologoodbye]
[mine]: [goologoodbye]

Testing strncat(s1, s2, 3):
[standard]: [goologoodbyehel]
[mine]: [goologoodbyehel]

Testing strchr(s1, 'l'):
[standard]: [0x7fff57d70a13]
[mine]: [0x7fff57d70a13]

Testing strchr(s1, 0):
[standard]: [0x7fff57d70a1f]
[mine]: [0x7fff57d70a1f]

Testing strchr(s1, 'z'):
[standard]: [0x0]
[mine]: [0x0]

Testing strcmp
Comparting ab to abc:
[standard]: [-99]
[mine]: [-99]
Comparting abc to ab:
[standard]: [99]
[mine]: [99]
Comparting abc to abc:
[standard]: [0]
[mine]: [0]

Work 04: Demon-string-tion

posted Sep 26, 2018, 8:04 AM by JonAlf Dyrland-Weaver

In class, you were given a number, that number corresponds to a pair of c functions:
0 : strcpy/strncpy
1 : strcat/strncat
2 : strcmp/strncmp
3 : strchr/strstr
You will look up what your functions do, and then write a small c program that demonstrates the functions (do not write your own version of the function). Be prepared to describe your functions and explain your demo in class tomorrow.

Work 03: The old switcheroo

posted Sep 18, 2018, 5:31 AM by JonAlf Dyrland-Weaver   [ updated Sep 18, 2018, 8:05 AM ]

In order do this assignment, you need to be able to generate random numbers in c. 
  • Note: On some systems, you may be able to use other functions than the ones described below, but they are not standard for linux, so you should stay away from them for now.
  • Generating a random number in C requires 2 steps
    1. Seeding the random number generator
      1. srand( time(NULL) );
        1. srand(<SEED>) seeds the random number generator with the provided argument.
        2. If you use the same argument to srand() multiple times, you will get the exact same sequence of random numbers.
        3. time(NULL) will return the current EPOCH time, it is commonly used with srand() to get new random sequences. 
    2. Getting a random number
      1. rand(); 
        1. Returns the next random number in the sequence seeded by srand().
        2. Returns an int.
    3. srand() and rand() are both in <stdlib.h>
    4. time() is in <time.h>
And now for the assignment:
  1. Create an array large enough to store 10 ints.
  2. Populate the array with random values.
  3. Set the last value in the array to 0.
  4. Print out the values in this array
  5. Create a separate array large enough to store 10 ints.
  6. USING ONLY POINTERS (that is, do not use the array variables) do the following:
    • Populate the second array with the values in the first but in reverse order
  7. Print out the values in the second array
For this assignment, do not create helper functions, put everything inside main (it won't be too long). We will talk about passing arrays and pointers are function arguments in a few days.

github link: https://github.com/mks65/array_swap.git

Work 02: Get to the point.

posted Sep 14, 2018, 10:04 AM by JonAlf Dyrland-Weaver

This is going to seem strange to many of you, but follow along.

Write a program that:
  1. Declares and initializes an unsigned int to some value > 2.1 billion.
  2. Declare a char * that points to the address of your unsigned int.
    • Yes, I really do mean char *
  3. Print out your int in hex. 
    • The formatting character for a hexadecimal int is: %x
  4. Use your pointer to print out each individual byte of your int.
    • The formatting character for a single byte in hex is %hhx, that is half of half of an integer.
  5. Using your pointer, modify the individual bytes of your int and print out the resulting value in hex and decimal after each modification.
    1. Increment each byte by 1
    2. Increment each byte by 16
  6. Feel free to try other things
NOTE: You do not need anything more than the basic pointer operations and the printf formatting characters to do this assignment. Stay away from confusing posts on stack overflow (and the like).

GitHub clone url: https://github.com/mks65/pointy.git

Work 01: If you're feeling rusty, apply eul.

posted Sep 7, 2018, 6:02 AM by JonAlf Dyrland-Weaver   [ updated Sep 8, 2018, 10:40 AM ]

The time has come for you to write a c program.
  1. Look at the problems here: http://projecteuler.net/index.php?section=problems
  2. They are all math problems of some sort, pick two and write a c program to solve them.
    • Some good starters are problems 1, 5 and 6
    • Finished early? pick a third, a fourth, a fifth...
  3. Remember, c syntax is the same as java, so all your old friends (if, while, else, for...) are around.
    • You cannot declare a variable in a for loop, but you can initialize it.
    • If you want to create separate functions, do that above main() 
      • You do not need to make separate functions, but if you do, they must be declared before being used.
  4. In order to print out variable values, you'll need to know a little more about printf()
    • printf( string, arg0, arg1, ...)
    • The string argument can contain placeholder characters, which will be replaced with the arguments listed after the string.
    • %d represents an int value.
    • %lf represents a floating point value.
    • The arguments are used in the order they are listed.
    • example
      • printf("here be numbers: %d %lf", 51, 37.472); will print the string: here be numbers 51 37.472
    • We will learn more about printf when we get back.
  5. Use scanf if you want user input (we'll talk about it Monday, but you can run  $ man scanf from the command line to get info on it). This is not required.
Github link
https://github.com/mks65/euler.git

Work 00 9/5

posted Sep 5, 2018, 7:28 AM by JonAlf Dyrland-Weaver

  • There are things I need to know about you. Fill out the following form with all the information.
  • Supplies needed:
    • A notebook
      • You will have to take notes in this class, on paper (why not on these computers sitting on our desks you ask?, Here are some reasons.)
      • You may use any kind of notebook, but it must be an organized notebook, not random sheets of paper in a folder.
    • Pens and pencils
      • Have a few of each, try multiple colors to make your notes even better.
    • Access to a suitable computer
      • This is a class in Linux systems programming. See the resources page for info on how to get situated.
    • Ducky
    • Github account

1-9 of 9