Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS1023 Homework 3 Recursion February 17, 2012 1. Write an application that has 4

ID: 3638845 • Letter: C

Question

CS1023 Homework 3 Recursion
February 17, 2012
1. Write an application that has 4 functions:
(a) int main()
Controls the program and makes calls to the other functions. Displays the results of the function calls.
(b) int GetData(string message)
Displays the message passed in its parameters and returns an integer value that it acquires from the user.
(c) int DigitalRoot(int n)
Calculates the digital root of an integer. The digital root of a number is defined by summing its digits repeatedly until only a single digit remains. For example:
563456 =
5 + 6 + 3 + 4 + 5 + 6 = 29 2 + 9 = 11
1+1=2
(d) int T(int m,int n)
Calculates the value of T(m,n) where
T(m,n) = n if m <= 1
T(m,n) = m if n <= 1
T(m,n) = 2 * T(m/2,n/3) otherwise
Output from your program might be:
This program demonstrates recursion
I will caclculate the digital root of the number you enter now:
1223
The digital root of 1223 is 8
Now I will calculate the value of T(m,n)
Enter m
5
Enter n
8
The value of T(5,8) is 0
2. Trace by hand the execution of
(a) DigitalRoot(85384) (b) T(67,67)

To be handed in:
Submit your source code via Blackboard. Copy and paste the output from your application as comments at the end
A plain text file with your tracings for Question 2
Hard copies of your code and solution for Question 2 must be submitted by 5:00pm Note that the marker collects the assignments promptly at 5:00pm on Fridays. You will be charged your one free extension if you submit even a minute late.
Be sure to include your name and student number on all files submitted

Explanation / Answer

