C++/c++

Advertisement


Question
Q.Please can you tell me how to build my own library?also how can I include different functions in it??

Q.I have a confusion ... I made a function of factorial but my problem is that whenever input some large no i loose some information please take a look and tell me what is wrong:

unsigned long factorial( int n)  //defining function factorial//
{  
 int a;//identifying variables//
 unsigned long b;
    
 b=1;//calculating factorial//
  for(a=n;a>=1;a--)
  {b= (b*a);}
  return b;//returning the value of calculated factorial//
}          

the actual problem is when i use the answer of this function factorial and do some further calculations i get incorrect answers....the same prob i get when i use the funtion (pow) in math.h library.....

Q.also help me understand pre increment and post increment...
eg
{
int k;
int m=5;
k= ++m - --m + m++ + ++m;
cout<<k<<endl;
cout<<m<<endl;

l= m++ + 2 - --m + 6*m + m++;
cout<<m<<endl;
cout<<l<<endl;
}
how can i get the answer of k and m and l without using a compiler.... especially in calculating l which part is solved first the arithmetic operators???

COMPILER: Microsoft visual C++ 6.0  

Answer
First thanks for mentioning the compiler you are using.

1/ Building a library:
----------------------
As you give few specifics on your library requirements, and you insist on asking a couple more questions to boot this will be a quick overview. Please post a followup question if you wish further details on any points covered here.

As you do not mention whether you require a dynamic load library (DLL) or static (LIB) library I shall assume a static library as they are easier to grasp and work with. A static library is just an archive of its component object files.

First you have to separate your code out into modules.

The main thing about a library is that it is not a program and so requires no main function.

With MSVC++ 6.0 using the Visual Studio IDE the easiest way is to select the relevant project type.

The library becomes a MSVC++ project in its own right, separate from program application projects. You add functions to it by adding source files to the project in which you place your function definitions. You should add function declarations (a.k.a. function prototypes) for publicly visible functions into one or more header files for your library, usually in a separate directory to the source code as these files need to be available to all code using the library, whereas the source files containing the function definitions do not.

The project compiles each source file into an object file (.obj file), then runs the MS LIB application to create the project .lib static library target from all the object files.

You can use the library from application program projects by including the relevant library header file(s) in the program project's source files (or header files in some cases) and adding the library to those used to link against. If the library project is in the same workspace as the program project then it can be added to the application program project as a dependency.

The command line equivalent to using a MSVC 6.0 static library project would be to compile without linking each C/C++ source file using the MS CL application using the /c option (note: you have to run the vcvars.bat file first to set up the environment).

Then when all source files are successfully built into object files use the LIB application to create the static library 'archive'.

For more information on the specific details of using MSVC 6.0, static and dynamic link libraries etc., as with all Microsoft development topics, refer to the MSDN web site: http://msdn2.microsoft.com/, specifically the library (I warned you that I would point you there for such topics in my instructions to questioners). However, as MSVC 6.0 is now over 10 years old you might get better information from its own supplied MSDN library documentation. Note that I no longer have this product version to hand - sorry.


2/ Wrong results with large value operations
---------------------------------------------
Again, as you asked several questions I am not going to go into a whole lot of detail. Please ask a followup if you require further explanation of any points.

Computer data types are _not_ infinite. For C and C++ on a typical 32-bit system an int is a 2s complement 32-bit value that can hold only values between approximately - or + 2000,000,000 [in fact the values are -(2^31) to +(2^31 - 1) which are a bit over - and + 2 billion], and an unsigned long is also typically 32-bits in size for 32-bit compilers and can hold values between 0 and (2^32) - 1, a bit more than 4000,000,000.

This is the case for Microsoft MSVC++ 6 programs. Hence using large factorial values and powers can easily exceed this value. Note that pow returns a floating point value and not an integer, in fact the C++ library supports several overloads of the pow function:

   float       pow( float, float );
   float       pow( float, int );
   double      pow( double, double );
   double      pow( double, int );
   long double pow( long double, long double );
   long double pow( long double, int );

Note that all of them return one of the floating point types. Floating point types can only be converted sensibly to an integer for the range of values in which the two types' value ranges overlap.

A floating point value can express very small or large values to a certain degree of precision. The limits on how small or large and the precision available depends on the compiler/platform and the type in question. The range and precision of long double is at least that of double and the range and precision of double at least that of float. Typically float is a 32-bit floating point value and double a 64-bit floating point value. For Microsoft compiler long double is the same as double (on GNU x86/AMD64 compilers long double is an 80 bit floating point type). For more on floating point see http://en.wikipedia.org/wiki/Floating_point for example.

In the case of integer types the values tend to wrap around when they over or under flow, so you still get a result just not the one you expected.

The only fix is to use data types that can hold larger values - e.g. a 64-bit integer such as __int64, or use a big number / arbitrary precision library - see http://en.wikipedia.org/wiki/Bignum for starters, and maybe look at http://mattmccutchen.net/bigint/.

Alternatively you can add code to limit the value of n passed into the function - and I would suggest considering making the n parameter unsigned: what is the factorial of -3 especially if non-negative results are expected?


3/ Pre increment and post increment
-----------------------------------

The reason you are _not_ getting understandable results is that what you are doing in these lines is illegal C++:

   k= ++m - --m + m++ + ++m;
   l= m++ + 2 - --m + 6*m + m++;

In both cases m is modified more than once in the expression statement. This is _not_ allowed as compilers are allowed to re-arrange and execute expression in various ways to their liking.

For a fuller explanation see some of my previous answers:

   http://en.allexperts.com/q/C-1040/increment-decrement.htm
   http://en.allexperts.com/q/C-1040/Pre-post-increment.htm
   http://en.allexperts.com/q/C-1040/2008/10/Question-C-using-HELP.htm

Again, please ask a followup question if you require further clarification.


ADDITIONAL
----------

I have thought about your request for how the values of k and l were produced and have a possible solution for k = 11. I shall leave the contortions required for the l = 30 case to you - especially as you failed to provide the values of m after each expression. Oh, please do not post me another follow-up with the values of m - its your brain that wants to know not mine so it can be your brain that can waste its time playing with this puzzle more if you are really that interested!

Try this for the k=11 case:

In the variable names below r can be considered to mean 'register' indicating where a possible load or store between memory and processor register might occur. The statements commented as optional might be produced by an un-optimised code generator but can safely be removed without affecting the results.

   int m = 5;
   int r1 = m;
   int r2 = m;
   ++r1;       // r1 = 6   ++m
   m = r1;     // optional
   --r2;       // r2 = 4   --m
   m = r2;     // m = 4
   r1 -= r2;   // r1 = 2   ++m - --m
   r2 = m;     // optional
   r1 += r2;   // r1 = 6   ++m - --m + m++
   r2 = m;     // optional
   ++r2;       // r2 = 5   ++m
   m = r2;     // optional   
   r1 += r2;   // r1 = 11  ++m - --m + m++ + ++m
   ++r2;       // r2 = 6   ++ for m++ after *both* uses
   m = r2;     // m = 6
   int k = r1; // k = 11

Have fun with the l = 30 case.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.