CISC105 - General Computer Science
Homework #3
(solutions)
Structures, Character Strings and I/O
1. (6) Given the following code, set the year for today's date to 1991.
struct date
{
int month;
int day;
int year;
};
struct date todays_date;
todays_date.year = 1991;
2. (6) Write the code to call a function named 'number_of_days' passing today's date
from Problem #1 above as an argument.
number_of_days( todays_date );
3. (6) Initialize a date structure (that has the format given in Problem #1) called
'tomorrows_date' with the date July 4th, 1776.
static struct date tomorrows_date = { 7, 4, 1776 };
or
static struct date tomorrows_date;
tomorrows_date.month = 7;
tomorrows_date.day = 4;
tomorrows_date.year = 1776;
4. (10) Using the date structure from Problem #1, declare a date structure with twelve
birthdays and set the month of the fifth entry to twelve.
struct date birthdays[12];
birthdays[4].month = 12;
5. (10) Code a structure called 'date_time' containing a date structure and a time
structure as its members.
struct date_time
{
struct date sdate;
struct time stime;
};
6. (10) Code a structure called 'employee' containing a social security number and a
twenty character name as its members.
struct employee
{
long int ssn;
char name[20];
};
7. (6) Given the following code and using the code from Problem #6, set the first
character of the name to R.
struct employee payroll_rec;
payroll_rec.name[0] = 'R';
8. (6) Rewrite the following code using a character string:
static char word [] = {'H','e','l','l','o','!','\0'};
static char word[] = {"Hello!"};
9. (6) Code a statement to display the following:
Single quotes (') and backslash (\) are special.
printf("Single quotes (\') and backslash (\\) are special.\n");
10. (6) Given the following information, convert the character '5' to the number 5
using integer arithmetic. (ASCII values for the characters '0' through '9'
are 48 through 57 respectively).
int i;
char c = '5';
i = c - '0'; or i = c - 48;
11. (10) Write the code needed to read from a file.
#include
FILE *file_ptr;
file_ptr = fopen("file_name", "r");
12. (18) Match the function with its purpose.
_A___fprintf A. Writes to a file instead of a terminal.
_B___fscanf B. Reads from a file instead of a terminal.
_C___fopen C. Initializes a file for I/O operations.
_D___fclose D. Tells the system that a file is no longer
needed to be accessed.
_E___feof E. Tests for an end of file condition.
_F___fgets F. Reads entire lines of data from a file.
_G___fputs G. Writes entire lines of data to a file.
_H___getc H. Reads a single character from a file.
_I___putc I. Writes a single character to a file.
J. Reads a single character from a terminal.
K. Writes a single character to a terminal.