1 / 15

Gestructureerd programmeren in C

Gestructureerd programmeren in C. GESPRG Les 2. Rekenen met gehele getallen. int a = 26, b = 7; printf (&quot;%d / %d = %d<br>&quot; , a, b, a / b); printf (&quot;%d %% %d = %d<br>&quot; , a, b, a % b);. Wat is de uitvoer ?. Rekenen met floating point. float a = 25.4, b = 2.0;

slone
Télécharger la présentation

Gestructureerd programmeren in C

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. Gestructureerd programmeren in C GESPRG Les 2

  2. Rekenen met gehelegetallen int a = 26, b = 7; printf("%d / %d = %d\n", a, b, a / b); printf("%d %% %d = %d\n", a, b, a % b); Wat is de uitvoer?

  3. Rekenen met floating point float a = 25.4, b = 2.0; printf("%f / %f = %f\n", a, b, a / b); Wat is de uitvoer? 25.400000 / 2.000000 = 12.700000

  4. Afdrukken van floating point float a = 25.4, b = 2.0; printf("%.1f / %.1f = %.1f\n", a, b, a / b); Wat is de uitvoer? 25.4 / 2.0 = 12.7 float a = 25.4, b = 2.0; printf("%06.2f / %06.2f = %06.2f\n", a, b, a / b); Wat is de uitvoer? 025.40 / 002.00 = 012.70

  5. double versus float • Eenfloatwordtopgeslagen in 32 bits en is niet erg nauwkeurig. Minimaal 6 (max 9) significantecijfers. • Eendoublewordtopgeslagen in 64 bits en is 2x zonauwkeurig. Miniaal15 (max 17) significantecijfers. #include<stdio.h> intmain(void) { floatf_derde = 1 / 3.0; doubled_derde = 1 / 3.0; printf("%.20f\n", f_derde); printf("%.20f\n", d_derde); getchar(); return0; } 0.33333334326744080000 0.33333333333333331000

  6. Voorbeeld: F  C • Schrijf een programma waarmee een temperatuur in graden Fahrenheit omgerekend kan worden naar graden Celsius. • De temperatuur in graden Fahrenheit is een geheel getal. • De temperatuur in gradenCelciusmoetwordenafgerond tot eengeheelgetal. Voorbeelden: TF = 100  TC = 37.7777…  TC = 38 TF = 0  TC = -17.7777…  TC = -18

  7. Voorbeeld: F  C #include <stdio.h> intmain(void) { intC, F; printf("Geef de temperatuur in graden Fahrenheit: "); scanf("%d", &F); C = (F - 32) * (5 / 9); printf("De temperatuur in gradenCelcius is: %d\n", C); fflush(stdin); getchar(); return 0; } Watgaaterverkeerd? Oplossing?

  8. Voorbeeld: F  C #include <stdio.h> intmain(void) { intC, F; printf("Geef de temperatuur in graden Fahrenheit: "); scanf("%d", &F); C = (F - 32) * 5 / 9; printf("De temperatuur in gradenCelcius is: %d\n", C); fflush(stdin); getchar(); return 0; } Erwordtafgekapt in plaats van afgerond! Oplossing?

  9. Voorbeeld: F  C #include <stdio.h> intmain(void) { intC, F; printf("Geef de temperatuur in graden Fahrenheit: "); scanf("%d", &F); C = (F - 32) * 5.0 / 9 + 0.5; printf("De temperatuur in gradenCelcius is: %d\n", C); fflush(stdin); getchar(); return 0; } Is het nu goed?

  10. Voorbeeld: F  C #include <stdio.h> intmain(void) { intC, F; printf("Geef de temperatuur in graden Fahrenheit: "); scanf("%d", &F); C = (F - 32) * 5.0 / 9 + 0.5; printf("De temperatuur in gradenCelcius is: %d\n", C); fflush(stdin); getchar(); return 0; } RondtnietgoedafalsCnegatiefwordt.

  11. Voorbeeld: F  C #include <stdio.h> intmain(void) { intC, F;doubleCdouble; printf("Geef de temperatuur in graden Fahrenheit: "); scanf("%d", &F); Cdouble = (F - 32) * 5.0 / 9; if (Cdouble > 0) { C = Cdouble + 0.5; } else { C = Cdouble - 0.5; } printf("De temperatuur in gradenCelcius is: %d\n", C); fflush(stdin); getchar(); return 0; } Kandatnieteenvoudiger?

  12. Voorbeeld: F  C #include<stdio.h> int main(void) { int F; double C; printf("Geef de temperatuur in graden Fahrenheit: "); scanf("%d", &F); C = (F - 32) * 5.0 / 9; printf("De temperatuur in graden Celcius is: %.0f\n", C); fflush(stdin); getchar(); return 0; } WerktC = (F - 32) * 5 / 9; ookgoed?

  13. Huiswerk: F  C • Schrijf een programma waarmee een temperatuur in graden Fahrenheit omgerekend kan worden naar graden Celsius. • De temperatuur in graden Fahrenheit is een floating point getal met 2 cijfers achter de decimale punt. • De temperatuur in gradenCelciusmoetwordenafgerond op 2 cijfersachter de decimale punt.

  14. Uitwerkinghuiswerk: F  C #include <stdio.h> intmain(void) { float C, F; printf("Geef de temperatuur in graden Fahrenheit:"); scanf("%f", &F); C = (F - 32) * 5 / 9; printf("De temperatuur in gradenCelcius is: %.2f\n", C); getchar(); fflush(stdin); return 0; } Waaromwerkt C = (F – 32) * 5 / 9 nu welgoed? WerktC = 5 / 9 * (F – 32) ookgoed?

  15. Huiswerk • Bestudeer C boek: • paragrafen 2.2 t/m 2.6. • paragrafen 2.8 t/m 2.9. • paragraaf 3.1. • paragraaf 3.4. • paragraaf 3.6. • Maakopdrachten: • 5, 7 en 9 van paragraaf 2.15. • 1 van paragraaf 3.13.

More Related