תרגיל.

matliv

New member
תרגיל.

כתבתי את זה, זה מזהה אם האיבר זוגי/אי זוגי את הזוגיים כופל פי 2 ולאי זוגיים מוסיף 2. עכשיו אני רוצה שהאיברים לא יהיו קבועים ומספר האיברים לא יהיה קבוע. אני יודע שצריך להשתמש ב- new ואני גם יודע שאני לא יודע להשתמש ב- new. אולי מישהו יכול לכוון אותי, את ההתחלה לפחות?
#include <iostream.h> void evenfun(int *x, int *y) { *x = *y; *x *= 2; } void oddfun(int *x, int *y) { *x = *y; *x += 2; } int main() { int orig[10] = {5,16,7,18,27,31,33,36,47,49}; int odds[7]; int evens[3]; int i,a=0,b=0; for(i=0; i<10; i++) if(orig%2==0) evenfun(evens+a++, orig+i); else oddfun(odds+b++, orig+i); cout<<"\n"<<"The even numbers after modification: "<<"\n"; for(a=0;a<3;a++) cout<<evens[a]<<" "; cout<<"\n"<<"The odd numbers after modification: "<<"\n"; for(a=0;a<7;a++) cout<<odds[a]<<" "; return(0); }​

בתודה מראש.
 

B r u l e

New member
פתרון חלקי

היי, ניסיתי לפתור את התרגיל, אבל יש לי בעייה... אולי תעלה מהי (תרגיל קצת בשבילך). הפתרון:
#include <iostream.h> void EvenFunc(int, int); void OddFunc(int, int); int* orig = new int[]; int* odds = new int[]; int* evens = new int[]; void main(void) { int numToCal = 0; int EvenCount = 0; int OddCount = 0; int i = 0; cout << "How many numbers do you want to calculate? "; cin >> numToCal; cout << "\nPlease enter numbers to calculate: "; for(i=0; i < numToCal; i++) { cin >> orig; if(orig % 2 == 0) { EvenCount++; EvenFunc(EvenCount, i); } else { OddCount++; OddFunc(OddCount, i); } } cout << "\nThe even numbers after modification: \n"; for(i=0; i <= EvenCount; i++) cout << evens << " "; cout << "\nThe odd numbers after modification: \n"; for(i=0; i <= OddCount; i++) cout << odds << " "; cout << "\n"; } void EvenFunc(int EvenCount, int i) { evens[EvenCount] = (orig * 2); } void OddFunc(int OddCount, int i) { odds[OddCount] = (orig + 2); }​
 

B r u l e

New member
כמובן, טיפש שכמותי, הנה מתוקן

#include <iostream.h> void EvenFunc(int, int); void OddFunc(int, int); int* orig = new int[]; int* odds = new int[]; int* evens = new int[]; void main(void) { int numToCal = 0; int EvenCount = 0; int OddCount = 0; int i = 0; cout << "How many numbers do you want to calculate? "; cin >> numToCal; cout << "\nPlease enter numbers to calculate: "; for(i=0; i < numToCal; i++) { cin >> orig; if(orig % 2 == 0) { EvenCount++; EvenFunc(EvenCount, i); } else { OddCount++; OddFunc(OddCount, i); } } cout << "\nThe even numbers after modification: \n"; for(i=1; i <= EvenCount; i++) cout << evens << " "; cout << "\nThe odd numbers after modification: \n"; for(i=1; i <= OddCount; i++) cout << odds << " "; cout << "\n"; } void EvenFunc(int EvenCount, int i) { evens[EvenCount] = (orig * 2); } void OddFunc(int OddCount, int i) { odds[OddCount] = (orig + 2); }​
 

gmorphus

New member
זה יפה שנחה עליך רוח הפתרון

אבל הבנאדם שאל ביקש הנחיה ולא שתפתור לו את התרגיל. הרעיון הוא שנעזור לאנשים להבין ולא לתת להם את התשובה עם כפית. ועכשיו חשבתי לעבור להסביר לו מה זה new אבל אני רואה שעשית כאן משהו שלא ברור לי. הקצית שלושה מערכים בזיכרון, אבל למעשה לא אמרת כמה מקום אתה רוצה. מה זה אמור להיות?
 

gmorphus

New member
אז שורה תחתונה

כמה מקום מוקצה?? איך זה שאתה ניגש למקום שלא מוקצה?
 

annefan

New member
אתה מטורף??

אתה לא יודע כמה זכרון אתה מקצה, ואתה מנסה לכתוב לשם? מה הפלא שהתוכנית שלך עפה במקומות לא צפויים? אחרי שאתה מקבל מהמשתמש את מספר האברים הכללי, אז תקצה את המערכים. השימוש במשתנים גלובלים גם הוא מיותר.
 

gmorphus

New member
זה בדיוק מה שאני חשבתי...

ובגלל היה נראה לי מוזר מה שהוא עשה... חשבתי אולי הוא יודע משהו במקרה הזה שאני לא... תודה annefan
 

Makover

New member
Java to C++ Syndrome

