Amazon

Wednesday, May 18, 2016

C language String and Mathematical Functions





C's STRING AND MATHEMATICAL FUNCTIONS

Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.

Title: C's STRING AND MATHEMATICAL FUNCTIONS
Ø  Download the .Docx Format of this article. ClickHERE
Ø  Download the .Pdf Format of this article. ClickHERE

PART A - HANDLING OF CHARACTER STRINGS
§  In C language, strings are stored in an array of char type along with the null terminating character "\0" at the end.
§  In other words to create a string in C you create an array of chars and set each element in the array to a char value that makes up the string.
§  When sizing the string array you need to add plus one to the actual size of the string to make space for the null terminating character, "\0".
Syntax to declare a string in C:
char fname[4];

The above statement declares a string called fname that can take up to 3 characters. It can be indexed just as a regular array as well.

fname[] = {'t','w','o'};

                                          Character        t           w         o          \0
                                           ASCII Code      116      119      41        0

The last character is the null character having ASCII value zero.

v  Initializing Strings
                        To initialize our fname string from above to store the name Brian,

char fname[31] = {"Brian"};

                        You can observe from the above statement that initializing a string is same as with any array.                            However we also need to surround the string with quotes.

v  String Operations
      Character arrays are a special type of array that uses a "\0" character at the end. As such it has it is own header library called string.h that contains built-in functions for performing operations on these specific array types.
You must include the string header file in your programs to utilize this functionality.
Sample Code:
#include <string.h>
·         Length of a String
      Use the strlen function to get the length of a string minus the null terminating character.
      Sample Code:
int strlen(string);

      If we had a string, and called the strlen function on it we could get its length.
      Sample Code:
                  char fname[30] = {"Bob"};
       int length = strlen(fname);
      This would set length to 3.

·         Concatenation of Strings
      The strcat function appends one string to another.
      Sample Code:
char *strcat(string1, string2);
      The first string gets the second string appended to it. So for example to print a full name       from a first and last name string we could do the following:
      Sample Code:
           char fname[30] = {"Bob"};
       char lname[30] = {"by"};
       printf("%s", strcat(fname, lname));
      The output of this snippet would be "Bobby."

·         Compare Two Strings (Case Sensitive)
      Sometimes you want to determine if two strings are the same. For this we have the strcmp function.
Sample Code:
int strcmp(string1, string2);
      The return value indicates how the 2 strings relate to each other. if they are equal strcmp returns 0. The value will be negative if string1 is less than string2, or positive in the opposite case.

·         Compare Two Strings (Not Case Sensitive)
      If you do not care whether your strings are upper case or lower case then use strcmpi function instead of the strcmp function. Other than that, it's exactly the same.
Sample Code:
int strcmpi(string1, string2);
      Imagine using this function in place of strcmp in the above example, all of the first and last combinations would output 0.

·         Copy Strings
      To copy one string to another string variable, you use the strcpy function. This makes up for not being able to use the "=" operator to set the value of a string variable.
Sample Code:
strcpy(string1, string2);
      To set the first name of our running example in code rather than terminal input we would use the following:
Sample Code:
strcpy(fname, "Bob");

·         Converting Uppercase Strings to Lowercase Strings
      The strlwr function is not part of the ANSI standard and therefore strongly recommended against if you want your code to be portable to platforms other than Windows.
Sample Code:
strlwr(string);
      This will convert uppercase characters in string to lowercase. So "BOBBY" would become "bobby".

·         Reversing the Order of a String
      This function is not part of the ANSI standard and therefore strongly recommended against if you want your code to be portable to platforms other than Windows.
Sample Code:
strrev(string);
      This function will reverse the order of string. So if string was "bobby", it would become "ybbob".

·         Converting Lowercase Strings to Uppercase Strings
      The strupr function is not part of the ANSI standard and therefore strongly recommended against if you want your code to be portable to platforms other than Windows.
strupr(string);
      This will convert lowercase characters in string to uppercase. So "bobby" would become "BOBBY".D

Part B - Mathematical Functions
v  math.h library/header
                                     The math header defines several mathematic functions.
·         Functions:
acos();
asin();
atan();
atan2();
ceil();
cos();
cosh();
exp();
fabs();
floor();
fmod();
frexp();
ldexp();
log();
log10();
modf();
pow();
sin();
sinh();
sqrt();
tan();
tanh();

v  Trigonometric Functions
·         acos
                                                 Declaration:
                                                            double acos(double x);
                                                            Returns the arc cosine of x in radians.
                                                 Range:
                                                            The value x must be within the range of -1 to +1 (inclusive). The returned                                                                         value is in the range of 0 to pi (inclusive).
·         asin
                                                Declaration:
                        double asin(double x);
                                                            Returns the arc sine of x in radians.
                                                Range:
                                                            The value of x must be within the range of -1 to +1 (inclusive). The                                                                        returned value is in the range of -p/2 to +p/2 (inclusive).
·         atan
                                                Declaration:
                        double atan(double x);

                                                            Returns the arc tangent of x in radians.
                                                Range:
                                                            The value of x has no range. The returned value is in the range of -p/2 to                                                             +p/2 (inclusive).
·         atan2
                                                Declaration:
                        double atan2(doubly y, double x);
                                                            Returns the arc tangent in radians of y/x based on the signs of both                                                                       values to determine the correct quadrant.
                                                Range:
                                                            Both y and x cannot be zero. The returned value is in the range of -p/2 to                                                             +p/2 (inclusive).
