Informatics3-2024/Practical1

A MathWikiből
(Változatok közti eltérés)
108. sor: 108. sor:
  
 
int main(void) {
 
int main(void) {
     /* deklarációk és értékadások */
+
     /* variable definitions */
 
      
 
      
     for (/* inic. */ ; /* feltétel */; /* minden ciklusmag végén */) {
+
     for (/* inic. */ ; /* condition */; /* after each cycle */) {
 
         printf("%d\n", i);
 
         printf("%d\n", i);
 
     }
 
     }
118. sor: 118. sor:
  
  
===== 3. Do / while ciklusok =====
+
===== 3. Do / while cycle =====
  
Írj C kódot, ami a felhasználótól egy ciklusban egész számokat kér be addig, amíg 0 értéket nem kap. Ekkor pedig írja ki a képernyőre a kapott nemnulla számok átlagát!
+
Write C code that reads integers from the user in a cycle until they input 0. Once that happens the program prints the average of the input numbers.
  
Segítség a nem egész értékű változó (''atlag'') kiírásához:
+
You can create floating point numbers (for the average) with the ''float'' type, and print them like so:
 
<c>
 
<c>
     printf("%f", atlag);
+
     printf("%f", average);
 
</c>
 
</c>
  
Példa '''while''' ciklusra:
+
Example for a '''while''' cycle:
  
 
<c>
 
<c>
while ( /* feltétel */ ) {
+
while ( /* condition */ ) {
     /* utasítások amik minden körben lefutnak */
+
     /* commands that run in each cycle */
 
}
 
}
 
</c>
 
</c>
  
Létezik '''do-while''' ciklus is, ami egyszer mindig lefuttatja a magját és csak utána kezdi teszteni a feltételt:
+
You can also us a ''do-while'' cycle. This guarantees that the block of the cycle executes at least once. (It doesn't check the condition for the first cycle.)
  
 
<c>
 
<c>
 
do {
 
do {
     /* utasítások amik minden körben lefutnak */
+
     /* commands that run in each cycle */
} while (/* feltétel */);
+
} while ( /* condition */ );
 
</c>
 
</c>
  

A lap 2024. február 15., 01:28-kori változata

Tartalomjegyzék

Compiling C

You are free to use any IDE/compiler. If someone is looking for suggestions, I can suggest these:

  • gcc
  • codeblocks
    • Simple opensource IDE
    • Supports Windows, Linux, Mac
    • If you don't have a C compiler already on your Windows then you should install the codeblocks-#.#mingw-setup.exe option. This is an IDE and a compiler in one.
  • online c compiler
    • Only in case you need a compiler urgently and you don't have the time to use any of the above.

Once you have your compiler/IDE try to compile this small code:

#include <stdio.h>
int main() {
    int x = 2;
    printf("hello world! %d\n", x);
    return 0;
}
  • In IDEs I recommend starting an "Empty C project".
  • If you're walking the way of the command like, then create a new file with the hello.c name, then in your command line you can use: gcc hello.c -o hello
  • In the command line you'll need to issue the hello or the hello.exe command to execute your new program. In IDEs there's usually a Run command.
  • Those who won't shy away from the command line should use the gcc -W -Wall hello.c -o hello command to compile from now on. This also gives some warnings that might be useful.


Deliberate errors

Let's make some errors in the "hello.c" code. After each of these changes save your file, compile the program. Once you've seen the error message fix the code again and try the next one.

  • Delete a semicolon from one of the lines
  • on the line with the printf write a different letter instead of "x"
  • define a variable y, but don't use it anywhere
  • delete the last "}"
  • delete the "return 0;" line


Exercises

Open a new project for each exercise or a new file if you're in the command line.

1. Conditional print

This is how an if, else if, else statement looks like:

if (/* condition */) {
    /* commands */
} else if (/* condition */) {
    /* commands */
} else {
    /* commands */
}

Complete the code based on the comments.

#include<stdio.h>
 
int main(void) {
    /* declare and give values to variables x and y */
 
    printf("value of X: %d\n", x);
    printf("value of Y: %d\n", y);
 
    printf("\n");
 
    /* using conditionals make it so only true statements are printed */
    printf("%d is greater than %d\n", x, y);
    printf("%d is greater than %d\n", y, x);
    printf("%d is equal to %d\n", x, x);
 
    return 0;
}

Once done modify your code so that x and y doesn't have any values in the beginning. Instead use scanf() to read the numbers from the user of the program. An example where the integer z is read:

    int z;
    printf("Provide a value for z: ");
    scanf("%d", &z);
    /* From this line z has the value that the user input. (If it truly was an integer.) */


2. For cycle

Segítségnek itt egy for ciklus példa: 0-tól 9-ig kiírjuk a számokat, vagyis a ciklusváltozó értékét (általában i-nek vagy j-nek nevezzük a ciklusváltozót, ami tipikusan minden körben növekszik eggyel, de persze máshogy is lehetne): Here's an example for a for cycle that prints the numbers from 0 to 9. (Usually we call the cycle's variable i or j but this is only a convention.)

int i;
for (i = 0; i < 10; i++) {
    printf("Value of the cycle variable: %d\n", i);
}

Complete the code below so that it prints the even numbers from 1 to 100.

#include<stdio.h>
 
int main(void) {
    /* variable definitions */
 
    for (/* inic. */ ; /* condition */; /* after each cycle */) {
        printf("%d\n", i);
    }
    return 0;
}


3. Do / while cycle

Write C code that reads integers from the user in a cycle until they input 0. Once that happens the program prints the average of the input numbers.

You can create floating point numbers (for the average) with the float type, and print them like so:

    printf("%f", average);

Example for a while cycle:

while ( /* condition */ ) {
    /* commands that run in each cycle */
}

You can also us a do-while cycle. This guarantees that the block of the cycle executes at least once. (It doesn't check the condition for the first cycle.)

do {
    /* commands that run in each cycle */
} while ( /* condition */ );


4. Egyszerű gyökkeresés

Egészítsd ki a következő kódot, amely megkeresi az 5329 négyzetgyökét (lesz neki, és egész)!
(Segítség: nem kell hogy haténkony legyen a kód, elég ha egyesével végigpróbálgatjuk a számokat hogy az-e a gyöke.)

#include<stdio.h>
 
int main(void) {
    int szam = 5329;
    int gyok;
    /* szamolj valamilyen ciklusban, valoszinuleg egy feltetel is kell majd a cikluson belul */    
 
    printf("A %d gyoke %d\n", szam, gyok);
    return 0;
}


5. Sakktábla

Rajzolj ki egy NxN-es sakktábla mintát, ahol X-szel jelöljük a fekete mezőket, és üresen hagyjuk (egy szóköz) a fehéreket. Nem kell keretet adni a táblának. A sakktábla méretét (N) a felhasználótól kérd be!

Tipp: a ciklusokat egymásba is ágyazhatjuk, de ilyenkor nagyon kell figyelni a ciklusváltozókra!

Egymásba ágyazott ciklus példa:

#include<stdio.h>
 
int main(void) {
    int i;
    int j;
 
    for(i = 0; i < 10; i++) {
        printf("i: %d \n", i);
        for(j = 0; j < i; j++) {
            printf(" (%d, %d) ", i, j);
        }
        printf("\n");
    }
    printf("\n");
}


Extra feladatok

Ha eddig eljutottunk, vagy unalmasnak találtuk a fenti feladatokat, akkor leprogramozhatjuk a példákat az elsõ elõadásról. Vagy ránézhetünk ezekre a bonyolultabb feladatokra.

6. Relatív prímek

Írjunk programot mely bekér két pozitív egész számot (n, m), majd kiírja az n-el relatív prím számokat 0-tól m-ig.

7. Gyök

Írjunk programot mely bekér egy pozitív számot és visszaadja a gyökét 0.0001 pontossággal.

8. Barkóba

Írjunk programot mely a barkóba játékot szimulálja. A játékosnak gondolnia kell 0 és 100 között egy egész számra. A program feltesz egy kérdést a játékosnak, amire a játékos 0 (nem) vagy 1 (igen)-el válaszolhat. Majd addig tesz fel kérdéseket amíg biztosan meg tudja mondani hogy mi volt a gondolt szám, ekkor azt kíírja. (Hogyan tudjuk a leggyorsabban megtalálni a számot?)

Személyes eszközök