Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #8473

Re: I have given my knowledge about c++ in the course ....Click and view the follwing link

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.programming
Subject Re: I have given my knowledge about c++ in the course ....Click and view the follwing link
Date 2018-03-14 11:11 +0000
Organization Fix this later
Message-ID <p8b00t$kk6$1@dont-email.me> (permalink)
References <529ece79-6578-4125-8f0c-0cc81727b2b4@googlegroups.com>

Show all headers | View raw


On 14/03/18 10:22, Pvr Pasupuleti wrote:
> https://unacademy.com/course/functions-in-c-for-programmers/90KD54O5

You seem to have got your server error sorted out very quickly. Well done.

So what do we have? Five videos, entitled "Functions", "Types of 
functions", "Types of functions 2", "Recursion", and "Inline Function". 
These videos last an average of just under 8 minutes each. Clearly I'm 
not going to sit through almost 40 minutes of video, so I'll just choose 
one at random, using the second hand on my clock as a randomiser (divide 
last digit by 2, giving me 0 to 4). It's 6. 6/2=3, so that's 0 1 2 3 the 
fourth video, Recursion. I'll stop at the first problem.

Okay, the first problem I see is at 25 seconds. (And I won't be going 
past the 25-second mark, either. What's there is what I'm going to 
comment on.)

Because you've chosen video, I'm going to have to copy-type the problem. 
Thanks a bundle. :-(

#include <iostream>
using namespace std;

We're two lines into the code, and already we've hit your first problem 
(in this video, at least). It's a *terrible* idea to drag in std like this.

What do you think namespaces are *for*?

Research tip: find out what namespaces are for, and then find a better 
way to write the code. Run it by some experts before you waste any more 
time making videos.

Since we're here anyway, let's press on:

int factorial(int a)  //function

Point to ponder: int is a signed type. What if a is negative? Did you 
think about that?

{
if(a==0 || a==1)

Second problem: your code doesn't use indentation to show the logical 
structure of the program. This is a recipe for errors.

Research tip: look up some common indenting styles that assist the 
reader in discerning the logical structure of the code. Find one you 
like and (in the absence of a house coding style) stick to it.

{
return 1 ;

This isn't actually wrong as such, but it's curious just how much effect 
such code has on graphical analysis tools such as McCabe.

}else
{
return a*factorial(a-1);
}
}

Third problem: let's pick up on that "point to ponder" from earlier. It 
seems that you haven't considered what happens if a is negative. 
Consider it now. If you call factorial(-1), what result will you get?

Research tip: find out more about validating parameters.

Fourth problem: assuming a four-byte int (probably a reasonable 
assumption nowadays) rather than the (perfectly legal) two-byte ints of 
old, what is the largest value of a you can deal with correctly?

Answer: 12.

The largest 4-byte int value is:

2147483648

12! is:

479001600

which fits comfortably.

But 13! is:

6227020800

which doesn't.

Research tip: think about the limitations of your data types, and plan 
accordingly.

Fifth problem: factorial calculation is a *terrible* example of 
recursion. Apart from the above problems, it doesn't give you any basic 
advantage over the obvious iterative technique, or the memoisation 
technique, both of which are better than the recursive solution.

In fact, there is only one common example of recursion I can think of 
that's actually worse than factorial calculation, and that's Fibonacci 
calculation.

Recursive techniques are tempting because they are so elegant, but they 
must be handled with care. For example, I have a recursive floodfill 
algorithm (in a rudimentary bitmapped graphics package I slung together 
for those rare occasions when I need to talk in bitmaps), and it works 
fine most of the time, but I know it will crash if I give it any 
complicated shape to fill because the recursion will crash the stack. As 
soon as I find the time to replace it with an iterative version, I will 
do so.

Research tip: find an example where recursion is a blessing rather than 
a curse.

Sixth problem: it's hard to be sure from inspecting the code on the 
video, but I think your output would look something like this:

Factorial of 4is24

Not good.

I haven't commented on your audio because it's incomprehensible.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.programming | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

I have given my knowledge about c++ in the course ....Click and view the follwing link Pvr Pasupuleti <pvr.ram34@gmail.com> - 2018-03-14 03:22 -0700
  Re: I have given my knowledge about c++ in the course ....Click and view the follwing link Richard Heathfield <rjh@cpax.org.uk> - 2018-03-14 10:27 +0000
  Re: I have given my knowledge about c++ in the course ....Click and view the follwing link Richard Heathfield <rjh@cpax.org.uk> - 2018-03-14 11:11 +0000

csiph-web