CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Summary: We consider basic issues related to the implementation and use of functions in C.
Prerequisites: C basics.
Contents:
a. Create a new directory for this lab. I'd suggest something like
courses/csc161/labs/functions.
b. In that directory, create the standard Makefile.
Consider the following program.
#include <stdio.h>
void
increment (int i)
{
printf ("Before incrementing, i is: %d\n", i);
i += 1;
printf ("After incrementing, i is: %d\n", i);
i += 1;
printf ("After incrementing again, i is: %d\n", i);
} // increment
int
main ()
{
int x = 10;
printf ("When we begin, x is %d.\n", x);
increment (x);
printf ("When we finish, x is %d.\n", x);
return 0;
} // main
a. What output do you expect to get from this program? (You need only
note the values of x and i.)
b. Check your answer experimentally.
c. What does your answer tell you about the way functions in C treat their integer parameters?
d. Change increment and the rest of the program to use
float values instead of int values.
e. What effect do you expect this to have on the output of the program?
f. Check your answer experimentally.
g. What does your answer tell you about the way functions in C treat their real number parameters?
Consider the following program:
#include <stdio.h>
void
munge (char str[])
{
printf ("About to munge \"%s\"\n", str);
str = "munged";
printf ("After munging: \"%s\"\n", str);
} // munge
int
main (int argc, char *argv[])
{
printf ("When we begin, argv[0] is %s.\n", argv[0]);
munge (argv[0]);
printf ("When we finish, argv[0] is %s.\n", argv[0]);
return 0;
} // main
a. What output do you expect to get from this program? (You need only
note the values of argv[0].)
b. Check your answer experimentally.
c. What does your answer tell you about the way functions in C treat their string parameters when our primary operation on strings is assignment?
Consider the following program:
#include <stdio.h>
void
munge (char *args[])
{
printf ("About to munge \"%s\"\n", args[0]);
args[0] = "munged";
printf ("After munging: \"%s\"\n", args[0]);
} // munge
int
main (int argc, char *argv[])
{
printf ("When we begin, argv[0] is %s.\n", argv[0]);
munge (argv);
printf ("When we finish, argv[0] is %s.\n", argv[0]);
return 0;
} // main
a. What output do you expect to get from this program? (You need only
note the value of argv[0].)
b. Check your answer experimentally.
c. What does your answer tell you about the way functions in C treat their array parameters?
Consider the following program:
#include <stdio.h>
void
munge (char str[])
{
printf ("About to munge \"%s\"\n", str);
str[0] = 'X';
printf ("After munging: \"%s\"\n", str);
} // munge
int
main (int argc, char *argv[])
{
printf ("When we begin, argv[0] is %s.\n", argv[0]);
munge (argv[0]);
printf ("When we finish, argv[0] is %s.\n", argv[0]);
return 0;
} // main
a. What output do you expect to get from this program? (You need only
note the value of argv[0].)
b. Check your answer experimentally.
c. What does your answer tell you about the way functions in C treat their string parameters when our primary operation on strings is assignment?
Determine through experimentation answers to the following questions:
a. What happens if you fail to predeclare a function that returns an
int. That is, what happens if you call this function
and the definition of the function follows the call?
b. What happens if you fail to predeclare a function that returns a
float?
c. What happens if you fail to predeclare a functions that returns
void?
d. What happens if you fail to predeclare a function and provide the function with the wrong type or number of parameters?
As you may recall, while we often do exponentiation through loops, it is more efficient to use a recursive formulation.
Write a procedure that implements this approach. The procedure should have the signature
double exponentiate (double x, int n);
Write a procedure that determines whether its integer parameter is a prime number. Your procedure should have the signature
int is_prime (int n)
Friday, 1 October 2010 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/functions-lab.html.
Tuesday, 1 March 2011 [Samuel A. Rebelsky]
Wednesday, 2 March 2011 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/functions-lab.html.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed Mar 2 11:34:18 2011.
The source to the document was last modified on Wed Mar 2 11:34:16 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/functions-lab.html.