Previous Exercise
Course Outline
For Exercise 3, let's practice some logic skills and implement a recursive function.
Problem:
Write a program that asks the user for two inputs - base and exponent. Define a recursive function power(int base, int exponent) that will compute for the result and display it on screen. For simplicity, assume that both the base and exponent are integers and the latter is non-negative.
Hint:
For any integer x,
x0 = 1
x1 = x
xn = x * xn-1
Previous Exercise
Course Outline
Saturday, September 12, 2015
Exercise 2: Phytagorean Triples
Previous Exercise
Course Outline
Next Exercise
Phytagorean triples are three numbers in which the sum of the squares of the two is equal to the square of the other one. Example is 3, 4, and 5, where in 9 + 16 = 25.
Problem:
Find out all the Phytagorean triples in which all elements are less than the given user input.
Previous Exercise Course Outline Next Exercise
Phytagorean triples are three numbers in which the sum of the squares of the two is equal to the square of the other one. Example is 3, 4, and 5, where in 9 + 16 = 25.
Problem:
Find out all the Phytagorean triples in which all elements are less than the given user input.
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<stdio.h> int main() { int i, j, k, num; printf("input a number: "); scanf("%d", &num); for (i = 1; i <= num; i++){ for (j = i; j <= num; j++){ for (k = 1; k <= num; k++){ if (i*i + j*j == k*k) { printf(" %5d%5d%5d\n", i, j, k); } } } } return 0; } |
Previous Exercise Course Outline Next Exercise
Friday, September 11, 2015
Exercise 1: Basics
Course Outline Next Exercise
For Exercise 1, we'll practice getting input from the user, displaying text and values to the console, implementing loop and basic arithmetic operations.
Problem:
Get 10 numbers from user. Output the sum, average, minimum and maximum.
Course Outline Next Exercise
For Exercise 1, we'll practice getting input from the user, displaying text and values to the console, implementing loop and basic arithmetic operations.
Problem:
Get 10 numbers from user. Output the sum, average, minimum and maximum.
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include<stdio.h> #define SIZE 10 int main() { int num; float ave, sum = 0; int max = 0, min = 0, i; for (i = 0; i < SIZE; i++) { printf("Input a number: "); scanf("%d", &num); sum = sum + num; if (max < num || i == 0) { max = num; } if (min > num || i == 0) { min = num; } } printf("The sum is %.0f.\n",sum); ave = sum/10; printf("The average is %.2f.",ave); printf("\nThe largest value is %d.", max); printf("\nThe smallest value is %d.", min); return 0; } |
Course Outline Next Exercise
Recursion
Previous Topic
Course Outline
Recursion might be one of hardest concept to grasp for a newbie but once you get the hang out of it, you'll find it really cool!
When you see a function that is calling itself, that's a recursion. So basically, you're performing something onto a subject over and over again until it becomes what you desire.
An example would be dropping the first and last letters of a word until its length becomes 1 (for odd-length strings) or 2 (for even-length strings) which will give you the word's middle letter(s).
theComSciGeek
heComSciGee
eComSciGe
ComSciG
omSci
mSc
S
One important thing in recursion is you should know where to stop. That's your basis. In this example, we stop if the length of the string is already less than or equal to 2.
Another example would be determining if a number is a multiple of 2.
I know that these are not so clever examples of recursion but these are pretty easy to grasp. Just remember this rule thumb: when you feel like you're doing the same thing over and over again to a certain subject, that's when recursion comes in.
Most (if not all) recursions can be implemented using loop(s). It's just that recursions are awesome and most of the time, if not always, less expensive than loops with regards to both space and time complexity.
Previous Topic Course Outline
Recursion might be one of hardest concept to grasp for a newbie but once you get the hang out of it, you'll find it really cool!
When you see a function that is calling itself, that's a recursion. So basically, you're performing something onto a subject over and over again until it becomes what you desire.
An example would be dropping the first and last letters of a word until its length becomes 1 (for odd-length strings) or 2 (for even-length strings) which will give you the word's middle letter(s).
theComSciGeek
heComSciGee
eComSciGe
ComSciG
omSci
mSc
S
One important thing in recursion is you should know where to stop. That's your basis. In this example, we stop if the length of the string is already less than or equal to 2.
Another example would be determining if a number is a multiple of 2.
1 2 3 4 5 6 7 8 9 | int isMultipleOfTwo(int num) { if (num == 2) { return 1; } if (num < 2) { return 0; } return isMultipleOfTwo(num/2); } |
I know that these are not so clever examples of recursion but these are pretty easy to grasp. Just remember this rule thumb: when you feel like you're doing the same thing over and over again to a certain subject, that's when recursion comes in.
Most (if not all) recursions can be implemented using loop(s). It's just that recursions are awesome and most of the time, if not always, less expensive than loops with regards to both space and time complexity.
Previous Topic Course Outline
Functions
Previous Topic
Course Outline
Next Topic
Functions are basically set of commands that we can call over our program.
First, we have the function declaration (line 1) consists of return data type (int), function name (isGameOver) and parameters (noOfTries) separated by commas.
Right after the declaration, we have the function body.
When to create a function?
Previous Topic
Course Outline
Next Topic
Functions are basically set of commands that we can call over our program.
There are two main reasons why they exist:
- Readability. We don't want to flood our main() function with a lot of statements. The main function should only contains the main flow of the program.
- Re-usability. Instead of copy-pasting set of commands, we can define a function to hold it and call it whenever it is needed.
In C, this is the structure of a function,
1 2 3 4 5 6 | int isGameOver(int noOfTries) { if (noOfTries > 10) { return 1; } return 0; } |
First, we have the function declaration (line 1) consists of return data type (int), function name (isGameOver) and parameters (noOfTries) separated by commas.
Right after the declaration, we have the function body.
When to create a function?
- You have to perform something on a subject over and over gain. Define 'something' as the function body and the 'subject' as the parameter.
- You don't want to clutter the main function. In this case, you put certain lines of code into a function with a void return type (use this when your function doesn't return anything), and even empty parameters.
Subscribe to:
Comments (Atom)