Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

C Programming Week4

The document provides an overview of perfect number detection, including pseudocode and examples of control structures like for and while loops. It also discusses methods for calculating square roots and structured programming principles. Additionally, it includes exercises for programming tasks related to matrices.

Uploaded by

ABHISHEK GOUTAM
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)
3 views

C Programming Week4

The document provides an overview of perfect number detection, including pseudocode and examples of control structures like for and while loops. It also discusses methods for calculating square roots and structured programming principles. Additionally, it includes exercises for programming tasks related to matrices.

Uploaded by

ABHISHEK GOUTAM
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/ 4

15/09/17

Perfect Number Detection


• Perfect number – sum of proper divisors adds up to
the number
• Pseudocode:
CS1100 – Read a number, A
Introduction to Programming – Set the sum of divisors to 1
– If A is divisible by 2, Add 2 to the sum of divisors
Control Structures
– If A is divisible by 3, Add 3 to the sum of divisors
Madhu Mutyam …
Department of Computer Science and Engineering
Indian Institute of Technology Madras – If A is divisible by A/2, Add A/2 to the sum of divisors
– If A is equal to the sum of divisors, A is a perfect
Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1 number
SD, PSK, NSN, DK, TAG – CS&E, IIT M 2

Refining the Pseudocode Perfect Number Detection


• Read a number, A main ( ){
• Set the sum of divisors to 1 int d=2, n, sum=1;
d<n will also do, but would
• Set B to 2 scanf (“%d”, &n); do unnecessary work
while (d <= (n/2)) {
• While B is less than or equal to A/2
if (n%d == 0)
– If A is divisible by B, Add B to the sum of divisors
sum += d;
– Increment B by 1
d++;
• If A is equal to the sum of divisors, A is a perfect
}
number
if (sum == n) printf (“%d is perfect\n”, n);
else printf (“%d is not perfect\n”, n);
Exercise: Modify to find4
SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 }
SD, PSK, NSN, DK, TAG – CS&E, IIT M
the first n perfect numbers

for loops The for construct


• Counter controlled repetitions needs • General form:
– Initial value for the counter for (expr1; expr2; expr3) <statement>
– Modification of counter: i = i+1 or i = i–1, or any • Semantics:
other arithmetic expression based on the problem, and
– evaluate “expr1” - initialization operation(s)
– Final value for the counter
– repeat - evaluate expression “expr2” and
• for repetition structure provides for the – If “expr2” is true
programmer to specify all these • execute “statement” and “expr3”
• Any statement written using for can be rewritten – Else stop and exit the loop
using while
• Use of for helps make the program error free
SD, PSK, NSN, DK, TAG – CS&E, IIT M 5 SD, PSK, NSN, DK, TAG – CS&E, IIT M 6

1
15/09/17

Example Code with the while Construct Example Code with the for Construct
scanf(“%d”, &n); scanf(“%d”, &n);
value = 1; value = 1;
printf (“current value is %d \n”, value); for (count = 0; count <= n; count = count+1){
counter = 0; if (count == 0) printf(“value is %d \n”,1);
while (counter <= n){ else{
value = 2 * value; value = 2 * value;
printf (“current value is %d \n”, value); printf(value is %d \n”, value);
counter = counter + 1; }
} }
SD, PSK, NSN, DK, TAG – CS&E, IIT M 7
• PSK,
SD, Observe:
NSN, DK, TAG – a mistake
CS&E, IIT M in the earlier program is gone8