#include /* all I/O functions */ #include /* all UNIX low level functions*/ #include /* all string manipulation functions */ #include /* permission modes for UNIX low level */ #include #include #include extern int errno; /* needed to go with perror() */ /* * describe a structure template, no variable of declared */ typedef struct tagPERSON { char name[30]; char street[20]; char city[20]; char state[3]; char zip[6]; char ssn[13]; int age; int height; int weight; } PERSON; /* * function prototypes */ int getdata( PERSON * ); int showdata(PERSON * ); int puts_gets( void ); int fprnt_fscan( void ); int fread_fwrite( void ); int read_write( void ); int err_handler(FILE *, char *, int ); /* * S T A R T O F P R O G R A M */ int main() { char ans[2]; int which; /* * which functions are to be executed */ do { printf(" Which set of I/O functions are to be tested?"); printf(" 1. fputs and fgets"); printf(" 2. fprintf and fscanf"); printf(" 3. fread and fwrite"); printf(" 4. read and write"); printf(" 5. quit this program"); printf(" Enter your selection: "); gets(ans); which = atoi(ans); switch(which) { case 1: puts_gets(); break; case 2: fprnt_fscan(); break; case 3: fread_fwrite(); break; case 4: read_write(); break; case 5: return(0); default: printf(" Invalid selection . . . try again!"); break; } }while(1); } /* * read data from screen into structure elements */ int getdata( PERSON *ptr) { int result; printf(" Enter your name: "); gets(ptr->name); printf(" Enter your street: "); gets(ptr->street); printf(" Enter your city: "); gets(ptr->city); printf(" Enter your state: "); gets(ptr->state); printf(" Enter your zip code: "); gets(ptr->zip); printf(" Enter your ssn: "); gets(ptr->ssn); printf(" Enter your age: "); scanf("%d",&ptr->age); printf(" Enter your height: "); scanf("%d",&ptr->height); printf(" Enter your weight: "); scanf("%d",&ptr->weight); /* * flush the input data stream so no newlines are left */ if((result = fflush(stdin)) == EOF) err_handler(stdin,"stdin",1); return 0; } /* * display the data held in structure elements on the screen */ int showdata(PERSON *ptr) { printf(" PERSON: %s",ptr->name); printf(" : %s",ptr->street); printf(" : %s",ptr->city); printf(" : %s",ptr->state); printf(" : %s",ptr->zip); printf(" : %s",ptr->ssn); printf(" : %d",ptr->age); printf(" : %d",ptr->height); printf(" : %d",ptr->weight); return 0; } /* * Using fputs() and fgets() write data to and read data back * from the disk. These functions only work with string data. */ int puts_gets() { FILE *fp; PERSON my; char *val; char ans[2],filename[16],text[80]; int rtnval ,linecnt ,lgth ; /* * load file name */ strcpy(filename,"testfil1.dat"); /* * open test data set */ if((fp = fopen(filename,"w")) == NULL) err_handler(fp,filename,1); do { /* * acquire data */ printf(" Enter Text:"); gets(text); /* * write to disk */ lgth = strlen(text); text[lgth] = ' '; /* replace NULL terminator */ text[lgth + 1] = ''; /* place NULL terminator */ if((rtnval = fputs(text,fp)) == EOF) err_handler(fp,filename,2); /* * keep going? */ strcpy(ans," "); printf(" Continue(Y/N)? "); gets(ans); }while(!strcmp(ans,"y")); if((rtnval = fclose(fp)) == EOF) { err_handler(fp,filename,3); } /* * open test data set */ if((fp = fopen(filename,"r")) == NULL) err_handler(fp,filename,3); /* * print the data back on the screen */ linecnt = 0; do { /* * read data from disk, newline is only way to * distinguish records */ if((val = fgets(text,sizeof(text),fp)) == NULL) if(err_handler(fp,filename,3)) break; /* * display data on screen */ printf(" Line %d:%s",linecnt,text); ++linecnt; strcpy(ans," "); printf(" Continue(Y/N)? "); gets(ans); }while(!strcmp(ans,"y")); if((rtnval = fclose(fp)) == EOF) { err_handler(fp,filename,3); } return 0; } /* * Using fprintf() and fscanf() functions write data to and * read data from the disk. Notice that fscanf() has same * limitations as scanf() */ int fprnt_fscan() { FILE *fp; PERSON my; char filename[16],lname[20],tstreet[20],ans[2]; int rtnval ; /* * load filename */ strcpy(filename,"testfil2.dat"); /* * open test data set */ if((fp = fopen(filename,"w")) == NULL) { perror("FPRNT_FSCAN(): cannot open file for write"); exit(3); } do { /* * inform user of limitations */ printf(" Enter only a single string "); printf("for name, street and city"); /* * acquire data */ getdata(&my); /* * write to disk */ if((rtnval = fprintf(fp,"%s %s %s %s %s %s %d %d %d ", my.name, my.street, my.city, my.state, my.zip, my.ssn, my.age, my.height, my.weight)) == EOF) { perror("FPRNT_FSCAN(): cannot fprintf to file"); exit(4); } /* * keep going? */ strcpy(ans," "); printf(" Continue(Y/N)? "); gets(ans); }while(!strcmp(ans,"y")); fclose(fp); /* * open test data set */ if((fp = fopen(filename,"r")) == NULL) { perror("FPRNT_FSCAN(): cannot open file for read"); exit(5); } /* * print the data back on the screen */ do { /* * read data from disk, notice fscanf has same * limitation in scanning disk data as scanf has * in screen data; it delimits values by whitespace */ if((rtnval = fscanf(fp, "%s %s %s %s %s %s %s %s %d %d %d", my.name, lname, my.street, tstreet, my.city, my.state, my.zip, my.ssn, &my.age, &my.height, &my.weight)) == NULL) { perror("FPRNT_FSCAN(): cannot fscanf from file"); exit(3); } /* * display data on screen */ showdata(&my); strcpy(ans," "); printf(" Continue(Y/N)? "); gets(ans); }while(!strcmp(ans,"y")); fclose(fp); return 0; } /* * Using the fwrite() and fread() functions write data to and * read data from the disk. These are block oriented, * high-level buffered I/O functions. */ int fread_fwrite() { FILE *fp; PERSON my; char filename[16],ans[2]; int rtnval ; /* * load filename */ strcpy(filename,"testfil3.dat"); /* * open test data set, binary mode because of integer * values to be written */ if((fp = fopen(filename,"w+b")) == NULL) { perror("FREAD_FWRITE(): cannot open file for write"); exit(6); } do { /* * acquire data */ getdata(&my); /* * write to disk; * &my = the address of the buffer to be written * sizeof(my) = the number of bytes to be written * 1 = the number of items of the above size to be * written * fp = the stream pointer */ if((rtnval = fwrite(&my,sizeof(my),1,fp)) == EOF) { perror("FREAD_FWRITE(): cannot fwrite to file"); exit(7); } /* * keep going? */ strcpy(ans," "); printf(" Continue(Y/N)? "); gets(ans); }while(!strcmp(ans,"y")); fclose(fp); /* * open test data set, allow for binary data because of * integer type values */ if((fp = fopen(filename,"r+b")) == NULL) { perror("FREAD_FWRITE(): cannot open file for read"); exit(8); } /* * print the data back on the screen */ do { /* * read data from disk; must be tested for less than * the count of items (1) to detect EOF */ if((rtnval = fread(&my,sizeof(my),1,fp)) < 1) if(err_handler(fp,filename,9)) break; /* * display data on screen */ showdata(&my); strcpy(ans," "); printf(" Continue(Y/N)? "); gets(ans); }while(!strcmp(ans,"y")); fclose(fp); return 0; }