0% found this document useful (0 votes)
59 views67 pages

CS1100 - Introduction To Programming

The document outlines the topics covered in the CS1100 course Introduction to Programming over 6 weeks. Week 1-4 covers programming concepts from Turtle to C language, data types, input/output, program execution and control flow using if-then-else and switch statements. Week 5-6 discusses loop constructs like while, for, do-while in C and example problems. Programming for engineers is also covered.

Uploaded by

Sarath nair
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)
59 views67 pages

CS1100 - Introduction To Programming

The document outlines the topics covered in the CS1100 course Introduction to Programming over 6 weeks. Week 1-4 covers programming concepts from Turtle to C language, data types, input/output, program execution and control flow using if-then-else and switch statements. Week 5-6 discusses loop constructs like while, for, do-while in C and example problems. Programming for engineers is also covered.

Uploaded by

Sarath nair
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/ 67

CS1100 – Introduction to Programming




• Programming : From Turtle to C. 





• Data Types in C, Representations, Operators. 



• Formatting the Input and the Output.
Week 1-4
• Execution of Programs, Compilers. 



• Modifying the control flow in Programs 



if-then-else, switch. 



CS1100 – Introduction to Programming



• Programming : From Turtle to C. 





• Data Types in C, Representations, Operators. 



• Formatting the Input and the Output.
Week 1-4
• Execution of Programs, Compilers. 



• Modifying the control flow in Programs 



if-then-else, switch. 








• while, for, do while constructs in C. 


• Example problems. Week 5-6


• Programming for engineers. 




Multiple Selection : switch construct

• Syntax:

switch (expression) {
case const-expr-1: statements-1;
case const-expr-2: statements-2;
..
.
default: statements-def;
}

• Semantics
• Evaluate expression.
• If it matches the const-expr-i, execute statements-i and
following as long as an explicit break is not encountered.
• If none of the const-expr-i match, execute the statements-def.
Even or odd – using switch

Given a number determine if it is even or odd.


#include<stdio.h>
int main() {

int input;

printf("Enter an integer: \t");


scanf("%d", &input);

switch (input % 2) {
case 0: printf("Number is even\n");
case 1: printf("Number is odd\n");
default: printf("Neither even nor odd\n");
}
return 0;
}
Even or odd – using switch

Given a number determine if it is even or odd.


#include<stdio.h>
int main() { • What is the output
of the program on
int input; 10, 5, 13?
printf("Enter an integer: \t"); • Observe the
scanf("%d", &input); missing break.
switch (input % 2) {
case 0: printf("Number is even\n");
case 1: printf("Number is odd\n");
default: printf("Neither even nor odd\n");
}
return 0;
}
Even or odd – using switch – a correct program

Given a number determine if it is even or odd.


#include<stdio.h>
int main() {

int input;

printf("Enter an integer: \t");


scanf("%d", &input);

switch (input % 2) {
case 0: printf("Number is even\n"); break;
case 1: printf("Number is odd\n"); break;
default: printf("Neither even nor odd\n");
}
return 0;
}
The while construct
• Syntax
while (expression) {
statements;
}
The while construct
• Syntax
while (expression) {
statements;
}
• Semantics
1. As long as expression is
true, execute statements.
2. If expression is false, exit
the loop.
The while construct
• Syntax
while (expression) {
statements;
}
• Semantics
1. As long as expression is
true, execute statements.
2. If expression is false, exit
the loop.
• Value of expression must be
changed by the body of the loop,
otherwise we have an infinite
loop.
The while construct
• Syntax
while (expression) {
statements;
}
• Semantics
1. As long as expression is
true, execute statements.
2. If expression is false, exit
the loop.
• Value of expression must be
changed by the body of the loop,
otherwise we have an infinite
loop.
• expression can contain relational,
logical or equality operators.
Relational <= < > >=
Equality == !=
Logical && ||
Reversing the digits of a given unsigned integer

