In this lab, you will create a recursive function that will use a skeleton progr
ID: 660114 • Letter: I
Question
In this lab, you will create a recursive function that will use a skeleton program in which you will add missing code that uses a recursive function and project the outcome written to a text file.
1. Write a recursive function find_sum that calculates the sum of successive integers starting at 1 and ending at n (i.e., find_sum(n) = (1 + 2 + . . . + (n ? 1) + n).
2. Define a structure type called subscriber_t that contains the components name, street_address, and monthly_bill (i.e., how much the subscriber owes).
3. Identify and correct the errors in the following program:
typedef struct
{
char name[15], start_date[15];
double hrs_worked;
}
summer_help_t;
/* prototype for function scan_sum_hlp goes here */
int
main(void)
{
struct operator;
/*one error in here */
scan_sum_hlp(operator);
printf("Name: %s Starting date: %s Hours worked: %.2f ", operator);
/*two errors in here */
return(0);
}
4.Assume these declarations for the problem that follows:
double x;
int n;
char ch, str[40];
Indicate the contents of these variables after each of the following input operations is performed. Assume that the file accessed by indatap consists of the data given and that each lettered group of operations occurs at the beginning of a program, immediately following a statement that opens the file.
123 3.145 xyz<newline>35 z<newline>
a. fscanf(indatap, "%d%lf%s%c", &n, &x, str, &ch);
b. fscanf(indatap, "%d%lf", &n, &x);
fscanf(indatap, "%s%c", str, &ch);
c. fscanf(indatap, "%lf%d%c%s", &x, &n, &ch, str);
d. fscanf(indatap, "%s%s%s%d%c%c", str, str, str, &n, &ch, &ch);
Explanation / Answer
1.
int find_sum(int n){
if (n == 0)
return n;
return n*find_sum(n-1);
}
2.
struct subscriber_t {
string name;
string street_address;
float monthly_bill;
}
3.
struct operator; is wrong instead we should make object
summer_help_t sht;
/* We can not give operator as a argument to printf. Instead we will provide variable of the struct opeartor to printf*/
// Wrong one printf("Name: %s Starting date: %s Hours worked: %.2f ", operator);
// Right one printf("Name: %s Starting date: %s Hours worked: %.2f ", sht.name,sht.start_date,sht.hrs_worked);
4. a) fscanf(indatap, "%d%lf%s%c", &n, &x, str, &ch);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.