0% found this document useful (0 votes)
118 views

CS213 1x Session1 Basic Built in Data Types

The document discusses a basic C++ program that reads two integers, adds them together, and outputs the sum. It explains that the program uses built-in integer data types to store the two numbers, and the addition operation is an algorithm that is part of the standard library. The document also provides questions about the guarantees of the program and alternatives for representing the numbers.

Uploaded by

carlosnyny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

CS213 1x Session1 Basic Built in Data Types

The document discusses a basic C++ program that reads two integers, adds them together, and outputs the sum. It explains that the program uses built-in integer data types to store the two numbers, and the addition operation is an algorithm that is part of the standard library. The document also provides questions about the guarantees of the program and alternatives for representing the numbers.

Uploaded by

carlosnyny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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

You might also like