#include "stdio.h"
int main () {
int number, revNumber, remainder;
revNumber = 0;
printf ("Input number:");
scanf ("%d", &number);
while (number > 0) {
remainder = number % 10;
revNumber = revNumber*10 + remainder;
number = number/10;
}
printf ("The reversed number is : %d\n", revNumber);
}
Example: Sum even and odd numbers

Accept integers from the standard input as long as the user does
not enter -1. Once the user enters -1, print the sum of all integers
entered so far, sum of even integers and sum of odd integers.

Two useful constructs:


• while loop repetitive statement
• switch multiple selection
Summing up odd and even numbers

Is the program correct?

#include<stdio.h>
int main() {
int input;
int sum, eSum, oSum;
printf("Enter an integer: \t");
scanf(" %d", &input);

while (input != -1) {


sum += input;
switch (input % 2) {
case 0: eSum += input; break;
case 1: oSum += input;
}
}
printf("sum = %d, oddSum = %d, evenSum = %d\n", sum, oSum, eSum);
return 0;
}
Summing up odd and even numbers

Is the program correct?

#include<stdio.h>
• common
int main() {
int input; mistake:
int sum, eSum, oSum; forgotten
printf("Enter an integer: \t"); initialization.
scanf(" %d", &input);
• expr. not
while (input != -1) { modified in body
sum += input; of loop.
switch (input % 2) {
case 0: eSum += input; break;
case 1: oSum += input;
}
}
printf("sum = %d, oddSum = %d, evenSum = %d\n", sum, oSum, eSum);
return 0;
}
Summing up odd and even numbers
#include<stdio.h>
int main() {
int input;
int sum, eSum, oSum;
printf("Enter an integer: \t");
scanf(" %d", &input);
sum = eSum = oSum = 0; // initialization.

while (input != -1) {


sum += input;
switch (input % 2) {
case 0: eSum += input; break;
case 1: oSum += input;
}
printf("Enter an integer: \t");
scanf(" %d", &input);
}
printf("sum = %d, oddSum = %d, evenSum = %d\n", sum, oSum, eSum);
return 0;
}
for construct

• Syntax
for (stmt1; expr; stmt2) {
statements;
}
for construct

• Syntax
for (stmt1; expr; stmt2) {
statements;
}

• Semantics
1. Execute stmt1.
2. If expr is true, execute
statements. execute
stmt2. goto step 2.
3. If expr is false, exit loop.

• expr must be changed to


ensure that it is not an
infinite loop.
Sum of 100 integers – using for loop

User enters 100 integers. Sum them and print the sum.
Sum of 100 integers – using for loop

User enters 100 integers. Sum them and print the sum.
#include<stdio.h>
int main() {
int input;
int count;
int sum = 0;

for (count=1; count <= 100; count++) {


printf("Enter an integer: \t");
scanf(" %d", &input);
sum += input;
}
printf("sum of numbers entered is %d\n", sum);
return 0;
}

Typically, stmt1 : initialization and stmt2 : increment.


Uncommon ways of using for
Predict the output of this program :
#include<stdio.h>
int main() {
int n;

printf("Enter an integer: ");


scanf("%d", &n);

int d, ten_power_d;
for (d=1, ten_power_d = 10; ten_power_d <= n; d++, ten_power_d *= 10)
{
}
printf("%d", d);
return 0;
}
Uncommon ways of using for
Predict the output of this program :
#include<stdio.h>
int main() {
int n;

printf("Enter an integer: ");


scanf("%d", &n);

int d, ten_power_d;
for (d=1, ten_power_d = 10; ten_power_d <= n; d++, ten_power_d *= 10)
{
}
printf("%d", d);
return 0;
}
• Prints the number of digits in the given number.
• The above method is considered to be a bad programming
practice.
do while construct

• Syntax
do
{ statements; }
while (expr);
do while construct

• Syntax
do
{ statements; }
while (expr);

