Βοήθεια με ασκήσεις προγραμματισμού

Jim175

Πολύ δραστήριο μέλος

Ο Jim175 αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 20 ετών. Έχει γράψει 844 μηνύματα.
Καλησπέρα παιδιά, επειδή είμαι αρκετά αρχάριος θα ήθελα μια βοήθεια σχετικά με το πώς λειτουργούν τα παρακάτω προγράμματα και τι εμφανίζουν
Screenshot_2024-02-07-22-36-51-695_com.miui.gallery.jpg
 

chester20080

Περιβόητο μέλος

Ο Μπάμπης αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 29 ετών και Πτυχιούχος του τμήματος Ηλεκτρολόγων Μηχανικών & Μηχανικών Υπολογιστών ΑΠΘ. Έχει γράψει 5,498 μηνύματα.
λόγω έλλειψης χρονου, το εβαλες στο chatgpt?
 

Jim175

Πολύ δραστήριο μέλος

Ο Jim175 αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 20 ετών. Έχει γράψει 844 μηνύματα.

chester20080

Περιβόητο μέλος

Ο Μπάμπης αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 29 ετών και Πτυχιούχος του τμήματος Ηλεκτρολόγων Μηχανικών & Μηχανικών Υπολογιστών ΑΠΘ. Έχει γράψει 5,498 μηνύματα.
Ναι αλλά δε με βοήθησε έβγαζε ότι το πρόγραμμα της ερώτησης 2.4 είναι λάθος γραμμένο κιόλας 😅
ναι βρισκει ως λαθος το δευτερο srand(t) με το ιδιο t
Το αποτελεσμα ειναι παντα 0 στο sum

This C program generates an array a of 10 random integers between 0 and 99 (inclusive), calculates the sum of 10 random numbers generated using rand(), and subtracts each element of the array a from the generated random numbers while accumulating the sum. Finally, it prints the result.

However, the second call to srand(t) with the same seed t as the first one effectively resets the random number generator to the same state, causing it to produce the same sequence of random numbers. As a result, the elements of the array a and the random numbers generated in the second loop are identical to those generated in the first loop. Therefore, when subtracting the elements of a from the random numbers in the second loop, the result will always be zero, leading to the sum being printed as zero.

To solve this issue and get a meaningful result, you should not reinitialize the random number generator with the same seed.
 

Jim175

Πολύ δραστήριο μέλος

Ο Jim175 αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 20 ετών. Έχει γράψει 844 μηνύματα.
ναι βρισκει ως λαθος το δευτερο srand(t) με το ιδιο t
Το αποτελεσμα ειναι παντα 0 στο sum

This C program generates an array a of 10 random integers between 0 and 99 (inclusive), calculates the sum of 10 random numbers generated using rand(), and subtracts each element of the array a from the generated random numbers while accumulating the sum. Finally, it prints the result.

However, the second call to srand(t) with the same seed t as the first one effectively resets the random number generator to the same state, causing it to produce the same sequence of random numbers. As a result, the elements of the array a and the random numbers generated in the second loop are identical to those generated in the first loop. Therefore, when subtracting the elements of a from the random numbers in the second loop, the result will always be zero, leading to the sum being printed as zero.

To solve this issue and get a meaningful result, you should not reinitialize the random number generator with the same seed.
Νταξει κατάλαβα τι εννοεί, βγάζει νόημα, το 2ο με το foo δεν έχω καταλάβει καθόλου
 

chester20080

Περιβόητο μέλος

Ο Μπάμπης αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 29 ετών και Πτυχιούχος του τμήματος Ηλεκτρολόγων Μηχανικών & Μηχανικών Υπολογιστών ΑΠΘ. Έχει γράψει 5,498 μηνύματα.

Jim175

Πολύ δραστήριο μέλος

Ο Jim175 αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 20 ετών. Έχει γράψει 844 μηνύματα.

chester20080

Περιβόητο μέλος

Ο Μπάμπης αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 29 ετών και Πτυχιούχος του τμήματος Ηλεκτρολόγων Μηχανικών & Μηχανικών Υπολογιστών ΑΠΘ. Έχει γράψει 5,498 μηνύματα.

Χρήστες Βρείτε παρόμοια

Top