Solution to Assignment 4

1. A possible program is here.
That's how the program looked before I wrote the implementation of the class functions.

2a. (Sect. 2.1 #9). Q:   Although the total number of bytes varies from computer to computer, memory sizes of 65,536 to more than several million bytes are not uncommon. (It's obsolete. For computers in use today, multiply these numbers by 100. - S.S.) In computer languages, the letter K is used to represent the number 1024=210, and the letter M is used to represent the number 1,048,576=220. Thus, a memory of size of 640K is really 640 x 1024 = 655,360 bytes, and a memory size of 4 MB is 4 x 1,048,576= 4,194,304 bytes. Using this information, calculate the actual number of bytes in:
a) a memory containing 8 MB
Answer: 8 x 220=8,388,608 bytes
e) a memory consisting of 8M words, where each word consists of 2 bytes
Comment: The formulation in the book, saying 8MB words rather than 8M words, is somewhat confusing; I think it is a typo. The letter B should stand for "bytes".
Answer: 8 x 2 x 220=16,777,216 bytes
g) a floppy diskette that can store 1.44 MB
Answer: 1.44 x 220= 1,509,949 bytes (The number of bytes must be integer.)

2b. Sect.3.2 #1. Q:   If average is a variable, what does &average mean?
A:   &average means "the address of the variable average."
#2. Q:   For the variables and addresses illustrated in Fig., determine &temp, &dist, &date, and &miles.
      Figure:
 
Addresses: 16892 16893 16894 16895 16896 16897 16898 16899
           |------- temp --------| |------ dist ---------|
 
Addresses: 16900 16901 16902 16903 16904 16905 16906 16907 
           |---------------- date -----------------------|
 
Addresses: 16908 16909 16910 16911 16912 16913 16914 16915
           |----------------  miles  --------------------|
A:   &temp=16892,    &dist=16896,     &date=16900,     &miles=16908.

#4. Q:   If a variable is declared as a pointer, what must be stored in that variable?
A:   An address of another variable.

2c. Sect.3.2 #12. Q:   Using the sizeof() operator, determine the number of bytes used by your computer to store the address of an integer, character, and double precision number. (Hint: sizeof(int*) can be used to determine the number of memory bytes used for a pointer to an integer.) Would you expect the size of each address to be the same? Why or why not?
A:   The following program can be used to determine the required sizes:
     /* Math 2120, Fall'04, Asst 4, Q.2c */
     /* This program prints the number of bytes used
     	by a computer to store the address of int, char, and double.
     	Then it prints the number of bytes used to store values of
     	these types.
     */
 
     #include <stdio.h>
 
     int main()
     {
       printf("Size of address of int is %d\n",sizeof(int*));
       printf("Size of address of char is %d\n",sizeof(char*));
       printf("Size of address of double is %d\n",sizeof(double*));
 
       printf("\nCompare the sizes of the values:\n");

       printf("Size of int is %d\n",sizeof(int));
       printf("Size of char is %d\n",sizeof(char));
       printf("Size of double is %d\n",sizeof(double));
  
       return(0);
     }      
Here is the output:
     Size of address of int is 4
     Size of address of char is 4
     Size of address of double is 4

     Compare the sizes of the values:
     Size of int is 4
     Size of char is 1
     Size of double is 8
As we see, every address occupies 4 bytes. We should have expected that these numbers are same, because all the addresses denote the first memory location used by a variable pointed to. All memory locations are numbered by consequtive integers, which can be used to denote location of a variable of any type.

2d. Sect.6.5 #2. Q:   The addresses of the integer variables sec, min, and hours are to be passed to a function named time(a,b,c). Write a suitable function header for time).
A:   void time(int *a, int *b, int *c);"

#9. Q:   The following program uses the same variable names in both the calling and called functions. Determine if this causes any problem for the computer. What problems might this pose to a programmer? Also determine the type of data stored in each variable.
   #include <stdio.h>
   
   void time(int*, int*);
   
   int main()
   {
     int min, hour;
     printf("Enter two numbers :");
     scanf("%d %d", &min, &hour);
     time(&min, &hour);
     return 0;
   }
   
   void time(int *min, int *hour)
   {
     int sec;
     sec= ((*hour)*60 + *min)*60;
     printf("The total number of seconds is %d", sec);
     return;
   }
A:   The fact that the same variable names are used in both the calling and called functions doesn't cause any problem for the computer. It presents however a problem for the programmer and especially for the program's reader, because in main() the variables min, hour store the respective values, while in the function time(..) the variables with the same names denote addresses of the values. It would be better to call the variables in time(..) something like min_ptr, hour_ptr.