·         cos
                                                Declaration:
                        double cos(double x);
                                                            Returns the cosine of a radian angle x.
                                                Range:
                                                            The value of x has no range. The returned value is in the range of -1 to +1                                                                         (inclusive).
·         cosh
                                                Declaration:
                        double cosh(double x);
                                                            Returns the hyperbolic cosine of x.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         sin
                                                Declaration:
                        double sin(double x);
                                                            Returns the sine of a radian angle x.
                                                Range:
                                                            The value of x has no range. The returned value is in the range of -1 to +1                                                                         (inclusive).
·         sinh
                                                Declaration:
                                                            double sinh(double x);
                                                            Returns the hyperbolic sine of x.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         tan
                                                Declaration:
                        double tan(double x);
                                                            Returns the tangent of a radian angle x.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         tanh
                                                Declaration:
                        double tanh(double x);
                                                            Returns the hyperbolic tangent of x.
                                                Range:
                                                            The value of x has no range. The returned value is in the range of -1 to +1                                                                         (inclusive).
·         exp
                                                 Declaration:
                        double exp(double x);
                                                            Returns the value of e raised to the xth power.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         frexp
                                                Declaration:
                                                            double frexp(double x, int *exponent);
                                                            The floating-point number x is broken up into a mantissa and exponent.
                                                             The returned value is the mantissa and the integer pointed to by                                                                           exponent is the exponent. The resultant value is x=mantissa *                                                                                2^exponent.
                                                Range:
                                                            The mantissa is in the range of .5 (inclusive) to 1 (exclusive).
·         ldexp
                                                Declaration:
                        double ldexp(double x, int exponent);
                                                            Returns x multiplied by 2 raised to the power of exponent.
                                                            x*2^exponent
                                                 Range:
                                                            There is no range limit on the argument or return value.
·         log
                                                Declaration:
                        double log(double x);
                                                            Returns the natural logarithm (base-e logarithm) of x.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         log10
                                                Declaration:
                        double log10(double x);
                                                            Returns the common logarithm (base-10 logarithm) of x.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         modf
                                                Declaration:
                        double modf(double x, double *integer);
                                                            Breaks the floating-point number x into integer and fraction components.
                                                            The returned value is the fraction component (part after the decimal),                                                                  and sets integer to the integer component.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         pow
                                                Declaration:
                        double pow(double x, double y);
                                                            Returns x raised to the power of y.
                                                Range:
                                                            x cannot be negative if y is a fractional value. x cannot be zero if y is less                                                             than or equal to zero.
·         sqrt
                                                Declaration:
                        double sqrt(double x);
                                                            Returns the square root of x.
                                                Range:
                                                            The argument cannot be negative. The returned value is always positive.
·         ceil
                                                Declaration:
                        double ceil(double x);
                                                            Returns the smallest integer value greater than or equal to x.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         fabs
                                                Declaration:
                        double fabs(double x);
                                                            Returns the absolute value of x (a negative value becomes positive,                                                                       positive value is unchanged).
                                                Range:
                                                            There is no range limit on the argument. The return value is always                                                                      positive.
·         floor
                                                Declaration:
                        double floor(double x);
                                                            Returns the largest integer value less than or equal to x.
                                                Range:
                                                            There is no range limit on the argument or return value.
·         fmod
                                                Declaration:
                        double fmod(double x, double y);
                                                            Returns the remainder of x divided by y.
                                                Range:
                                                            There is no range limit on the return value. If y is zero, then either a range     \error will occur or the function will return zero (implementation-defined).

Workshop

          This Workshop provides quiz questions to help you solidify your understanding of the material covered, and exercises to provide you with experience in using what you've learned.
1)     Why do nearly all of the math functions return doubles?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
2)     Does strcat() ignore trailing spaces when doing a concatenation?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
3)     Can I convert numbers to strings?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
4)     What is the difference between strcmp() and strncmp()?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Part C- Program Development
MATH CONVERSION PROGRAM (10 points)
                Create a program a Math Conversion Program with Log-in Validation. 1) The program must ask the user to enter the username and password (Batch1-Case Sensitive and Batch2-Not Case Sensitive), the default username is "admin" and the default password is "admin1234". If login successful, the program will then ask for the user to enter a positive integer value and the program will return the following:
                * 2) The largest integer value less than or equal to the entered number;
                * 3) The absolute value of the entered number (a negative value becomes positive, positive                                                      value is unchanged);
                 4) and The square root of the entered number.
                Note: 5)  The program must validate if the entered number is Positive and should prompt the user if      the entered number is negative.
                6)  If login is not successful, the program should prompt the user and must return the user to the login module.


Sample Output:
                Enter username: admin
                Enter password: admin1234
                Successfully login!
                Enter a positive number: 5
                The largest integer value less than or equal to the   entered number is 5.0000


                The absolute value of the entered number is 5.0000  The square root of the entered number is 2.236068
Scoring Rules:
                10 pts If the problem is solved within 60 minutes. 
                7 pts If the problem is solved within 80 minutes.
                6 pts If the problem is solved within 120 minutes.
                5 pts If the problem is solved past 121 minutes.
                4 pts If the problem is solved past 121 minutes but with     runtime errors or bugs.
                3 pts for running program with incorrect output.
                0 zero for non running program.

Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.



Title: C's STRING AND MATHEMATICAL FUNCTIONS
Ø  Download the .Docx Format of this article. ClickHERE
Ø  Download the .Pdf Format of this article. ClickHERE

1 comment:

  1. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more. 稼げる

    ReplyDelete