מאפיין את התכניתנים שעברו מ - Java ל - ++C. זה בא לידי ביטוי לא רק במערכים אלא גם (ובעיקר) ביצירת אוביקטים.
 

gmorphus

New member
אתה יכול לפרט?

מה קורה בJAVA? הוא עושה realloc או משהו מקביל כל פעם שאתה מוסיף איבר למערך שהגודל שלו לא ידוע מראש או משהו כזה? מה קורה ביצירת אובייקטים?
 

Makover

New member
אין לי הרבה מה לפרט

רק אומר שב - Java קריאה ל - constructor מתבצעת באמצעות קריאה מפורשת ל - new. אין דבר כזה
MyClass myObejct ;​
כלומר יש, אבל בסה"כ יוצרים כך רפרנס. עוד לא מתבצעת הקריאה ל - ctor. (תקנו אותי אם אני טועה וסליחה אם אני מתבלבל - הרבה זמן לא נגעתי ב - Java). בכל אופן, תכניתנים ב - Java רגילים לעשות new על כל אוביקט וכשהם עוברים ל - ++C הם ממשיכים עם ההרגל הזה (עם זאת, מתגברים על זה מהר מאד...).
 

matliv

New member
קודם כל תודה.

ואישית אין לי בעיה לקבל פתרון, לי זה עוזר להבין. ולאנשי המוסר שביננו - אני לא לומד בקורס כך שזה לא שיעורי בית
בכל אופן, מהי הדרך הנכונה להקצות את המקום?
 

annefan

New member
ככה

#include <iostream> class Stam { int a_; int b_; public: Stam(int a = 0): a_(a) { std::cout << "Creating with a_: " << a_ << std::endl; } ~Stam() { std::cout << "Destroying with a_: " << a_ << std::endl; }; }; int main() { Stam * s1 = new Stam; // Dynamically create an object, pointed-to by s1 Stam * s2 = new Stam(4); // Dynamically create an object, pointed-to by s2 int * arr = new int[8]; // Dynamicaly allocate anonymous array with size 8 arr[0] = 8; // Set the first cell in the array delete [] arr; // Free the array delete s1; // Free the memory pointed by s1 delete s2; // Free the memory pointed by s2 return(0); }​
 

matliv

New member
אולי יש אפשרות

לפתור את זה בלי class? זה פשוט יותר מדי מושגים חדשים בשבילי בבת אחת.
 

matliv

New member
ננסה לתקוף את זה מזווית שונה...

מה לא בסדר פה:
#include <iostream.h> int main() { int i; int num; new int mush[num]; cout<<"how many numbers"; cin>>num; for (i=0; i<num; i++) mush = i+1; return(0); }​

ברור לי שזה לא דומה לשום דבר וכמעט לחלוטין המצאה שלי, אבל מה אני יכול לעשות, ככה זה כשלומדים.
 

B r u l e

New member
הנה הפתרון המלאה והיפה

#include <iostream.h> void EvenFunc(int*, int*, int, int); void OddFunc(int*, int*, int, int); void main(void) { int numToCal = 0; int EvenCount = 0; int OddCount = 0; int i = 0; cout << "How many numbers do you want to calculate? "; cin >> numToCal; int* orig = new int[numToCal]; int* odds = new int[numToCal]; int* evens = new int[numToCal]; cout << "\nPlease enter numbers to calculate: "; for(i=0; i < numToCal; i++) { cin >> orig; if(orig % 2 == 0) { EvenCount++; EvenFunc(orig, evens, EvenCount, i); } else { OddCount++; OddFunc(orig, odds, OddCount, i); } } cout << "\nThe even numbers after modification: \n"; for(i=1; i <= EvenCount; i++) cout << evens << " "; cout << "\nThe odd numbers after modification: \n"; for(i=1; i <= OddCount; i++) cout << odds << " "; cout << "\n"; } void EvenFunc(int* orig, int* evens, int EvenCount, int i) { evens[EvenCount] = (orig * 2); } void OddFunc(int* orig, int* odds, int OddCount, int i) { odds[OddCount] = (orig + 2); }​
 

matliv

New member
זה עובד, אבל איך יכול להיות

שהפונקציות צריכות לקבל שני פוינטרים ושני אינטים ואתה שולח להן רק אינט אחד?
 

gmorphus

New member
כנראה שלא קראת טוב...

הוא שולח בדיוק מה שהוא התכוון לשלוח...
 

annefan

New member
והנה עוד פתרון

#include <vector> #include <algorithm> #include <iostream> int vecinput(int) { int num; std::cin >> num; return num; } int vecdup(int num) { if (num % 2) return num; else return num*2; } void display(int num) { std::cout << num << std::endl; } int main() { int num = 0; std::cout << "Enter number of elements: "; std::cin >> num; if (num < 1) return -1; std::vector<int> vec(num); std::transform(vec.begin(), vec.end(), vec.begin(), vecinput); std::for_each(vec.begin(), vec.end(), display); std::transform(vec.begin(), vec.end(), vec.begin(), vecdup); std::for_each(vec.begin(), vec.end(), display); return 0; } סתם, כי בא לי לתכנת...​
 
למעלה