-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/adriann elementary #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
pjohn3
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A következetes indentációra figyelj.
A 3-as feladat megoldása még nem jó.
Részleteket lásd a kommentekben.
Feladatok:
✔️ 1. Write a program that prints ‘Hello World’ to the screen.
✔️ 2. Write a program that asks the user for their name and greets them with their name.
❌ 3. Modify the previous program such that only the users Alice and Bob are greeted with their names.
✔️ 4. Write a program that asks the user for a number n and prints the sum of the numbers 1 to n
✔️ 5. Modify the previous program such that only multiples of three or five are considered in the sum, e.g. 3, 5, 6, 9, 10, 12, 15 for n=17
❔ 6. Write a program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n.
❔ 7. Write a program that prints a multiplication table for numbers up to 12.
❔ 8. Write a program that prints all prime numbers. (Note: if your programming language does not support arbitrary size numbers, printing all primes up to the largest number you can easily represent is fine too.)
❔ 9. Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.
❔ 10. Write a program that prints the next 20 leap years.
helloworld/elementary_2_3.c
Outdated
|
|
||
| int main() { | ||
|
|
||
| char name[100] ;/* define name */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ez a sor 4 szóközzel van indentálva
helloworld/elementary_2_3.c
Outdated
|
|
||
| char name[100] ;/* define name */ | ||
|
|
||
| setbuf(stdout, NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ez két szóközzel, és egy tab-bal
helloworld/elementary_2_3.c
Outdated
|
|
||
| setbuf(stdout, NULL); | ||
|
|
||
| printf("Hogy hívnak? "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Innentől meg minden egy vagy két tabbal, ha jól látom... Ez legyen kozisztens, mert más felületek más méretűnek mutatják a tab-ot. Pl. a Notepad++-ban 1 tab == 4 space, ezért ott nem látszik, hogy bármi fura lenne, de itt GitHub-on pl 1 tab kb 5-6 space méretű.
Vagy space-eket, vagy tab-okat használj. A Notepad++ alapértelmezésben tab-okat használ, ha te nem nyomsz mást. Szóval mikor manuálisan indentálsz, használj te is tab-ot, ne a space-t nyomkodd.
Notepad++-ban van egy olyan menüpont, hogy View > Show Symbol > Show White Space and TAB. Ha erre rákattintasz, halványan mutatja, hogy mi a space és mi a tab, és akkor látod. Ha zavar, akkor újra rákattintva kikapcsolhatod.
Ha van egy fájlod, amiben összevissza van keverve a tab és a space, akkor arra is van egy opció, hogy automatikusan lecseréljen minden 4 space-t tab-ra: Edit > Blank Operations > Space to TAB.
A (Leading) verzió csak a soreleji space-eket cseréli le TAB-okra. az (All) mindet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, elolvastam, használom :-)
helloworld/elementary_2_3.c
Outdated
| printf("Hogy hívnak? "); | ||
| scanf("%s", &name); | ||
|
|
||
| if (name[0] == 'A' || name[0] == 'B') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❗️ a feladatban az szerepelt, hogy "only the users Alice and Bob are greeted with their names".
Ez itt viszont Antoniettát és Barbarellát is köszönteni fogja, szóval még nem teljesítettük a feladat követelményét.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ezen még dolgozom, egyelőre abba futottam bele, hogy "Bob" == "Bob" értéke 0 :-(
helloworld/elementary_4.c
Outdated
|
|
||
| int main() { | ||
|
|
||
| int szam; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ez a sor is tab-okkal legyen indentálva, mint a többi. Most space-ekkel van.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kész
| printf("Írj egy számot: "); | ||
| scanf("%d", &szam); | ||
|
|
||
| osszeg = (1+szam)*szam/2; // persze lehetett volna nagyon idétlen ciklussal is megoldani, de ez elegánsabb megoldásnak tűnt :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Ez felvet egy izgalmas témát az algoritmusokról, amivel még találkozni fogsz. Van az ún. "big O notation", ami azt fejezi ki, hogy egy algoritmus által igénybevett idő miként függ a bemenetek számától, méretétől, stb.
Ha kétszer annyi elemmel valami kétszer annyi idejig is tart, magyarán az idő n-nel egyenesen arányok, akkor az algoritmusra azt mondják, hogy O(n) komplexitású. Ha a szükséges idő négyzetesen nő a bemenettel, akkor az O(n²). Ha úgy nő, mint a faktoriális, akkor O(n!).
Itt ha ciklust használtál volna, akkor O(n) komplexitású algoritmust írtál volna, mert minél nagyobb számot ír be az ember, annál többször kell lefutnia a ciklusnak. Ez a megoldás viszont O(1) komplexitású, aminek örülünk, mert ugyanolyan gyors bármely számra.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
legközelebb osszeg = (1 + szam)*szam / 2;
| int i; | ||
| for (i=1; i <= szam; i++) | ||
| { | ||
| if (i % 3 == 0 || i % 5 == 0) { | ||
|
|
||
| osszeg = osszeg + i; | ||
|
|
||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha a <= operátor köré raksz space-t, akkor a = operátor köré is rakj:
for (i = 1; i <= szam; i++)
Ha az int main() { és az if (i % 3 == 0 || i % 5 == 0) {-nél ugyanarra a sorra raktad a nyitó kapcsos zárójelet, akkor for-nál ne kezdd új sorba.
osszeg = osszeg + i; helyett írhattad volna azt, hogy osszeg += i;
A megoldás jó, és szerintem ezt a választ várta aki kitalálta, de ha már az előzőt meg tudtad oldani jobban, akkor megsúgom, hogy erre is van egy jobb megoldás. A for loop minden egyes számon végigmegy, és minden alkalommal meg kell állapítani if-fel, hogy osztható-e az adott szám 3-al, vagy 5-tel, nem pazarlás ez?
Nem lehetne gyorsabban valahogy két for loop-pal csak a 3-al és az 5-tel osztható számokon végigmenni? és akkor még az if se kéne. Van vele valami bökkenő?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Szóval, lehetne azt csinálni, hogy végig megyek a 3-mmal osztható számokon úgy, hogy 3-mmal inkrementálok, aztán az öttel oszthatókon, úgy hogy 5-tel inkrementálok, aztán a 15-tel osztható számokat meg kivonom egy 3. looppal, amit 15-ösével inkrementálok :-). Gyorsabb lesz, de megírni lassabb. Szeretnéd, hogy megcsináljam?
| scanf("%s", &name); | ||
|
|
||
| if (name[0] == 'A' || name[0] == 'B') { | ||
| if ( name == "Alice" || name == "Bob") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A name elé nem kell space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arról, hogy miért nem működik a ==-val történő hasonlítás majd beszéljük.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Egyébként nem azt mondtad, hogy ezt egyszer már megoldottad hogy csak Alice-el és Bob-bal működjön, csak "továbbfejlesztetted", hogy megmutasd, hogy kezdőbetűk szerint is tudod?
| #include <time.h> | ||
| int main() { | ||
|
|
||
| time_t s, val = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ezeket külön sorban deklaráld, az olvashatóbb.
Az is kicsit fura, hogy az egyiknek egyből adsz értéket, a másiknak meg csak két sorral lejebb.
Én így csinálnám:
time_t s = time(NULL);
time_t val = 1;
| int main() { | ||
|
|
||
| time_t s, val = 1; | ||
| struct tm* current_time; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct tm* current_time = localtime(&s);
| current_time = localtime(&s); | ||
| printf("Current year = %d\n",(current_time->tm_year + 1900)); | ||
| int current_year = current_time->tm_year + 1900; | ||
| int next_leap_year = 4 - current_year % 4; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ félrevezető változónév: a next_leap_year alapján arra gondol az ember, hogy ez a következő szökőév évszáma. Én valami olyan nevet adnék neki, hogy years_until_next_leap_year
| int next_leap_year = 4 - current_year % 4; | ||
| int leap_year = current_year + next_leap_year; | ||
|
|
||
| for (int i = 0; i < 20; i++){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A kapcsos zárójel előtt legyen szóköz:
for (int i = 0; i < 20; i++) {
| printf(" %d", a); | ||
| printf("\nGondoltam egy számra 1 és 100 között, találd ki melyikre! \n"); | ||
| scanf(" %d", &number); | ||
| trials++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ez miért kell?
| trials--; | ||
| printf("ezt a számot épp most írtad, figyelj jobban!\n"); | ||
| } | ||
| if (number < a){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space a { előtt
| } | ||
| else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else {
| printf("Szuper! Eltaláltad! %d-t találgattál.\n", trials); | ||
| return 0; | ||
| } | ||
| //most jó No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ezt ne ide írd, hanem a commit message-be (fájlnévvel együtt)
| #include<stdio.h> | ||
|
|
||
| int main(){ | ||
| //definiáljuk a szükséges változókat | ||
| int ellen; | ||
| int adossag; | ||
|
|
||
| setbuf(stdout, NULL); | ||
|
|
||
| printf("\nHány ellenség van?"); | ||
| scanf("%d", &ellen); | ||
| printf("\nHány tallér az adósság?"); | ||
| scanf("%d", &adossag); | ||
|
|
||
| if ((ellen == 1 || ellen == 2 ) && adossag > 100){ | ||
|
|
||
| printf("\nSKANDALUM");} | ||
|
|
||
| if (ellen == 0 && adossag > 0) { | ||
|
|
||
| printf("\nIMBECIL"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| return 0; | ||
| } No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ez nem erre a branch-re való
No description provided.