MCA World :: Computer Application ::
Questions :  Answers : Solutions of TMA & Project Assignment  of  MCA courses of IGNOU, NIT & Other Universities.

::: Data Structures



Home • Introduction to Software • Data Structures • System Engineering • Database Management System • Discrete Mathematics • Numerical & Statistical Computing • Software Engineering • Operation Research • Accounting & Finance • Computer Architecture • Operating Systems • Intelligent Systems • Relational Database • Obejct Oiented Systems

Data Structures


Main Chapters

Home
Introduction to Software
Data Structures
System Engineering
Database Management System
Discrete Mathematics
Numerical & Statistical Computing
Software Engineering
Operation Research
Accounting & Finance
Computer Architecture
Operating Systems
Intelligent Systems
Relational Database
Obejct Oiented Systems

Addition of  two very large floating numbers

Question 1:  Write a program in 'C' language to add very large floating-point numbers. The floating point numbers may be equal to maximum floating point number that is possible on your machine.

Ans :  Floats (Floating point numbers) are numbers with fractional parts i.e, with decimal or float point. The floats are typically represented as follows

            float bank_amt= 1.2345E7

The Mantissa is 1.2345 and Characteristic also called Exponent is 7.

Floats are stored in four bytes and are accurate to about seven significant digits and their exponentials range extends over the interval [E-37 to E+38].

Some times scientific or astronomical computation often requires a far greater range and accuracy than provided by single precision floats. The double precision floats (8 bytes) which are held accurately up to 15 figures and have range [E-307 to E+308].

ANSI C has another Float type called  long double  and are accurate to about 19 significant digits and their exponentials range extends over the interval [E-4931 to E+4932].

The  above program to add very large floating point can be written in following manner if the two inputs (in  floats) and their sum (in double) do not exceeds the limits mentioned  just before this para.

# include <stdio.h>

main()

