Question: You are given an array height[] of positive integers containing the heights of the children in a class. Write a function that takes height[] as an input and gives the shortest height as the output.
For example -
Input - [23,29,20,22,23,22,19,25,21,26,23,24,18,30]
Output - 18
Request: Pls take the quiz the right side bar to give feedback on the easiness/difficulty level of the questions. Thanks
Showing posts with label code. Show all posts
Showing posts with label code. Show all posts
Wednesday, April 30, 2008
Tuesday, April 29, 2008
Solution to Group 3 Problem
Group 3.
View the question here.
Here we want to practice a bit of recursion.
Whenever you go about writing recursive code, you have to keep two things in mind.
1. There must be a terminating condition.
2. As the number of recursions increase, the problem state should approach the terminating condition.
Lets see that with respect to this program.

Now lets come back to the 2 conditions that we discussed above.
1. There must be a terminating condition. As you see, the (if n < 10) is the terminating condition. It is a terminating condition because a number is returned if that condition is satisfied. (Unlike the default case which calls the function again)
2. As the number of recursions increase, the problem state should approach the terminating condition. This is also satisfied because, we keep dividing the number by 10 in the default condition. So the number will keep becoming smaller and smaller and finally become <10, and the terminating condition will be reached.
OK, so did you find a problem with the program above?
Yes? -> Great!
No? -> If you check the previous post, I have mentioned that whenever we write a program, we should check for boundary conditions. This program won't work for negative numbers.
How to make it work for negative numbers?
This is your assignment.
Request: Please take the quiz or leave your comment to help me make this site more useful. Thanks
View the question here.
Here we want to practice a bit of recursion.
Whenever you go about writing recursive code, you have to keep two things in mind.
1. There must be a terminating condition.
2. As the number of recursions increase, the problem state should approach the terminating condition.
Lets see that with respect to this program.

Now lets come back to the 2 conditions that we discussed above.
1. There must be a terminating condition. As you see, the (if n < 10) is the terminating condition. It is a terminating condition because a number is returned if that condition is satisfied. (Unlike the default case which calls the function again)
2. As the number of recursions increase, the problem state should approach the terminating condition. This is also satisfied because, we keep dividing the number by 10 in the default condition. So the number will keep becoming smaller and smaller and finally become <10, and the terminating condition will be reached.
OK, so did you find a problem with the program above?
Yes? -> Great!
No? -> If you check the previous post, I have mentioned that whenever we write a program, we should check for boundary conditions. This program won't work for negative numbers.
How to make it work for negative numbers?
This is your assignment.
Request: Please take the quiz or leave your comment to help me make this site more useful. Thanks
Labels:
answer,
c++,
code,
program,
programing,
programming,
recursion,
solution
Solution to Group 2 Problem
Group 2
View the problem here.
Here we have to add the sum of digits of a number. We can go about doing it by taking taking 1 digit at a time and adding it to the result.

Note: that whenever you write programs like these, always test for boundary conditions. Eg. Will the program work when the input is 0? What if the number is a negative number? etc.
Please click on the image for clarity.
Request: Please take the quiz or leave your comment to help me make this site more useful. Thanks
View the problem here.
Here we have to add the sum of digits of a number. We can go about doing it by taking taking 1 digit at a time and adding it to the result.

Note: that whenever you write programs like these, always test for boundary conditions. Eg. Will the program work when the input is 0? What if the number is a negative number? etc.
Please click on the image for clarity.
Request: Please take the quiz or leave your comment to help me make this site more useful. Thanks
Labels:
c++,
code,
problem,
program,
programing,
programming
Monday, April 28, 2008
So you wanna code huh?
The best way to be a good coder is to write code and learn from other coders better than you.
I will post 1 problem every single day for people who have just started programming. I will post the solution in the night, the same day.
Based on user feedback, I will make the problems tougher or easier. So, do take part in the quiz.
I will post 1 problem every single day for people who have just started programming. I will post the solution in the night, the same day.
Based on user feedback, I will make the problems tougher or easier. So, do take part in the quiz.
Subscribe to:
Posts (Atom)