CHAPTER 2 Strings Number and DateTime
CHAPTER 2 Strings Number and DateTime
CHAPTER 2 Strings Number and DateTime
ARRAY
CHAPTER II
STRINGS, NUMBER AND
DATE/TIME
Learning Content:
Number
o Math Operations in C++
o Random Numbers in C++
String
o C-Style Character String
o Functions that manipulate null-terminated strings
Date/Time
o C++ standard library for date and time
o four time-related types: clock_t, time_t, size_t, and tm
o Date Time Functions
o Format Time using struct tm
1
CHAPTER I. ARRAY
Let us determine how much you already know about the INTRODUCTION TO DISCRETE
STRUTURES
Identification:
Enumeration:
1. What are the The traditional “GUESS” method (Heller and Heller, Univ. of Minnesota)
_______________
_______________
_______________
_______________
_______________
2. What are the Framework for Problem Solving (Polya)
_______________
_______________
_______________
_______________
2
CHAPTER I. ARRAY
INFORMATION SHEET
STRINGS, NUMBER , DATE/TIME
A. STRINGS
The following declaration and initialization create a string consisting of the word "Hello". To hold
the null character at the end of the array, the size of the character array containing the string is
one more than the number of characters in the word "Hello."
If you follow the rule of array initialization, then you can write the above statement as follows −
char greeting[] = "Hello";
Following is the memory presentation of above defined string in C/C++ −
Actually, you do not place the null character at the end of a string constant. The C++ compiler
automatically places the '\0' at the end of the string when it initializes the array. Let us try to print
above-mentioned string −
3
CHAPTER I. ARRAY
#include <iostream>
int main () {
return 0;
}
4
CHAPTER I. ARRAY
#include <iostream>
#include <cstring>
int main () {
return 0;
}
5
CHAPTER I. ARRAY
int main () {
return 0;
}
String Length
To get the length of a string, use the length() function:
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
Tip: You might see some C++ programs that use the size() function to get the length of a string.
This is just an alias of length(). It is completely up to you if you want to use length() or size():
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.size();
6
CHAPTER I. ARRAY
B. NUMBER
Normally, when we work with Numbers, we use primitive data types such as int, short, long, float
and double, etc. The number data types, their possible values and number ranges have been
explained while discussing C++ Data Types.
int main () {
// number definition:
short s;
int i;
long l;
float f;
double d;
// number assignments;
s = 10;
i = 1000;
l = 1000000;
f = 230.47;
d = 30949.374;
// number printing;
cout << "short s :" << s << endl;
cout << "int i :" << i << endl;
cout << "long l :" << l << endl;
cout << "float f :" << f << endl;
cout << "double d :" << d << endl;
return 0;
}
7
CHAPTER I. ARRAY
C++ has a rich set of mathematical operations, which can be performed on various numbers.
Following table lists down some useful built-in mathematical functions available in C++.
To utilize these functions you need to include the math header file <cmath>.
Sr.N
o Function Purpose
1 double cos(double); This function takes an angle (as a double) and returns the cosine.
2 double sin(double); This function takes an angle (as a double) and returns the sine.
3 double tan(double); This function takes an angle (as a double) and returns the tangent.
This function takes a number and returns the natural log of that
4 double log(double); number.
double pow(double, The first is a number you wish to raise and the second is the power
5 double); you wish to raise it t
double hypot(double, If you pass this function the length of two sides of a right triangle, it
6 double); will return you the length of the hypotenuse.
7 double sqrt(double); You pass this function a number and it gives you the square root.
This function returns the absolute value of an integer that is passed
8 int abs(int); to it.
This function returns the absolute value of any decimal number
9 double fabs(double); passed to it.
Finds the integer which is less than or equal to the argument passed
10 double floor(double); to it.
#include <iostream>
#include <cmath>
using namespace std;
int main () {
// number definition:
short s = 10;
int i = -1000;
long l = 100000;
float f = 230.47;
double d = 200.374;
// mathematical operations;
cout << "sin(d) :" << sin(d) << endl;
cout << "abs(i) :" << abs(i) << endl;
cout << "floor(d) :" << floor(d) << endl;
cout << "sqrt(f) :" << sqrt(f) << endl;
cout << "pow( d, 2) :" << pow(d, 2) << endl;
return 0;
}
8
CHAPTER I. ARRAY
Following is a simple example to generate few random numbers. This example makes use of
time() function to get the number of seconds on your system time, to randomly seed the rand()
function –
#include <iostream>
#include <ctime>
#include <cstdlib>
int main () {
int i,j;
return 0;
}
9
CHAPTER I. ARRAY
C. DATE/TIME
The C++ standard library does not provide a proper date type. C++ inherits the structs and
functions for date and time manipulation from C. To access date and time related functions and
structures, you would need to include <ctime> header file in your C++ program.
There are four time-related types: clock_t, time_t, size_t, and tm. The types - clock_t, size_t and
time_t are capable of representing the system time and date as some sort of integer.
The structure type tm holds the date and time in the form of a C structure having the following
elements –
struct tm {
int tm_sec; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
}
Following are the important functions, which we use while working with date and time in C or C+
+. All these functions are part of standard C and C++ library and you can check their detail using
reference to C++ standard library given below.
10
CHAPTER I. ARRAY
#include <iostream>
#include <ctime>
int main() {
// current date/time based on current system
time_t now = time(0);
cout << "The local date and time is: " << dt << endl;
11
CHAPTER I. ARRAY
While using structure in this chapter, I'm making an assumption that you have basic
understanding on C structure and how to access structure members using arrow -> operator.
#include <iostream>
#include <ctime>
int main() {
// current date/time based on current system
time_t now = time(0);
cout << "Number of sec since January 1,1970 is:: " << now << endl;
tm *ltm = localtime(&now);
12
CHAPTER I. ARRAY
Let us determine how much you already know about the INTRODUCTION TO DISCRETE
STRUTURES
Identification:
Enumeration:
2. What are the The traditional “GUESS” method (Heller and Heller, Univ. of Minnesota)
_______________
_______________
_______________
_______________
_______________
2. What are the Framework for Problem Solving (Polya)
_______________
_______________
_______________
_______________
Answer Key
IDENTIFICATION:
1. Discrete Mathematics It is a branch of mathematics involving discrete elements that uses
algebra and arithmetic.
2. Continuous Mathematics It is based upon continuous number line or the real numbers.
13
CHAPTER I. ARRAY
3. Problem solving is an art and there are no universal approaches to solving problems
ENUMERATION:
1. What are the The traditional “GUESS” method (Heller and Heller, Univ. of Minnesota)
a. Givens - Identify the “given” information
b. Unknown - Identify what is wanted
c. Equation - Select equation for solution
d. Solution - Solve equation for unknown
e. Survey - Make certain solution is realistic
2. What are the Framework for Problem Solving (Polya)
a. Understanding the problem
b. Devising a Solution Plan
c. Carrying out the plan
d. Looking back i.e. verifying
14