• Semantics
1. Execute statement.
2. If expr is true, goto step
1, else exit loop.

• expr must be changed by


statement.
Sum of 100 integers – using do while loop
User enters 100 integers. Sum them and print the sum.

#include<stdio.h>
int main() {
int input;

int count = 1;
int sum = 0;

do {
printf("Enter an integer: \t");
scanf(" %d", &input);
count++;
sum += input;
} while (____);

printf("sum of numbers entered is %d\n", sum);


return 0;
}
Sum of 100 integers – using do while loop
User enters 100 integers. Sum them and print the sum.

#include<stdio.h>
int main() {
int input;

int count = 1;
int sum = 0;

do {
printf("Enter an integer: \t");
scanf(" %d", &input);
count++;
sum += input;
} while (count <= 100);

printf("sum of numbers entered is %d\n", sum);


return 0;
}
Sum of 100 integers – using do while loop
User enters 100 integers. Sum them and print the sum.

#include<stdio.h>
int main() {
int input;

int count = 1;
int sum = 0;

do {
printf("Enter an integer: \t");
scanf(" %d", &input);
count++;
sum += input;
} while (count <= 100);

printf("sum of numbers entered is %d\n", sum);


return 0;
}
Sum of 100 integers – using do while loop
User enters either 100 integers or terminates the program by
entering -1. Sum the integers input and print the sum.
Sum of 100 integers – using do while loop
User enters either 100 integers or terminates the program by
entering -1. Sum the integers input and print the sum.

#include<stdio.h>
main() {
int int input;

int count = 1;
int sum = 0;

do {
printf("Enter an integer: \t");
scanf(" %d", &input);
count++;
if (input != -1) sum += input;
} while (count <= 100 && input != -1);

printf("sum of numbers entered is %d\n", sum);


return 0;
}
break statement in loops in C
break : break out of the loop execution completely.
break statement in loops in C
break : break out of the loop execution completely.
Example : using break
User enters either 100 integers or terminates the program by
entering -1. Sum the integers input and print the sum.
Example : using break
User enters either 100 integers or terminates the program by
entering -1. Sum the integers input and print the sum.
#include <stdio.h>
int main() {
int i;
int number, sum = 0;

for(i=1; i <= 100; i++)


{
printf("Enter n%d: ",i);
scanf("%d",&number);

/* If user enters negative number, loop is terminated */


if (number == -1) break;

sum = sum + number;


}
printf("Sum = %d",sum);
return 0;
}
continue statement in loops in C

continue : skip the rest of iteration and go to next iteration.


continue statement in loops in C

continue : skip the rest of iteration and go to next iteration.


Example : using continue
User enters either 100 integers some of which may be negative.
Sum the positive integers given and print the sum.
Example : using continue
User enters either 100 integers some of which may be negative.
Sum the positive integers given and print the sum.
#include <stdio.h>
int main() {
int i;
int number, sum = 0;

for(i=1; i <= 100; i++)


{
printf("Enter n%d: ",i);
scanf("%d",&number);

/* If user enters negative number, it is skipped from sum */


if (number < 0) continue;

sum = sum + number;


}
printf("Sum = %d",sum);
return 0;
}
Different Loop constructs - and related ...

• while, for, do while.


• Why have different constructs?
• Can you replace one by another?
Yes, with careful modifications
• Each of the construct is natural for different problems.
• break, continue to handle control flow within the runs of
the loop.
Computing 2n
Accept n from the user. Print 2n on the standard output.

#include<stdio.h>

int main() {
int n;
int value = 2;

printf("Enter a positive integer:\t");


scanf("%d", &n);

while (____) {
value *= 2;
______;
}
printf("%d\n", value);
return 0;
}
Computing 2n
Accept n from the user. Print 2n on the standard output.
Computing 2n
Accept n from the user. Print 2n on the standard output.

#include<stdio.h>

