COMP-215
Problem Solving With C
Homeworks
Instructions
: All homeworks for this course are to be contained within a 3-tab paper folder with the following information clearly marked on front: Your name, COMP-215, Professor Wheeler, Spring 1999. The homeworks will be placed in reverse order within the folder so that the latest homework is visible upon opening the folder. All work is to be typed or word-processed. Use complete sentences when answering questions.
Homework 1
1. Explain the difference between a low and high-level computer language. Which one allows the user to more readily concentrate on the solution of a problem, and why?
2. What are the objects in the C language?
3. Outline the steps in the solution of a problem using a computer programming language.
4. Draw a block diagram illustrating each step that takes place when a C program is compiled.
5. Define the following terms using a complete sentence for each:
(a) Source code
(b) Assembly language
(c) Object code
(d) Library
(e) Machine language
6. List the fundamental C object types in the order of least to most precise. For each one, give the type of information that it is normally used to store.
7. What is the type "
8. Where do all C programs begin execution?
Homework 2
1. What is a C "expression?"
2. For each of the following C expressions, give the data type and result. Use a tabular format similar to what is presented here.
C Expression |
Data Type |
Result |
7 |
||
25. |
||
‘ A’ |
||
7+3 |
||
‘ A’ + 1 |
||
22/7 |
||
22/7. |
||
22 / ((double) 7) |
||
0x1f |
||
023 |
||
7/3 + 1.5 |
||
(7*3 - 2 ) / 10. |
||
(float) 3 |
||
(int) (22. / 7. ) |
||
22. / ((int) 7) |
3. Describe the purpose of the following C operators:
(a)
4. For the following PRINTF format specifiers, give the data type expected and field-width. If applicable, give the number of decimals to the right of the decimal point.
Format Specifier |
Data Type Expected |
Width and Decimal Precision |
%d |
||
%x |
||
%c |
||
%ld |
||
%f |
||
%lf |
||
%10.2f |
||
%3c |
||
%s |
||
%20.2ld |
Homework 3
1. What is a C function?
2. What is meant when we state that C is a "one-pass" compiler?
3. What is the default data type for C functions?
4. How should a C function that returns no data (and therefore doesn’t appear on the right-hand side of an equals sign) be declared?
5. What are the parameters of a function? When calling a function, does C automatically convert function arguments to the correct data type?
6. What is a prototype? Why are prototypes needed in C?
7. A global variable is visible to what parts of a C program? What are the initial contents of a global variable? How long does a global variable persist (exist)?
8. What parts of a C program have access to a local variable? What are the initial contents of a local variable? When does a local variable begin to exist, and when is it destroyed?
9. Write prototypes for the following short C functions:
// Function 1
void SetModifiedFlag( int nFlag )
{
// Set the global document-modified flag according to the argument.
if (nFlag) doc_mod = 1;
else
doc_mod = 0;
}
// Function 2
float PI(void)
{
// Return an approximation of the constant "PI"
return(3.1415927);
}
// Function 3
void Goodbye(void)
{
int i;
for(i=0;i<10;i++)
printf("\nGoodbye, world!\n");
}
10. Explain what is wrong with the following C function. How could the error be fixed?
// Function 4, bad coding (compiler will flag an error on
// this one)
void Funct4( float value1 )
{
float temp;
temp = value1 * value1 + 1; // Compute x^2 + 1
return(temp);
}
11. A recursive procedure is one that uses previous output as an input. C functions permit this, since each new copy of the function has a new and independent set of local variables. Predict the output of the following program; run it to confirm your prediction.
#include <stdio.h>
#include <math.h>
//
// This is an example of a recursive function call -- a function
// that calls itself.
//
float Factorial( float value )
{
if (value < 2)
return(1); // 0! and 1! = 1 by definition
else
return( value * Factorial( value - 1. ) );
}
//
// The main event
//
void main(void)
{
float x;
printf(" X X!\n---------------\n");
for(x=0; x< 8; x++)
printf("%2d %6.0f\n", (int) x , Factorial(x) );
}
Homework 4
1. What is a memory model? List the six memory models that your C compiler supports (see the documentation / text).
2. How much data can be held in memory under the small memory model (the default model your compiler uses)?
3. What is a C pointer?
4. What is meant by the phrase "dereferencing a pointer?"
5. How are the arguments being passed to the function below? Can the function modify the caller's original variables that have placed their data into value1 and value2? Why or why not?
void MyFunction( int value1, float value2)
{
// ... function code here ..
}
6. How are the arguments being passed to the function below? What are the advantages and disadvantages of this method of passing parameters?
void NewFunction( int* value1, float* value2)
{
// ... function code here ..
}
7. Write a program statement that properly calls the function of question 6.
8. Write a version of NewFunction() [Question 6] that takes the sum of the contents of value1 and value2 and stores the result into the variable stored at value2.
9. A program has the following variables and declarations:
int nCount=5;
int nIncrement=6;
float Result;
int* pPtr1;
int* pPtr2;
float* pPtr3;
pPtr1 = &nCount;
pPtr2 = &nIncrement;
pPtr3 = &Result;
*pPtr3 = ( *pPtr1 + *pPtr2 ) * 3.1415927;
(*pPtr2)++;
*pPtr1 = 25;
What will be the final values in nCount, nIncrement, and Result?
10. Given the declarations of problem 9, fill in the information in the table below. Some expressions may be invalid.
C Expression |
Data Type |
pPtr1 |
|
*pPtr1 |
|
pPtr3 |
|
&nIncrement |
|
*pPtr3 / *pPtr2 |
|
*pPtr2 - 25 |
|
pPtr2 |
|
*Result |
11. What is Hungarian notation? Why is it used in C programming? Give at least two examples.
Homework 5
1. What is a structure?
2. List and explain the steps involved in defining and allocating memory for a structure.
3. What is meant by the phrase "Instantiating an object?"
4. How is the "." operator used with structures?
5. For the structure below, write a
struct MyStruct {
int m_nNum;
Char m_szString[40];
} Database[1000];
6. Convert the printing code in your
main() function from problem 5 into a function called printstruct() that takes one instance of the structure and prints it to the screen. Print out and comment this function, and make the necessary changes to main() so that the program runs properly. The prototype for the function must be as follows:
7. What are the disadvantages of passing structures by value as the function of problem 6 does?
8. Convert the function of problem 6 so that it uses a pointer to the structure, instead of a copy. Print out and comment this function, and make the necessary changes to
main() again so that the program runs properly. The prototype for the function must be as follows:
Homework 6
MODE STRING |
Meaning... |
"r" |
|
"rb" |
|
"rb+" |
|
"w" |
|
"wb" |
|
"wb+" |
struct SomeStruct aStruct; // Define one instance of a previously
// defined structure
//
// Program logic here to fill aStruct with data
//
FILE* f1;
int nResult;
f1 = fopen("datafile", "wb");
if (f1 != NULL)
{
nResult = fwrite( &aStruct, 1, sizeof(int), f1);
fclose(f1);
printf("%d Items were written to the file.\n", nResult);
}
else
{
printf("Sorry, couldn't open the file!");
}