cs1010e programming methodology tutorial 3 control structures and data files c14,a15,d11,c08,c11,a02
TRANSCRIPT
CS1010E Programming MethodologyTutorial 3
Control Structures and Data Files
Question 1 (a) (b)
1. What is output ?
2. Correct the code to print [0, input)!
1. #include <stdio.h> 2. int main(void) { 3. int i, input; 4. scanf(“%d”, &input)5. while(i < input) 6. printf("%d\n", i); 7. i++; 8. return 0; 9. }
1. #include <stdio.h> 2. int main(void) { 3. int i = 0, input; 4. scanf(“%d”, &input);5. while(i < input) {6. printf("%d\n", i); 7. i++; 8. }9. return 0; 10.}
Question 1 (c)
Change to Do-while Loop
Which one is correct ?
1. #include <stdio.h> 2. int main(void) { 3. int i = 0, input; 4. scanf(“%d”, &input);5. while(i < input) {6. printf("%d\n", i); 7. i++; 8. }9. return 0; 10. }
int i = 0, input;scanf(“%d”, &input);do { printf("%d\n", i); i++;} while(i < input);
int i = 0, input;scanf(“%d”, &input);do { printf("%d\n", i);} while(i++ < input);
int i = 0, input;scanf(“%d”, &input);if ( i < input) {
do { printf("%d\n", i);
i++;} while(i < input);
}
√
Question 1 (d)
Rewrite using for-loop
1. #include <stdio.h> 2. int main(void) { 3. int i = 0, input; 4. scanf(“%d”, &input);5. while(i < input) {6. printf("%d\n", i); 7. i++; 8. }9. return 0; 10. }
for(i = 0;i < input; i++) { printf("%d\n", i);
}
Question 1 (d)
• Three loops are equivalent !!
• Why we have three loops instead of one ?!!• For convenience • While - used when you know stopping condition• For - used when you know the number of iterations
while(i < input){ printf("%d\n", i); i++; }
if ( i < input){ do{ printf("%d\n", i); i++; } while(i < input); }
for(i = 0;i < input; i++){ printf("%d\n", i);
}
Question 1 (e)
Print Out all the odd number from [0, input)• How to test parity of a number ?• num % 2 != 0
for(i = 0;i < input; i++) { printf("%d\n", i);
}
for(i = 0;i < input; i++){ if(i%2 != 0) {
printf("%d\n", i);
}}
Can you use num%2 == 1 for odd number test?
What is wrong here?
Question 1 (f)
Print Out all the odd number divisible by 5 from [0, input)• How to test such condition ?• num is a not multiple of 2 AND num is multiple of 5
for(i = 0;i < input; i++) { printf("%d\n", i);
}
for(i = 0;i < input; i++){ if(i%2 != 0 && i%5 == 0){
printf("%d\n", i); }
}
Note the difference between“&&” and “&”
Question 2
Iterate through a table (matrix)• Standard trick is to use a Nested-For-Loop
“i” indicate row number“j” indicate column number
i =1
j =1 j =3
i =2
j =2
for(i=1; i<=input; i++){ for(j=1; j<=input; j++){
do sth… }}
(1,1,)->(1,2)->(1,3)-> … -> (2,1) -> (2,2) ->(2,3)-> ….->….
Column First Order
Question 2 (a)
Write a program to take in an integer from 1 to 10 and print a multiplication table of that number.
• Analyze:
1.For every cell, cell value = row number * column number
for(i=1; i<=input; i++){ for(j=1; j<=input; j++){ printf("%4d", i * j); } printf(“\n”); }
for(i=1; i<=input; i++){ for(j=1; j<=input; j++){
do sth… }}
“i” indicate row number“j” indicate column number
End of each row, we need a newline
Question 2 (b)
Only the bottom triangle is obtained. • Analyze:
1.For every cell, cell value = row number * column number
2.Bottom triangle ≡ row number >= column number
for(i=1; i<=input; i++){ for(j=1; j<=i; j++){ printf("%4d", i * j); } printf(“\n”); }
for(i=1; i<=input; i++){ for(j=1; j<=input; j++){
do sth… }}
“i” indicate row number“j” indicate column number
Indicate row number >= column number
How to print out Top triangle ?How to print out Pascal's triangle ?
Question 2 (c)
Only the bottom triangle is obtained. Square Number -> ‘-’
Prime Number -> ‘p’• Analyze:
1.For every cell, cell value = row number * column number
2.Bottom triangle ≡ row number >= column number
3.We need to test the cell value:1.Square number
2.Prime number
for(i=1; i<=input; i++){ for(j=1; j<=i; j++){
cell = i *j;if(isPrime) { printf(“p”);} else if (isSquare) { printf(“-”);} else {
printf("%4d", cell); }
} printf(“\n”); }
Question 2 (c)
Test cell for prime
1. If cell is 1, it is not prime
2. Else If cell is divided by any number in [2, itself-1], it is not prime
3. Else it is primeint isPrime = 1; if(cell == 1) isPrime = 0; else{ for(k=2; k<= cell - 1; k++){ if(cell % k == 0) isPrime = 0; } }
Assume cell is prime
Find all cases where cell is not prime
Question 2 (c)
Test cell for square
1. If exists a number k in [1, cell), s.t. k * k == cell then cell is square
isSquare = 0; for(k=1; k<=cell; k++) {
if(k * k == cell) isSquare = 1;
}
Assume cell is not Square
Try to find a k
Question 2 (c)
Put all these together.int i, j, k, cell, input, isSquare, isPrime; scanf(“%d”, &input) for(i=1; i<=input; i++){ for(j=1; j<=i; j++){ cell = i * j; /* Square number check */ isSquare = 0; for(k=1; k<=cell; k++){ if(k * k == cell)
isSquare = 1; }
/* Prime number check */ isPrime = 1; if(cell == 1)
isPrime = 0; for(k=2; k<cell; k++){
if(cell % k == 0) isPrimeNumber = 0;
} /* Choose what to print */ if(isPrime == 1) printf(“ p”); else if(isSquare == 1) printf(“ -”); else printf(“%4d”, i*j);
} printf(“\n”); }
Can you improve your code to run faster ?
Question 3
Solution to Diophantine Equation• In mathematics, we build a linear system and solve it
using Guassian’s Elimination.• In Computer Science, we can do brute force using power
of computers.•Given a set of equations of X,Y. Test all the possible combinations of <X,Y> to find a solution
• Analyze:
1.Read input from file
2.Test all possible values of <X,Y>
3.Output to file
Question 3Read Input From File:
#define NOSOLUTION -99999FILE *fin, *fout;fin = fopen(“input.txt”, “r”);
int x, y, solutionX, solutionY, a1, a2, b1, b2, c1, c2;
fscanf(fin, “%d %d %d”, &a1, &b1, &c1);fscanf(fin, “%d %d %d”, &a2, &b2, &c2);
Will a1 b1 c1 has the same value of a2 b2 c2?
Other opening mode :w writea appendr+ read & write…
“w” will erase all the content of original file
Be careful when read and write on the same file!!!
Question 3
Test all combinations of possible X, Y’s value
Since we have two variable, we use nested loop
solutionX = NOSOLUTION; solutionY = NOSOLUTION; for(x=-100; x<=100; x++) { for(y=-100; y<=100; y++) { if(a1*x + b1*y == c1 && a2*x + b2 * y == c2) {
solutionX = x; solutionY = y;
} } }
Can we insert a break here ? solutionX = x; solutionY = y; break;
Question 3
Output to File Accordingly:
fout = fopen(“ouptut.txt”, “w”);if(solutionX != NOSOLUTION) fprintf(fout,“X = %d, Y = %d”, solutionX, solutionY); else fprintf(fout,“Integer solution not found in range [-100,100]”); fclose(fout);fclose(in);return 0;
If not closed, the result is not guaranteed to be written into file !
Thank You !
See you next week!