int main() {
int n;
int value = 2;

printf("Enter a positive integer:\t");


scanf("%d", &n);

while (n > 1) {
value *= 2;
n--;
}
printf("%d\n", value);
return 0;
}
Stepping back : Stages of Program Design ...

To solve a problem using a computer program :


Stepping back : Stages of Program Design ...

To solve a problem using a computer program :


• Need to develop an idea of the sequence of statements and
the flow of the control through the statements to achieve a
given task.
Stepping back : Stages of Program Design ...

To solve a problem using a computer program :


• Need to develop an idea of the sequence of statements and
the flow of the control through the statements to achieve a
given task.
• Algorithm : The description of the idea - a step-by-step
procedure to solve the problem.
Stepping back : Stages of Program Design ...

To solve a problem using a computer program :


• Need to develop an idea of the sequence of statements and
the flow of the control through the statements to achieve a
given task.
• Algorithm : The description of the idea - a step-by-step
procedure to solve the problem.
• Pseudo-code : A language-independent but program-like
description of an algorithm.
Stepping back : Stages of Program Design ...

To solve a problem using a computer program :


• Need to develop an idea of the sequence of statements and
the flow of the control through the statements to achieve a
given task.
• Algorithm : The description of the idea - a step-by-step
procedure to solve the problem.
• Pseudo-code : A language-independent but program-like
description of an algorithm.
• Flowchart : A diagramatic representation of the algorithm is
called a flowchart.
An Illustration : Collatz Sequence
An Illustration : Collatz Sequence

Algorithm : To get a Collatz sequence from a number, if it’s even, divide it by


two, and if it’s odd, multiply it by three and add one. Continue the operation
on the result of the previous operation until the number becomes 1.
An Illustration : Collatz Sequence

Algorithm : To get a Collatz sequence from a number, if it’s even, divide it by


two, and if it’s odd, multiply it by three and add one. Continue the operation
on the result of the previous operation until the number becomes 1.

Pseudo-code:
begin program
while n is more than 1
show n
if n is odd then
set n = 3n + 1
else
set n = n / 2
endif
endwhile
show n
end program
An Illustration : Collatz Sequence

Algorithm : To get a Collatz sequence from a number, if it’s even, divide it by


two, and if it’s odd, multiply it by three and add one. Continue the operation
on the result of the previous operation until the number becomes 1.

Pseudo-code:
begin program C-program segment:
while n is more than 1
while(n > 1) {
show n
printf("%d,",n);
if n is odd then
if (n%2 == 1)
set n = 3n + 1
n = 3*n+1;
else
else
set n = n / 2
n = n/2;
endif
}
endwhile
printf("%d.",n);
show n
end program
Programs : Termination and Correctness
∑n n(n−1)
Someone claims that For every n, k=1 k = 2 .
Programs : Termination and Correctness
∑n n(n−1)
Someone claims that For every n, k=1 k = 2 . You ask for
proof !
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs.
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
• ”Hello World” program is easy. Hand simulation works.
Infeasible when program receives inputs (many possibilities).
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
• ”Hello World” program is easy. Hand simulation works.
Infeasible when program receives inputs (many possibilities).
• An interesting complication : even for a given input, a loop
could run for infinite times (hence not hand-simulatable).
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
• ”Hello World” program is easy. Hand simulation works.
Infeasible when program receives inputs (many possibilities).
• An interesting complication : even for a given input, a loop
could run for infinite times (hence not hand-simulatable).
• There has to be mathematical statements which proves:
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
• ”Hello World” program is easy. Hand simulation works.
Infeasible when program receives inputs (many possibilities).
• An interesting complication : even for a given input, a loop
could run for infinite times (hence not hand-simulatable).
• There has to be mathematical statements which proves:
• Termination: The loop (hence, the program) terminates.
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
• ”Hello World” program is easy. Hand simulation works.
Infeasible when program receives inputs (many possibilities).
• An interesting complication : even for a given input, a loop
could run for infinite times (hence not hand-simulatable).
• There has to be mathematical statements which proves:
• Termination: The loop (hence, the program) terminates.
(Idea : Some measure decreases as loop executes every time,
and hence finally, it should terminate).
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
• ”Hello World” program is easy. Hand simulation works.
Infeasible when program receives inputs (many possibilities).
• An interesting complication : even for a given input, a loop
could run for infinite times (hence not hand-simulatable).
• There has to be mathematical statements which proves:
• Termination: The loop (hence, the program) terminates.
(Idea : Some measure decreases as loop executes every time,
and hence finally, it should terminate).
• Correctness: The program is correctly producting the
expected output.
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
• ”Hello World” program is easy. Hand simulation works.
Infeasible when program receives inputs (many possibilities).
• An interesting complication : even for a given input, a loop
could run for infinite times (hence not hand-simulatable).
• There has to be mathematical statements which proves:
• Termination: The loop (hence, the program) terminates.
(Idea : Some measure decreases as loop executes every time,
and hence finally, it should terminate).
• Correctness: The program is correctly producting the
expected output. (Idea : After every iteration of the loop, the
partial result that it computes implies the final result).
Programs : Termination and Correctness

