1 / 10

Introduction to Computer Algorithmics and Programming Ceng 113

This exercise focuses on top-down, stepwise refinement to check the primality of a given number. Variables and operators in C are utilized to divide the task into smaller steps. The program determines if the number is prime or not.

Download Presentation

Introduction to Computer Algorithmics and Programming Ceng 113

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computer Algorithmics and ProgrammingCeng 113 Variables and Operators in C

  2. Exercise - 1 Top-Down, Stepwise Refinement • Top-down, stepwise refinement • Begin with a pseudocode representation of the top: Check the primality of given number. • Divide top into smaller tasks and list them in order: Initialize variables.Input number and try to find a divisor for this number. (The divisor should be different than the given number).Check status and print the primality decision for the given number.

  3. Exercise - 1 Top-Down, Stepwise Refinement • Refine the initialization phase from Initialize variables to: Initialize counter to zero Initialize quotient to zero • Refine Input number and try to find and divisor for this number. (The divisor should be different one and the given number), Input the number divider is equal to (number/2 +1)‏ While the divider greater than 1 and (quotient*divider) not equal to number quotient = number / divider divider = divider – 1

  4. Exercise - 1 Top-Down, Stepwise Refinement • Refine Check the status and print the primality decision for the given number to if divider equal to one print “The given number is prime” else print “The given number is not prime”

  5. // Subject: Check the primality of given number. // Initialize counter to zero // Initialize quotient to zero // Input the number // divider is equal to (number/2 + 1)‏ // While (divider greater than 1) and (quotient*divider is not equal to number)‏ // divider = divider - 1 // quotient = number / divider // if divider equal to one // print "The given number is prime" // else print "The given number is not prime" # include <stdio.h> int main ()‏ { int counter=0, quotient=0, number, divider; printf("Enter an integer number:"); scanf("%d", &number); divider = number/2 + 1; while ((divider>1) && ((quotient*divider) != number))‏ { divider = divider - 1; quotient = number / divider; } if (divider == 1) printf("The number %d is prime.\n", number); else printf("The number %d is not prime. \n", number); return 0; }

  6. Exercise # 3 • Write a program to calculate the value of factorials. • 1.Take the values for upper and lower limits from the screen between 0 and 10 then, • 2. display the factorial values for the each number.

  7. Exercise #3 • Calculate the factorial value of a given integer. • Pre : Take the integer value. The value of this number should be between 0 and 10. • Post: Calculate the factorial value of the given number. (n!=1.2.3...n) • Return: Factorial value should be return. • 1. number, sayac = 0, factorial = 1 • 2. Loop number<0 OR number >10 • 2.1 Input the integer value to number variable. • 3. Sayac = number • 4. Loop sayac != 0 • 4.1 factorial = factorial.sayac • 4.2 sayac = sayac – 1 • 5. Return (factorial)‏

  8. Exercise #3 • # include <stdio.h> • int main ()‏ • { int number=0, factorial=1; • do { • printf(“\n 0-10 aralığında bir tamsayı giriniz :”); • scanf(“%d”, &number); • } while ((number<0) || (number>10)); • while (number >0) { • factorial = factorial * number; • number = number – 1; • } • printf(“\n Factorial : %d”, factorial); • } Main function

  9. Exercise #3 • # include <stdio.h> • int factor(int x); • int main ()‏ • { int number; • do { • printf(“\n 0-10 aralığında bir tamsayı giriniz :”); • scanf(“%d”, &number); • } while ((number<0) || (number>10)); • printf(“\n Factorial : %d”, factor(number)); • } • int factor(int x) { • int factorial = 1; • while (x >0) { • factorial = factorial * x; • x = x – 1; • } • return(factorial); • } Main function Factorial function

  10. Exercise #4 • #include "stdio.h" • int count; /* count is global */ • void func1(void); • void func2(void); • void main(void)‏ • { count = 100; • func1(); • } • void func1(void)‏ • { int temp; • temp = count; • func2(); • printf("\n count is %d \n", count); • } • void func2(void)‏ • { int count; • for (count=1; count<10; count++)‏ • printf(“*"); • }

More Related