Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

PA 1 (5 Points)

Introduction to C Programming

Goals

• To get experience programming in C

• To learn how to use man pages

• To start using Linux (using rhea to compile and run your code)

Introduction and Getting Started

NOTE: Please read this whole section before starting!

In this assignment, you’ ll be writing three programs: sizes, tokenize, and cards.

The project includes some stuff to help get you started and help you test things out.          Connect to rhea and run the following command (once you’ve read the rest of this section):

setup 343 PA1

When this completes, it will create a directory called PA1 in a folder called COMSC343 in your shared folder.  This will contain the three C files mentioned in the assignment (though they don’t do anything interesting yet!).

There's also a Makefile included.1   You can do make sizes” to compile the sizes program. You can do make test_sizes” to run some tests similar to ones that will be used for          grading.  There are variants of the above for the other two parts as well.  Additionally, “make all” (or just make”) makes everything, and “make tests” will make and test everything (it may be wise to run this before submitting!).  As a side note, it’s often the case that while    grading assignments, I’ll find students doing things I hadn’t considered and will adjust the   grading scripts.  If you think your program is okay, but it’s not passing tests, let me know!

Part 1: Type Sizes (1 point)

C provides (at least) four different fundamental integer types: char, short, int, and long. In Java, the sizes of predefined types like char and int are defined as part of the Java      language, so they’ re always guaranteed to accept the same range of values. This isn’t the  case in C.  In C, different implementations can use different sizes for these basic types. A  benefit is that an implementation will generally choose the size that a given processor is    most efficient with as the basic int type.  However, it can make it difficult to write a          program that behaves the same way on all machines!  Additionally, while one might expect each of these basic types to have a different size, all the language standard requires is that long is at least as large as int, int is at least as large as short, short is at least as large as char, and char can hold a basic character (generally this means an ASCII character –   most of the things you can easily type on a standard English keyboard).

C provides a sizeof operator. Its operand can be an expression or the name of a type.  The result is the size of the type of the operand in bytes. Usually people put the operand in        parentheses, which makes sizeof look like a function, but it isn’t one (you can’t pass the    name of a type to a function!).  For example, sizeof(int) will tell you the number of bytes used to store an int value.  If you had an int variable named foo, then sizeof(foo)     would return the same thing.  For that matter, 1 is an int, so sizeof(1) would do the same thing yet again!

For this part of the assignment, edit the sizes .c program so that it uses the sizeof     operator to determine the size of the four main integer types as well as the size of pointers and prints out the results. (Hint: the universal pointer type in C is void * -- it can be a     pointer to anything. )

When writing this program, you will need to use the printf function.  Use the %zu specifier  in the format string for the value resulting from sizeof . If you need a refresher on printf, you can look at its manual page. There is also a command line tool called printf, so a        simple “man printf” might turn that up instead.  To get around that, manually specify that  you are interested in section 3 of the manual, which is the one about C functions: “man 3   printf”.

When you look at man pages, be sure to note what files you need to #include as specified in the synopsis section of the man page.  Read the description and return value sections for details on how to call the functions.  (Don’t be surprised if you do not yet completely          understand the man pages, but you should be able to get the information you need there.)

Ultimately, your program should produce output in the following format, though the X”s should be replaced by the number of bytes for the type!

size of char is X

size of short is X

size of int is X

size of long is X

size of void * is X

Note: C has lots of types.  For example, unlike Java, C also provides both signed and         unsigned variants of all of the basic integer types, e.g., unsigned int”. Additionally,       modern versions of the C standard let you include the <stdint.h> header, which defines    integers of fixed sizes, such as int8_t (8 bit signed), and uint32_t (32 bit unsigned). And if long isnt long enough, theres usually a long long”, as well. And theres more!

Part 2: Tokenizing a String (2 points)

In this part, you'll write a program (tokenize .c) that takes either one or two command line arguments: a quoted string and, optionally, a positive integer.  Note that the string needs to be quoted in order to make it just one argument (argv[1]).  Without the quotes, the words would get broken into separate elements of argv automatically.  Instead, your program       should break the string into words and output the word at the requested position.  If the     integer is missing, the program should output each word on a separate line.  Here are some example outputs:

[rhea :PA1]$ ./tokenize "This is example" 2

is

[rhea :PA1]$ ./tokenize "This is example"

This

is

example

[rhea :PA1]$ ./tokenize "This is example" 5

The string contains fewer than 5 tokens .

When writing this program, you will need to use the following C functions:

●    printf - use %s in the formatting string to put a string argument into the output and %d or %i to put an integer into the output.

●    char * strtok (char *str, const char *delim) - strtok will help you break    the string into tokens.  See the man page for details on how to use it by running man strtok.

●     int atoi (const char *nptr) - atoi is used to convert a numeric string to an integer.  See its man page for details.

If the user enters too few or too many arguments on the command line, you should output an error message and quit with a non-zero return value (indicating an error).  Your error   message should briefly tell the user what arguments are expected.  The automated tests   (run them with "make test_tokenize") test several error cases.

Part 3: Deck of Cards (2 points)

In this question, you will gain experience with C arrays and structs.  The program should     create astandard deck of 52 cards, with suits of clubs, diamonds, hearts, and spades, and  with ranks Ace through King.  Each card should be represented by a struct where one        element stores the suit, and another element stores the rank.  The entire deck should be an array of such structs.

After creating the deck, you should shuffle it.  The simplest way to shuffle the deck is to      repeatedly swap pairs of cards.  To swap two cards, generate two random numbers between 0 and 51 and then swap the cards at the corresponding positions of the array.  You may      need to experiment to see how many times you need to swap cards to end up with a deck that appears to be in a random order.  (If you’ re interested in shuffling, you might look up the Fisher-Yates algorithm.)

The function to call to generate random numbers in C is called rand.  As with printf,         there’s a command line tool called rand, so you’ ll want man 3 rand”.  The random number generator isn't truly random; given a particular "seed" value, it always outputs the same     sequence of numbers.  A common trick is to seed it with the current time when your           program starts, so that on different runs, it will produce different values.  The skeleton code for cards .c does this if you don't give it any command line arguments (using the srand and time functions).  If you do give it an argument, it expects this to be an integer and uses it   as the seed.  You can leave this skeleton code alone, but you might want to read it over!

Once you’ve got your deck shuffled, print it out, one line per card.  It should look something like this (but more cards and probably not in this order!):

King of Clubs

3 of Spades

Ace of Spades

9 of Hearts

6 of Spades

. . .

Grading

You're given a makefile and a bunch of tests -- use them!  The provided tests are the         baseline required for a good grade.  In particular, programs that do not compile will not be graded.  I will grade them if there are compiler warnings (though you shouldn’t have    any!), but if it won’t even compile, it gets a zero.

Grading of the programs will be based primarily on correctness, but you can also lose points if your documentation or coding style is weak.

Turning in your solution

Upload your three C files (sizes.c, tokenize.c, and cards.c) to the PA1 Moodle assignment. Please ensure that they have the correct filenames.