{ float num1,num2;

double sum;

printf ("Enter Two Very large Floats in the permissible range\n");

printf ("Enter first Float \n");

scanf('%e", &num1);

printf ("Enter Second Float \n");

scanf('%e", &num2);

sum = num1+num2;

printf ("The sum of  %e + %e = %le ", num1,num2,sum);

return 0;

}

General case Program : when the floats are really very large, even more than what is permissible by long double. That is when the exponents are greater than +4932 (the max allowed by long double) and are required to be added. The following C programs does it. It adds such numbers.

Salient Points of the program  :

·        The program takes the two very large floats as  string input  with 80 digits max permissible for each input.

·        It separates the Mantissa and exponents and stores separately in different strings for each number.

·        It normalises the mantissa of first number wrt to second number and adds the two mantissa separately.

·        The combined/added mantissa with the exponent of the second number is displayed as the required sum.

The Listing with adequate comments is in the box.

 

/* Program for Addition of two very large floating numbers. The Numbers may be larger than the permissible float/double numbers */

 #include<stdio.h>

# include<math.h>

# include <string.h>

# include <iostream.h>

# include <stdlib.h>

main ()

            {

            int len1=0,len2=0;

int len_mant_1=0,len_mant_2=0,len_mant=0;

/* 80 digits max  */

            char num1[80] ="";

            char num2[80] ="";

            char *num3="eE";

            char key, *mants1,*exp1,*mants2,*exp2;

   double mant;

/* Mantissa and Exponents are separated and stored separately in strings variables

mants1, exp1, mants2, exp2 respectively for the two numbers */

   printf("\n\n\n");

   printf("| --------------------------------------------------------------------------|\n");

   printf("| Enter Two very large Floating Point Numbers of 80 digits with exponentials |\n");

   printf("|   such as 1.23456788888E1234567 or 1.23444444444E+300001111     |\n");

   printf("| --------------------------------------------------------------------------|\n");

   printf("\n\n");

   printf("Enter First Number  : ");

               gets(num1);

               printf("Enter Second Number : ");

               gets(num2);

     mants1 = strtok(num1,num3);

             exp1 = strtok(NULL,num3);

             mants2 = strtok(num2,num3);

             exp2 = strtok(NULL,num3);

             printf ("\nAs per this program Your's \n");

             printf ("Number 1 -> mantissa  = %s....exponential = %s\n",mants1,exp1 );

             printf ("Nember 2 -> mantissa  = %s....exponential = %s\n",mants2,exp2 );

     printf ("\n\n\n");

     printf ("---------------Results-------------------\n\n");

            len_mant_1 = strlen(mants1);

            len_mant_2 = strlen(mants2);

/* len_mant is the number of required digits of mantissa and which shall be

equal to the largest value of both the mantissa, so minimum value is fixed

as per the input of user's

*/

            if (len_mant_1 > len_mant_2)

                        len_mant = len_mant_1;

            else 

                        len_mant = len_mant_2;

/* Check for the validity of the Mantisaa and exponents.

            They should not be equal to zero . If so terminate the program. */

            if(atof(mants1) == 0 )

                        {

                        printf ("** Mantissa Part for First number is invalid **\n");

                        printf ("** Program Terminated Re-run the program  **\n");

                        goto stop;

                        }

            else if(atof(mants2) == 0 )

            {

                        printf ("** Mantissa Part for Second number is invalid **\n");

                        printf ("** Program Terminated Re-run the program  **\n");

                        goto stop;

            }

            /* Check whether the Exponential is zero */

                        else if(atol(exp1) == 0 )

            {

                        printf ("** Exponential Part for First number is invalid.**\n");

                        printf ("** Program Terminated Re-run the program  **\n");

                        goto stop;

            }

                        else if(atol(exp2) == 0 )

                        {

                        printf ("** Exponential Part for Second number is invalid.**\n");

                        printf ("** Program Terminated Re-run the program  **\n");

                        goto stop;

                        }

                        else;

 /* Normalise the exponentials with respect to exp2 i.e, the output will be

 Written with the eponential of second Number

 */

 {

            mant = atof(mants2)+atof(mants1)*(pow(10,(atol(exp1)-atol(exp2))));

          gcvt( mant, len_mant, mants1 );

 printf( "    Addition= number 1 + number 2  =  %sE%s \n",mants1,exp2);

    printf ("\n\n\n");

   printf( "< To quit type 'q' or 'Q' or to add more number press Any Other Key >");

   key = getchar();

   if (key =='q' || key== 'Q')

               goto stop;

   else

               main ();

}stop:

            return 0;

            }

 

The following is the sum of the runouts of the programs.

 

Typical Out Put :

 | ----------------------------------------------------------------------------------------------|

| Enter Two very large Floating Point Numbers of 80 digits with exponentials   |

|       such as 1.23456788888E1234567 or 1.23444444444E+300001111            |

| ------------------------------------------------------------------------------------------    -|

 Enter First Number      : 2.3333333E200001

 Enter Second Number :  1.1111111E200002

 As per this program Your's

Number 1 -> mantissa  = 2.3333333  exponential = 200001

Number 1 -> mantissa  = 1.1111111  exponential = 200002

  ---------------Results-------------------

    Addition= number 1 + number 2  =  1.3444444E200002

 < To quit type 'q' or 'Q' or to add more number press Any Other Key >Enter 

 --------------------------------------------------------------------------------------------------

| ----------------------------------------------------------------------------------------------|

| Enter Two very large Floating Point Numbers of 80 digits with exponentials   |

|       such as 1.23456788888E1234567 or 1.23444444444E+300001111            |

| ------------------------------------------------------------------------------------------    -|

 Enter First Number      : 1.11E1234567890        

 Enter Second Number :  3.33E1234567890

 As per this program Your's

Number 1 -> mantissa  =  1.11 exponential = 1234567890

Number 1 -> mantissa  = 3.33  exponential = 1234567890

  ---------------Results-------------------

    Addition= number 1 + number 2  =  4.44E123456789

 < To quit type 'q' or 'Q' or to add more number press Any Other Key > Q 

 

 

Links for Web Developers Web Publishers

 

Links from Google

 

 
 


Home • Introduction to Software • Data Structures • System Engineering • Database Management System • Discrete Mathematics • Numerical & Statistical Computing • Software Engineering • Operation Research • Accounting & Finance • Computer Architecture • Operating Systems • Intelligent Systems • Relational Database • Obejct Oiented Systems



This is complete MCA study materials containing question, solutions, answers, programming, scripts, TMA, Project assignment solutions for aspiring MCA or BCA student. This website may be useful for all in general and IGNOU student in particulars. Disclaimer : Execution and correctness of programs/answers may be established by the users himself and the mcaworld.net shall not held responsible for any damage/loss caused due to the use of code over this site.



Webmaster Bipin Bihari Pandey, An IT Expert of DotNet Technology, MCA from IGNOU
bipin9830@rediffmail.com or bipin9830@yahoo.com