Problem Solving With C



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 "void" normally reserved for in C?


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










A + 1






22 / ((double) 7)






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:

(c) &
(f) >
(h) !



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























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;


doc_mod = 0;



// Function 2

float PI(void)


// Return an approximation of the constant "PI"




// Function 3

void Goodbye(void)


int 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





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




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;




*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









*pPtr3 / *pPtr2


*pPtr2 - 25








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 main() procedure that does the following to each member of the structure array:

(a) Initializes the "m_nNum" of the member with a unique numeric value.

(b) Initializes the "m_szString" member with the phrase "Testing xx" where "xx" is the same numeric value loaded into "m_nNum." (Use
sprintf() to do this).

(c) Print out all members of the array in a neat format to demonstrate that each is holding correct data.


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:

void PrintStruct( struct MyStruct aStruct );


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:

void PrintPointerStruct( struct MyStruct* aStruct );



Homework 6



  1. What is a file?

  3. List and explain the three steps in the general approach to file input and output.

  5. What must be done with all disk files before exiting a C program?

  7. What is a stream?

  9. When a disk file is opened for the purpose of reading or writing text information, what character translations take place when an I/O operation take place?

  11. When a disk file is opened with the intent of reading or writing binary (structure) information, what mode must be used, and why?

  13. Explain the meaning of each of the mode strings below as used by the fopen() function. Present your answers in tabular form (like below.)
















  15. How do you detect errors during the fopen() function? How does fopen() let you know whether it is successful or not?

  17. What is the purpose of the fseek() function?

  19. How is the rewind() function similar to the fseek() function? How are they different?

  21. Study your C compiler's documentation and/or the textbook: Explain the purpose of the fread() and fwrite() functions. Explain each of the four parameters in a clear manner.


  23. What is wrong with the following application of fwrite()?


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);


printf("%d Items were written to the file.\n", nResult);






printf("Sorry, couldn't open the file!");