Someone writes a program and claims that it solves the problem


correctly for all inputs. You ask for proof !
• ”Hello World” program is easy. Hand simulation works.
Infeasible when program receives inputs (many possibilities).
• An interesting complication : even for a given input, a loop
could run for infinite times (hence not hand-simulatable).
• There has to be mathematical statements which proves:
• Termination: The loop (hence, the program) terminates.
(Idea : Some measure decreases as loop executes every time,
and hence finally, it should terminate).
• Correctness: The program is correctly producting the
expected output. (Idea : After every iteration of the loop, the
partial result that it computes implies the final result).
• Algorithm designer’s job. Not so much programmer’s.
More Practice Problems

• Write a program to check if a given number n is prime or not.


More Practice Problems

• Write a program to check if a given number n is prime or not.


Algorithm: Check, for every number m in the range 2 to
n − 1, whether m divides n or not. If none divides, then you
can declare that it is a prime number. If one of them divides,
then you can declare right away that is is a composite number.
More Practice Problems

• Write a program to check if a given number n is prime or not.


Algorithm: Check, for every number m in the range 2 to
n − 1, whether m divides n or not. If none divides, then you
can declare that it is a prime number. If one of them divides,
then you can declare right away that is is a composite number.
A typical example where for construct is natural.
More Practice Problems

• Write a program to check if a given number n is prime or not.


Algorithm: Check, for every number m in the range 2 to
n − 1, whether m divides n or not. If none divides, then you
can declare that it is a prime number. If one of them divides,
then you can declare right away that is is a composite number.
A typical example where for construct is natural.
Notice the use case of break statement within the loop.
More Practice Problems

• Write a program to check if a given number n is prime or not.


Algorithm: Check, for every number m in the range 2 to
n − 1, whether m divides n or not. If none divides, then you
can declare that it is a prime number. If one of them divides,
then you can declare right away that is is a composite number.
A typical example where for construct is natural.
Notice the use case of break statement within the loop.
• Write a program to find the greatest common divisor (gcd) of
two given integers.
More Practice Problems

• Write a program to check if a given number n is prime or not.


Algorithm: Check, for every number m in the range 2 to
n − 1, whether m divides n or not. If none divides, then you
can declare that it is a prime number. If one of them divides,
then you can declare right away that is is a composite number.
A typical example where for construct is natural.
Notice the use case of break statement within the loop.
• Write a program to find the greatest common divisor (gcd) of
two given integers. (Hint : Use the fact that for every a, b,
if a%b = 0 then gcd(a, b) = b.
otherwise gcd(a, b) = gcd(b, a%b).
A typical example where while construct is natural.

You might also like