Week2 and Week3
Week2 and Week3
• Programming your ”friend”.
• Programming the ”turtle”. Week 1
• Interact with turtle at run time !
From Turtle to C
• Programming your ”friend”.
• Programming the ”turtle”. Week 1
• Interact with turtle at run time !
• First C program.
• Compilation and Execution.
• The computing machine.
• Variables and memory. This week
• Input and output in C.
• Assignment and other operators.
Lab1 and Survey : Some stats
• Q1: Receive input and turtle draws a
polygon with that many sides
• Q2: Turtle draws a crude pic of a car.
• Q3: Textonly Turtle prints some text.
• (Optional) Q4: Turtle draws a
sophisticated car.
Lab1 and Survey : Some stats
• Q1: Receive input and turtle draws a • # attended: 86.
polygon with that many sides
• # solved Q1: 85.
• Q2: Turtle draws a crude pic of a car.
• # solved Q2: 85.
• Q3: Textonly Turtle prints some text.
• # solved Q3 : 85.
• (Optional) Q4: Turtle draws a
• # solved Q3 : 74.
sophisticated car.
Lab1 and Survey : Some stats
• Q1: Receive input and turtle draws a • # attended: 86.
polygon with that many sides
• # solved Q1: 85.
• Q2: Turtle draws a crude pic of a car.
• # solved Q2: 85.
• Q3: Textonly Turtle prints some text.
• # solved Q3 : 85.
• (Optional) Q4: Turtle draws a
• # solved Q3 : 74.
sophisticated car.
End Goals:
• First C program.
• Edit, Compile, Execute – what is that?
• Structure of a C program.
• The printf command.
• syntax and semantics of scanf command.
Goals for Today & Tomorrow
End Goals:
• First C program.
• Edit, Compile, Execute – what is that?
• Structure of a C program.
• The printf command.
• syntax and semantics of scanf command.
Detour:
• Computing on Computers.
• A bird’s eye-view of the computing machine.
• How are numbers represented?
What do we want our C program to achieve?
What do we want our C program to achieve?
********
********
********
********
********
********
********
********
********
********
********
********
********
********
********
********
#include<stdio.h>
int main() {
printf("********\n");
printf("********\n");
printf("********\n");
printf("********\n");
return 0;
}
My First C program
#include<stdio.h>
int main() {
printf("********\n");
printf("********\n");
printf("********\n");
printf("********\n");
return 0;
}
#include<stdio.h>
int main() {
printf("********\n");
printf("********\n");
printf("********\n");
printf("********\n");
return 0;
}
• gcc first.c
Compilation process:
• gcc first.c
• Executable code is created in a file called a.out
Compilation process:
• gcc first.c
• Executable code is created in a file called a.out
• Execute the executable code by typing ./a.out
Things to remember while writing a program
• Counting frame.
• One of the earliest form
of calculator.
• Still used by kids to do
fast simple arithmetic.
From Abacus to Apple
• Counting frame.
• One of the earliest form
of calculator.
• Still used by kids to do
fast simple arithmetic.
• Father of Theoretical
Computer Science (TCS)
and Artificial Intelligence
(AI).
• Turing machine – a
model for a general
purpose computer.
• Turing test – how
intelligent is a machine?
First Electronic Computer : ENIAC 1946
• 50,000 vacuum
tubes, diodes,
relays, resistors,
capacitors.
• 5 million
hand-soldered
joints.
• Weighed 27 tons.
• Covered 167m2
area.
• Consumed 150
Electronic Numerical Integerator kW of power.
And Calculator.
1946 – 1976
Integrated Circuits
Transistors
1946 – 1976
Integrated Circuits
Transistors
Apple Macintosh
Today’s World : Core i7 Processor
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
• Hexadecimal(base 16 – uses A-F for 10-15)
Eg : 0, 1, ..., 9, A, B, C, D, E, F, 10, 11, ... 19, 1A, 1B, ...
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
• Hexadecimal(base 16 – uses A-F for 10-15)
Eg : 0, 1, ..., 9, A, B, C, D, E, F, 10, 11, ... 19, 1A, 1B, ...
Goals for this week:
End Goals:
• First C program.
• Edit, Compile, Execute – what is that?
• Structure of a C program.
• The printf command.
• The scanf command.
• Operators in C .
• Assignment statement.
Goals for this week:
End Goals:
• First C program.
• Edit, Compile, Execute – what is that?
• Structure of a C program.
• The printf command.
• The scanf command.
• Operators in C .
• Assignment statement.
Detour:
• History - from Abacus to Apple
• A bird’s eye-view of the computing machine.
• How are numbers represented?
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
• Hexadecimal(base 16 – uses A-F for 10-15)
Eg : 0, 1, ..., 9, A, B, C, D, E, F, 10, 11, ... 19, 1A, 1B, ...
But, how does number get stored?
Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
• Hexadecimal(base 16 – uses A-F for 10-15)
Eg : 0, 1, ..., 9, A, B, C, D, E, F, 10, 11, ... 19, 1A, 1B, ...
Quick Primer on Number System : Base n
Sign Magn.
0 0 0 0
0 0 1 +1
0 1 0 +2
0 1 1 +3
1 0 0 0
1 0 1 -1
1 1 0 -2
1 1 1 -3
Representing negative numbers
Sign Magn.
0 0 0 0
0 0 1 +1
0 1 0 +2
0 1 1 +3
1 0 0 0
1 0 1 -1
1 1 0 -2
1 1 1 -3
• 2 + (−3)
Representing negative numbers
• 2 + (−3)
• 3 + (−2)
More examples : The case of 4 bits
More examples : The case of 4 bits
More examples : The case of 4 bits
−2147483648 to 2147483647
Take Aways so far ...
#include <stdio.h>
/* My first C program */
main() {
printf("Hello World!\n");
}
Recall our First C program
#include <stdio.h>
/* My first C program */
main() {
printf("Hello World!\n");
}
#include <stdio.h>
/* My first C program */
main() {
printf("Hello World!\n");
}
#include <stdio.h>
/* sum 2 integers */
main() {
int x;
int y;
printf("%d\n", x+y);
}
Sum of 2 numbers
main() {
int x;
int y;
printf("%d\n", x+y);
}
Sum of 2 numbers
printf("%d\n", x+y);
}
Sum of 2 numbers – with initialization
printf("%d\n", x+y);
}
Can we handle ”characters” instead of ”integers”
#include <stdio.h>
/* sum 2 integers */
main() {
int x;
int y;
printf("Enter x y: ");
scanf("%d %d",&x,&y);
Yet Another Question : Print the complement of the last bit of the
binary equivalent representation of the decimal number n :
Quick Problem to Solve :
Yet Another Question : Print the complement of the last bit of the
binary equivalent representation of the decimal number n :
(n+1)%2 or ((n%2)+1)%2
Comment about ranges of values: