Create a lab2 directory in /home/username/cspp50101 for your work for Lab 2.
Files for this lab are located in: /stage/classes/archive/2011/summer/50101-1/lab/lab2. This directory contains four .c files:
$ ls /stage/classes/archive/2011/summer/50101-1/lab/lab2/*.c /stage/classes/archive/2011/summer/50101-1/lab/lab2/ex1.c /stage/classes/archive/2011/summer/50101-1/lab/lab2/ex2.c /stage/classes/archive/2011/summer/50101-1/lab/lab2/ex3.c /stage/classes/archive/2011/summer/50101-1/lab/lab2/ex4.cCopy these four files into your own lab2 directory.
After you hit CTRL-C, the program will terminate and control will return to the shell prompt:
Enter a number, 1-10: 5 111111111111111111111111111111111111111111111111111111111111111111111111111111111 (... lots of rows of ones ...) 111111111111111111111111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111^C $Also note that this program uses the function scanf() to allow the user to enter a number at runtime. scanf() reads text input from standard input (i.e., from the keyboard) and converts it to a data type as indicated by conversion specifiers. Here, "%d" indicates that scanf() should read an integer from standard input:
printf("Enter a number, 1-10: "); scanf("%d",&n);You can read more about scanf() in Chapter 4 of Prata. In particular, you should be familiar with the different conversion specifiers used with integers ("%d"), floating point numbers ("%f"), characters ("%c") and strings ("%s"). These are the same conversion specifiers that you use with printf().
This program ex1.c goes into an infinite loop because it has a run-time error, meaning that it compiles but does not run as intended. Here is what this program should output:
Enter a number, 1-10: 6 123456That is, the program should print every integer from 1 up to the number entered by the user.
Fix this program so that it displays the correct output, as shown above.
HINT: In C, whitespace and indention are not used to determine the start or end a block of code. If the body of a loop or if statement has more than one statement then you must enclose it in curly braces. For example:
if(n>=10) { printf("Invalid entry\n"); return 0; }
Also note that vi provides parenthesis matching. If you move the cursor over a parenthesis, curly brace, or bracket and hit SHIFT-5 (or, '%'), then the cursor will move to what vi finds to be the best matching symbol. This will be helpful as you debug your own programs.
If you are not already familiar with this feature of vi try it out now in your ex1.c file.
Your task for this exercise is to modify ex2.c so that it also displays the length of the string.
Also note that in this example, the ampersand (&) is not used with scanf() when a string is read from standard input:
File ex3.c already has a main() function that calls num_quarters():
For this exercise, add another function to ex4.c called string_length(). This function should return the length of the string, in characters.
(You can refer back to your code in Exercise 2.)
Also add a printf() statement to main() to test your new function. Your output should look like this:
First, be sure that every source file you submit includes this information in comments
at the top of the file:
Also include a simple README file in your lab2 directory.
This file should include your first and last name, your username, the list of files that you are submitting, and any other comments or questions for the Lab TA. For example:
Do not submit any compiled binary files
You will submit this lab with the hwsubmit program.
Update (6/28): Before you leave today, be sure to set up CS email forwarding.
prepared by Sonjia Waxmonsky
Exercise 1: ex1.c
After you fix the runtime error for Exercise 0, modify ex1.c so that numbers are displayed in descending order. Output should look like this:
$ ./ex1
Enter a number, 1-10: 7
7654321
Exercise 2: ex2.c
File ex2.c is a simple program that prints the first character in a word. If the word only has one character than a message is displayed:
$ ./ex2
Enter a word: Mississippi
Mississippi starts with: M
$ ./ex2
Enter a word: M
M starts with: M
M only has one letter!
In this program you can see how C uses the null character '\0' to test for the end of a C-style string, or character array:
if (str[1]=='\0')
printf("%s only has one letter!\n",str);
When the user enters "M", the character 'M' is stored in str[0] (Remember that string indexes start at zero.) The null character '\0' is located at index 1, to indicate that there are no more characters in this string after 'M'.
So, the test in the above if-condition passes, since str[1] is equal to '\0'.
$ ./ex2
Enter a word: Mississippi
Mississippi starts with: M
Mississippi has 11 letter(s).
$ ./ex2
Enter a word: M
M starts with: M
M has 1 letter(s).
(You can either keep or remove the message "%s only has one letter!".)
printf("Enter a word: ");
scanf("%s",str);
You will learn more about this syntax when we get to pointers and memory addresses, but for now just remember this detail of working with scanf() and strings.
Exercise 3: ex3.c
Assume all Chicago parking meters have a rate of $0.25 per 5 minute interval, and do not accept any other coins.
Write a function called num_quarters() that has one argument, the number of minutes that the car will be parked, and returns the number of quarters the driver should put in the meter.
int main()
{
int m;
printf("How many minutes will your car be parked?: ");
scanf("%d",&m);
printf("Number of quarters needed: %d\n",num_quarters(m));
return 0;
}
To complete this exercise, implement num_quarters() in file ex3.c.
num_quarters() should return an integer, and should not call printf().
Your output should look like this:
$ ./ex3
How many minutes will you be parked?: 4
Number of quarters needed: 1
$ ./ex3
How many minutes will you be parked?: 5
Number of quarters needed: 1
$ ./ex3
How many minutes will you be parked?: 6
Number of quarters needed: 2
Note that you need to be careful with rounding. In integer division, 6 divided by 5 is equal to 1, since the remainder of 0.2 is not stored in the integer result. But at 6 minutes a 2nd quarter is needed.
How can you work around this?
Exercise 4: ex4.c
File ex4.c contains a function called starts_with() that returns the first character in a string:
char starts_with(char str[])
{
return str[0];
}
The output is similar to Exercise 2 above, except that the functionality of retrieving the first character in a string is moved into this starts_with() function. This function is called from main(), and the return value is displayed with printf():
printf("%s starts with: %c\n",str,starts_with(str));
Enter a word: Mississippi
Mississippi starts with: M
Mississippi has 11 letter(s).
You may not call printf() from function string_length().
Part 4 - Submitting your assignment
Lab 2 is due: Saturday, July 2 at 11:59pm
For example:
/* Sonjia Waxmonsky
* wax
*
* ex2.c: Converts from USD to JPY
*
* Uses exchange rates from June 22, 2011
*
*/
Sonjia Waxmonsky
wax
ex1.c
ex2.c
ex3.c
ex4.c
$ hwsubmit cspp50101lab <path to your lab2 directory>
Submit to cspp50101lab, NOT cspp50101.