IIT Bombay
Data Structures and Algorithms
Prof. Ajit A. Diwan
Prof. Ganesh Ramakrishnan
Prof. Deepak B. Phatak
Department of Computer Science and Engineering
IIT Bombay
Session: Basic Built-in Data Types
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1
Programs IIT Bombay
DATA STRUCTURES + ALGORITHMS = PROGRAMS
• A program prescribes ‘what’ is to be done with ‘which’ data
• A C++ program has Variables and Functions
• Variables ↔ Data Structures
• Functions ↔ Algorithms
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2
Example Program IIT Bombay
• Read two integers and output their sum
#include <iostream>
using namespace std;
int main()
{
int i, j;
cin >> i >> j;
cout << i+j << endl;
}
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3
Example Program IIT Bombay
• This program uses two variables and one function
• Variables are declared to be integers
• They can store integer values
• The only function reads two integers and outputs their sum
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4
Data Structure IIT Bombay
• Variables i and j are used to represent integers
• They form the data structure
• This is a built-in data structure int available as part of the
C++ programming language
• A data structure is also called a data type in a programming
language
• The data type of a variable defines its possible values and
the operations that can be performed on it
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5
Algorithm IIT Bombay
• The main function defines the algorithm
• This uses the built-in operation + for adding int variables
• It also uses functions defined in the iostream library for
reading and printing int variables
• Many commonly required data structures and algorithms
are available as built-in types or as part of libraries
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6
Questions IIT Bombay
• Will this program correctly add two integers always?
• If not, under what conditions is it guaranteed to work correctly?
• What are other possible built-in data structures that could
have been used in this program?
• Write a program for adding two integers that is guaranteed
to always work correctly.
• In this case, you may need to use your own data structures rather
than built-in ones.
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 7