Unformatted text preview:

Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge EngineeringIn-class exercise 1Name(s):_______________________________________________ Points:________/46__Instructions: You will do this exercise in pairs. Turn this exercise in at the end of class. Only one copy per team must be turned in. All the questions pertain to the slides on “Introduction to C”.Questions:1. (3 points) What is the output of the program in example 1? 2. (3 points) What is the output of the program in example 2?Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge Engineering3. (4 points) The following program is a modified version of example 3. Complete the program by filling the spaces provided. #include < stdio.h> int main() { int nStudents, nFaculty; printf(“How many students and faculty does KU have?\n”); /* You may use only one scanf statement to accept the two values.Assume that a space will be used by the user to delimit the two input values */ scanf(_____________________________________________); /* You may use only one printf statement to print the values. Usea meaningful statement to do so. */ printf(_______________________________________ _________________________________________________); return 0; }Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge Engineering4. (3 points) This problem is based on example 4. Change the “if” statement in the program to a “while”, such that program will remain in the loop until “level” is greater than the DANGERLEVEL. float level = 1; ________________________________________________ {printf(“Low on gas!\n”); //Method or function that fills gas and updates level. level = FillingGas(); } printf(“ I’m on my way!\n”);Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge Engineering5. (2 points) In example 5, the statement: number[index] = index; following the “for” loop has been commented out. If you were to include the statement in the program, it will give you a run-time error, Why?6. (4 points) What is the output of the following code segment? How will you figure out what the function strlen() does? char name[] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’); printf(“ The length of the string is %d\n”,strlen(name)); printf(“ And the string is %s\n”,name);Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge Engineering7. a. (4 points) Define a structure called Inventory. It has the following attributes:- item number- quantity at hand- price- expiration date (month/year)The expiration date must be defined as a struct within the Inventory struct.b. (2 points) Declare an array of 10 such Inventory items.c. (2 points) Initialize the expiration date for the 5th inventory item to January 2005.Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge Engineering8. (4 points) Look at Example 7. For the following inputs, specify the outputs of the program. Assume that the name of the executable created from the program is a.out.a. prompt>>./a.out 5 3b. prompt>>./a.out “Strings are allowed” 5 9. Use the man pages(http://www.linuxmanpages.com/), to answer the following questions:a. (1 point) What is the type of the value returned by the fopen function?b. (2 points) Match the operation with the file open mode argument value? w error if file does not already exist. Else, file pointer at the beginning of file. a delete contents of existing file or create a new file. File pointer at the beginning of file. r create a new file if file does not exist. Preserve the contents if file does exist and place file pointer at the end of the file.Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge Engineering10. (5 points) Complete the following code segment:/* Declare a file pointer called in_file */_________________________________________/* open a file called “test.txt” for writing */________________________________________/* No exception handling – so check if file opened successfully */ if(in_file == NULL){ printf(“Error opening file!”); exit(1); /* exit program – don’t return to calling function */ }/* Write the string “Sabai dee reu” to the file *//* Function prototype: int fprintf(FILE *stream, const char *format, /* args*/ ...); */______________________________________________________________/* Write the value of the int variable “count” followed by the value of thefloat variable “price” to the file. Separate the two values with space */___________________________________________________________________/* Don’t forget to release the file pointer */ ____________________________________Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge Engineering11. (3 points) Complete the following code segment. The code segment reads two values(an int and a float) from a file “input.txt”. The format of the data in the text file is as shown below: <int>space<float> Code segment:intmain() { FILE *data_file; int x; float y; /* Open a file for reading */ Data_file = fopen(“input.txt”, ___________________); if(data_file == NULL){ printf(“ Input file does not exist! Quitting ….\n); exit(1); } /* Read an integer and a float from the input file */ _______________________________________________________ printf(“The values in the file are %d and %f \n”,x,y); fclose(data_file); return 0; }Data and Communications Networks (219234) Kasetsart University1st Semester 2009 Software and Knowledge Engineering12. (4 points) Given the function prototypes and assuming that function definitions exist, complete the code segment by writing the function calls. //Function prototypes: /* Function: Product The function returns the product of the two input parameters. */ int Product (int input1, int input2); /* Function: WriteToFileThe function writes to the file pointed to by


View Full Document

Rose-Hulman CSSE 432 - Study Notes

Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?