Computing the Sum of the First 20 Odd Numbers Calculating Compound Interest a = p(1 + r)n
#include<stdio.h>
int i, j, sum; #include<math.h>
Set j to the first odd number
sum = 0; main( ){ String constants used to align
i : Loop control variable
for (j = 1, i = 1; i <= 20; i = i+1){ int yr; heading and output data in a table

Termination condition
sum += j; double amt, principal = 1000.0, rate = .05;
Increment sum by the ith odd number
j += 2; Set j to the next odd number
printf(“%4s%10s\n”, “year”, “Amount”);
} for (yr = 1; yr < = 10; yr++) {
amt = principal * pow(1.0 + rate, yr);
printf(“%4d%10.2f\n”, yr, amt);
SD, PSK, NSN, DK, TAG – CS&E, IIT M 9
} NSN, DK, TAG – CS&E, IIT M
SD, PSK, 10

The do-while construct An Example


• for and while check termination condition before #include<stdio.h>
each iteration of the loop body int main( )
• Sometimes - execute the statement and check for {
condition
int count = 1;
• General form:
do{
do {<statement>} while (expr);
printf(“%d\n”, count);
• Semantics:
}while(++count <= 10);
– execute the statement and check expr
return 0;
– if expr is true, re-execute statement else exit
}
SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12

2
15/09/17

Find the Square Root of a Number Newton–Raphson Method


• How do we find the square root of a given f(x) = x2 − N f(x)
number N?
• We need to find the positive root of the
polynomial x2 – N f ' : the derivative of the function f
• Solve: x2 – N = 0 By simple algebra we can derive

xn+1 = xn – ( xn2 – N )/2xn


√N
= (xn2 + N)/2xn = (xn + N/xn )/2

SD, PSK, NSN, DK, TAG – CS&E, IIT M 13 SD, PSK, NSN, DK, TAG – CS&E, IIT M 14
http://en.wikipedia.org/wiki/Newton's_method

Square Root of a Number Repetition Structures


int n;
float prevGuess, currGuess, error, sqRoot; body
true
scanf(“%d”, &n); expr body

currGuess = (float) n/2 ; error = 0.0001; true


false expr
do{ while Structure do-while
false
Structure
prevGuess = currGuess;
currGuess = (prevGuess + n/prevGuess)/2;
init
}while(fabs(prevGuess – currGuess)>error);
Single Entry
sqRoot = currGuess; Single Exit
expr
true
body incr
printf(“%f\n”, sqRoot);
false for Structure
SD, PSK, NSN, DK, TAG – CS&E, IIT M 15 SD, PSK, NSN, DK, TAG – CS&E, IIT M 16

Structured Programming Break and Continue


• To produce programs that are • break – breaks out of the innermost loop or
– easier to develop, understand, test, modify switch statement in which it occurs
– easier to get correctness proof • continue – starts the next iteration of the loop in
• Rules which it occurs
1. Begin with the “simplest flowchart”
2. Any action box can be replaced by two action boxes in
sequence
3. Any action box can be replaced by any elementary structures
(sequence, if, if/else, switch, while, do-while or for)
4. Rules 2 and 3 can be applied as many times as required and in
any order

SD, PSK, NSN, DK, TAG – CS&E, IIT M 17 SD, PSK, NSN, DK, TAG – CS&E, IIT M 18

3
15/09/17

An Example Find a Smallest Positive Number


#include<stdio.h>
#include<stdio.h>
main ( ){
main ( ){
int n=0, smallNum = 10000;
int i;
printf(“Enter Numbers (in the range 0 to 9999):\n”);
for (i = 1; i < 10; i = i+1){ scanf(“%d”, &n);
if( i == 5) while (n >= 0){
break; //continue; if(smallNum > n) smallNum = n;
printf(“%4d”, i); scanf(“%d”,&n);
} }
} printf(“Smallest number is %d\n”,smallNum);
SD, PSK, NSN, DK, TAG – CS&E, IIT M 19 } PSK, NSN, DK, TAG – CS&E, IIT M
SD, 20

Exercises
• Write a program that reads in the entries of a 3x3
matrix, and prints it out in the form of a matrix.
The entries could be floating point too.
• Write a program that reads in orders of two
matrices and decides whether two such matrices
can be multiplied. Print out the decision.
• Write a program that reads in two matrices, and
multiplies them. Your output should be the two
matrices and the resulting product matrix.

SD, PSK, NSN, DK, TAG – CS&E, IIT M 21

You might also like