190 likes | 373 Views
CS1010E Programming Methodology Tutorial 3 Control Structures and Data Files. C14,A15,D11,C08,C11,A02. Question 1 (a) (b). 1 . #include < stdio.h > 2 . int main ( void ) { 3 . int i = 0 , input ; 4 . scanf ( “ % d” , & input ) ; 5. while( i < input ) {
E N D
CS1010E Programming MethodologyTutorial 3Control Structures and Data Files C14,A15,D11,C08,C11,A02
Question 1 (a) (b) 1. #include <stdio.h> 2. intmain(void){ 3. inti= 0,input; 4. scanf(“%d”,&input); 5. while(i< input){ 6. printf("%d\n",i); 7. i++; 8.} 9. return0; 10.} 1. #include <stdio.h> 2. intmain(void){ 3. inti, input; 4. scanf(“%d”,&input) 5. while(i< input) 6. printf("%d\n",i); 7.i++; 8. return0; 9. } • What is output ? • Correct the code to print [0, input)!
Question 1 (c) inti = 0, input; scanf(“%d”, &input); do { printf("%d\n", i); i++; } while(i < input); inti = 0, input; scanf(“%d”, &input); do { printf("%d\n", i); } while(i++ < input); 1. #include <stdio.h> 2. intmain(void){ 3. inti = 0,input; 4. scanf(“%d”,&input); 5. while(i< input){ 6. printf("%d\n",i); 7. i++; 8. } 9. return0; 10. } • Change to Do-while Loop • Which one is correct ? inti = 0, input; scanf(“%d”, &input); if ( i < input) { do { printf("%d\n", i); i++; } while(i < input); }
Question 1 (d) 1. #include <stdio.h> 2. intmain(void){ 3. inti = 0,input; 4. scanf(“%d”,&input); 5. while(i< input){ 6. printf("%d\n",i); 7. i++; 8. } 9. return0; 10. } • Rewrite using for-loop for(i=0;i < input;i++){ printf("%d\n",i); }
Question 1 (d) if(i< input){ do{ printf("%d\n",i); i++; }while(i< input); } • 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++; } 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); } Can you use num%2 == 1 for odd number test? What is wrong here? for(i=0;i < input;i++){ if(i%2 != 0) { printf("%d\n",i); } }
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 j =1 j =2 j =3 “i” indicate row number “j” indicate column number i =1 i =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: • 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) How to print out Top triangle ? • How to print out Pascal's triangle ? • Only the bottom triangle is obtained. • Analyze: • For every cell, cell value = row number * column number • 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
Question 2 (c) • Only the bottom triangle is obtained. Square Number -> ‘-’ • Prime Number -> ‘p’ • Analyze: • For every cell, cell value = row number * column number • Bottom triangle ≡ row number >= column number • We need to test the cell value: • Square number • 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 • Ifcellis 1, it is not prime • Else If cellis divided by any number in [2, itself-1], it is not prime • Else it is prime intisPrime=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 • If exists a number k in [1, cell), s.t. k * k == cellthen 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) /* 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”); elseif(isSquare==1)printf(“ -”); elseprintf(“%4d”,i*j); } printf(“\n”); } • Put all these together. inti, 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; } 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: • Read input from file • Test all possible values of <X,Y> • Output to file
Question 3 Other opening mode : w write a append r+ read & write … “w” will erase all the content of original file • Read Input From File: #define NOSOLUTION -99999 FILE *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); Be careful when read and write on the same file!!! Will a1 b1 c1 has the same value of a2 b2 c2?
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!