Due date: Wednesday, February 17, 2010, 11:55pm.
Task 1 (25 points)
Write a program, in a file called task1.c, that does the following:
- Ask the user to input how much money they have in their pocket.
- If the amount of money is less than or equal to $1.00, the computer should output "you cannot buy anything with that money".
- If the amount of money is greater than $1.00 and less than or equal to $10.00, the computer should output "you can buy candy with that money".
- If the amount of money is greater than $10.00 and less than or equal to $80.00, the computer should output "you can buy books with that money".
- If the amount of money is greater than $80.00 and less than or equal to $300.00, the computer should output "you can buy a digital camera with that money".
- If the amount of money is greater than $300.00, the computer should output "you can buy a computer with that money".
Your solution must satisfy the following requirements:
- The amount of money should be stored in a variable of type double.
Task 2 (25 points)
Mr. Bugs wants to write a simple program that sums up 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10. Unfortunately, the computer prints out that "the sum is 0.000000". Can you help Mr. Bugs fix his code?
Submit a file called task2.c, with the corrected version of the code.
Here is the program that Mr. Bugs wrote:
#include <stdio.h>
int main()
{
int i;
double result = 0;
for (i = 4; i <= 10; i++)
{
result = result + 1/i;
}
printf("the sum is %lf\n", result);
}
Task 3 (25 points)
Write a program, in a file called task3.c, that does the following:
- Ask the user to input two positive integers, A and B.
- If A > B, the program should exit without doing anything.
- Otherwise, the computer computes the sum 1.0/A + 1.0/(A+1) + 1.0/(A+2) + ... + 1.0/B, and prints out that sum.
Your program should work with any integer values A and B. Here are some examples:
- If the user enters 4 for A and 4 for B, the computer should print "0.25" (which is equal to 1.0/4.0).
- If the user enters 4 for A and 5 for B, the computer should print "0.45" (which is equal to 1.0/4.0 + 1.0/5.0).
- If the user enters 4 for A and 6 for B, the computer should print "0.616667" (which is equal to 1.0/4.0 + 1.0/5.0 + 1.0/6.0).
- If the user enters 4 for A and 7 for B, the computer should print "0.759524" (which is equal to 1.0/4.0 + 1.0/5.0 + 1.0/6.0 + 1.0/7.0).
Important: here is code you can use to store integer values in double numbers:
int a, b;
double a2, b2;
printf("enter A and B:\n");
scanf("%d %d", &a, &b);
a2 = a;
b2 = b;
You may (or may not) need to do that, depending on your solution. Have in mind that, in the C language, 1/4 is equal to 0, if 1 and 4 are integers, but 1.0/4.0 is equal to 0.25., if 1.0 and 4.0 are doubles.
Task 4 (25 points)
Write a program, in a file called task4.c, that redoes task 3, but using a function. In particular, you should define a function called double sum_fractions(int A, int B) that, assuming that A <= B, A returns the sum 1.0/A + 1.0/(A+1) + 1.0/(A+2) + ... + 1.0/B.
Important: as in task 2, you may find it useful to convert A and B into doubles inside your function, with code such as:
double sum_fractions(int A, int B)
{
double A2, B2;
...
A2 = A;
B2 = B;
}
How to submit
Submisions are only accepted via WebCT. The submission should include files task1.c, task2.c, task3.c, task4.c, answers.doc. Do not submit entire project directories, or anything else except for the .c files and the answers.doc file.
Your answers.doc file should be a single Microsoft Word file that includes:
- Your name and UTA ID number at the top.
- A copy of your code from task1.c.
- A copy of your code from task2.c.
- A copy of your code from task3.c.
- A copy of your code from task4.c.
Make sure that your answers.doc file is easy to parse, and that it is easy for us to figure out which part corresponds to which task. Feel free to use headings such as "start of copy of task1.c".
We try to automate the grading process as much as possible. Not complying precisely with instructions causes a significant waste of time during grading, and thus points will be taken off for failure to comply, and/or you may receive a request to resubmit.
Submission checklist
- Was the assignment submitted via WebCT?
- Did answers.doc contain your name and UTA ID?
- Did the submission include files task1.c, task2.c, task3.c, task4.c, answers.docs, and nothing else?