Well I've been busy and have managed to produce this piece of code for Practical 3. I put an explaination below :-)
#include <stdio.h>
int checkTrees (int number);
int askAgain (int value);
int main() {int num1, num2, ctRes, sum;printf(
"Please enter the number of trees you have planted: ");scanf("%d", &num1);num1 = checkTrees (num1);
printf("Please enter the probable absorption per tree (kg): ");scanf(
"%d", &num2);sum = num1 * num2;
printf("Your planting scheme will absorb %d kilogrammes of Carbon Dioxide.\n", sum);if (num2<750) {printf(
"Your trees are absorbing very little carbon dioxide each,\nyour overall absorption would be higher if you used better trees.\n");}
if (sum>100000) {printf("Your carbon dioxide absorption is making a significant impact\non the world's environment! :)");}
return 0;
}
int checkTrees (int number) {if (number < 1) {askAgain(1);
} else {
return number;}
}
int askAgain (int value) {int num1;printf(
"Please enter a number of trees greater than 0: ");scanf("%d", &num1);checkTrees (num1);
}
Basically, it checks with function checkTrees, then if it's less than 1 it asks again with the askAgain function, then passes the value back to checkTrees which checks again. If it's 1 or more this time, it passes the inputted value back into num1. As a bit of an extra, if you have less than 750kg absorption on each tree it recommends you get better trees, and if you absorb loads of carbon dioxide (more than 100 000 kg) it congratulates you for making a significant contribution to saving the planet. Fun!
Keywords: SE1SA5, trees
Comments
I guess elgg stole your indents.
At least it is better than Blackboard which throws out things in triangular brackets.
Nice bit of recursion.
Shirley Williams on Thursday, 25 October 2007, 10:15 BST # |
Gah! Yes it did! How rude :P
At least it didn't delete anything =D
Chris on Thursday, 25 October 2007, 22:58 BST # |
David Johnson on Friday, 26 October 2007, 09:25 BST # |