הגעת למגבלת הצפייה למשתמשים שאינם רשומים באתר
  • נשמח אם תצטרפו לקהילה שלנו. הרשמה לאתר תקנה לכם את האפשרות לשאול שאלות ולהגיב לשרשורים באתר ללא כל עלות
  • טופס ההרשמה לאתר נמצא כאן למטה ולוקח פחות מ-30 שניות למלא אותו (כן, בדקנו עם סטופר 🤓)

בעיה בתכנות

  • פותח/ת השרשור Bariah
  • פורסם בתאריך

Bariah

משתמש משקיע
3,440
1,062
נק' מוניטין:
6,732
3,440
1,062
ראיתי שיש פה הרבה שמתעסקים אז יש לי שאלה קטנה -

int main() {
int numcase, sizeA, sizeB, *pNewSize = 0, i, *A, *B;
while (1) {
printf("Pick a function to activate\n 1 - First paragraph\n 2 - Second paragraph\n 3 - Third paragraph\n 0 - Terminate the application\n");
scanf("%d", &numcase);
switch (numcase) {
case 0:
return 0;
printf("\n\n");
break;

case 1:
printf("Enter a size for the first array\n");
scanf("%d", &sizeA);
A = malloc(sizeA * sizeof(int));
for (i = 0; i < sizeA; i++) {
printf("Enter a number for the first array\n");
scanf("%d", &A);
}
printf("Enter a size for the second array\n");
scanf("%d", &sizeB);
B = malloc(sizeB * sizeof(int));
for (i = 0; i < sizeB; i++) {
printf("Enter a number for the second array\n");
scanf("%d", &B);
}
printf("check\n");
*symmetricDiff(*A, sizeA, *B, sizeB, *pNewSize);
printf("spcheck\n");
printf("\n\n");
free(A);
free(B);
break;

והפונקציה היא -

int *symmetricDiff(int *A, int sizeA, int *B, int sizeB, int *pNewSize)

(כל ההדפסות המיותרות הן בשביל דיבאגינג).
עכשיו הבעיה היא שאני קולט את שני המערכים וברגע שאני מגיע לקריאה לפונקציה (SYMMETRICDIFF) התכנית קורסת.
זה לא מגיע להדפסה שאחרי הקריאה ולא להדפסה שישר אחרי שנכנסתי לפונקציה.
חסכתי את כל הקוד בתכנית שלא רלוונטי לבעיה.
לא כל כך מצליח להבין למה, אשמח לעזרה 🤩
 
שאלה לא קשורה: איפה אתה לומד?
והצעה: תלך לאתר שמתעסק עם זה, תמצא תשובה יותר מהירה ויותר מקצועית כניראה (בלי לזלזל פה באנשים לא מכיר כאן אף אחד)
 
ראיתי שיש פה הרבה שמתעסקים אז יש לי שאלה קטנה -

קוד:
int main() {
    int numcase, sizeA, sizeB, *pNewSize = 0, i, *A, *B;
    while (1) {
        printf("Pick a function to activate\n 1 - First paragraph\n 2 - Second paragraph\n 3 - Third paragraph\n 0 - Terminate the application\n");
        scanf("%d", &numcase);
        switch (numcase) {
        case 0:
            return 0;
            printf("\n\n");
            break;

        case 1:
            printf("Enter a size for the first array\n");
            scanf("%d", &sizeA);
            A = malloc(sizeA * sizeof(int));
            for (i = 0; i < sizeA; i++) {
                printf("Enter a number for the first array\n");
                scanf("%d", &A);
            }
            printf("Enter a size for the second array\n");
            scanf("%d", &sizeB);
            B = malloc(sizeB * sizeof(int));
            for (i = 0; i < sizeB; i++) {
                printf("Enter a number for the second array\n");
                scanf("%d", &B);
            }
            printf("check\n");
            *symmetricDiff(*A, sizeA, *B, sizeB, *pNewSize);
            printf("spcheck\n");
            printf("\n\n");
            free(A);
            free(B);
            break;

והפונקציה היא -

int *symmetricDiff(int *A, int sizeA, int *B, int sizeB, int *pNewSize)

(כל ההדפסות המיותרות הן בשביל דיבאגינג).
עכשיו הבעיה היא שאני קולט את שני המערכים וברגע שאני מגיע לקריאה לפונקציה (SYMMETRICDIFF) התכנית קורסת.
זה לא מגיע להדפסה שאחרי הקריאה ולא להדפסה שישר אחרי שנכנסתי לפונקציה.
חסכתי את כל הקוד בתכנית שלא רלוונטי לבעיה.
לא כל כך מצליח להבין למה, אשמח לעזרה 🤩
השתמש בתגיות קוד להבא.
ותרשום את כל הקוד,במיוחד את הפונקציה אם זה קורס בתוכה.
 
  • פותח/ת השרשור
  • #4
ניסיתי להזכר איך אנשים פה רשמו קוד בלי שיתחרבש הסדר ולא מצאתי 😄

הינה כל הקוד (למרות שהתוכן של הפונקציה לא רלוונטי כי זה לא נכנס אליה בכלל) -

קוד:
int *symmetricDiff(int *A, int sizeA, int *B, int sizeB, int *pNewSize) {
    printf("check1\n");
    int *newArr, i, j, k = 0, cnt = 0;
    printf("check2\n");
    for (i = 0; i < sizeA; i++) {
        printf("loop1\n");
        for (j = 0; j < sizeB; j++) {
            printf("loop2\n");
            if (A[i] != B[j]) {
                cnt++;
            }
        }
        if (cnt == sizeA){
            *pNewSize++;
            newArr = malloc(*pNewSize * sizeof(int));
            newArr[k] = A[i];
            k++;
        }
    }
    for (k = 0; k < pNewSize; k++) {
        printf("%d", newArr[k]);
    }
    return &newArr;
}


int main() {
    int numcase, sizeA, sizeB, *pNewSize = 0, i, *A, *B;
    while (1) {
        printf("Pick a function to activate\n 1 - First paragraph\n 2 - Second paragraph\n 3 - Third paragraph\n 0 - Terminate the application\n");
        scanf("%d", &numcase);
        switch (numcase) {
        case 0:
            return 0;
            printf("\n\n");
            break;

        case 1:
            printf("Enter a size for the first array\n");
            scanf("%d", &sizeA);
            A = malloc(sizeA * sizeof(int));
            for (i = 0; i < sizeA; i++) {
                printf("Enter a number for the first array\n");
                scanf("%d", &A[i]);
            }
            printf("Enter a size for the second array\n");
            scanf("%d", &sizeB);
            B = malloc(sizeB * sizeof(int));
            for (i = 0; i < sizeB; i++) {
                printf("Enter a number for the second array\n");
                scanf("%d", &B[i]);
            }
            printf("check\n");
            *symmetricDiff(*A, sizeA, *B, sizeB, *pNewSize);
            printf("spcheck\n");
            printf("\n\n");
            free(A);
            free(B);
            break;

        default:
            printf("Incorrent input\n");
            printf("\n\n");
            break;
        }
    }
}

כתבתי פה כי ראיתי ששאלו פה שאלות על זה וקיבלו תשובות די מהר, וזו לא שאלה מסובכת.
 
קוד:
int *symmetricDiff(int *A, int sizeA, int *B, int sizeB, int *pNewSize) {
    int *newArr, i, j, k = 0, cnt = 0;
    for (i = 0; i < sizeA; i++) {
        for (j = 0; j < sizeB; j++) {
            if (A[i] != B[j]) {
                cnt++;
            }
        }
        if (cnt == sizeA){
            *pNewSize++;
            newArr =(int*) malloc((*pNewSize) * sizeof(int));
            newArr[k] = A[i];
            k++;
        }
    }
    for (k = 0; k < *pNewSize; k++) {
        printf("%d", newArr[k]);
    }
    return newArr;
}


int main() {
    int numcase, sizeA, sizeB, pNewSize = 0, i, *A, *B;
    while (1) {
        printf("Pick a function to activate\n 1 - First paragraph\n 2 - Second paragraph\n 3 - Third paragraph\n 0 - Terminate the application\n");
        scanf("%d", &numcase);
        switch (numcase) {
        case 0:
            return 0;
            printf("\n\n");
            break;

        case 1:
            printf("Enter a size for the first array\n");
            scanf("%d", &sizeA);
            A = (int*)malloc(sizeA * sizeof(int));
            for (i = 0; i < sizeA; i++) {
                printf("Enter a number for the first array\n");
                scanf("%d", &A[i]);
            }
            printf("Enter a size for the second array\n");
            scanf("%d", &sizeB);
            B =(int*) malloc(sizeB * sizeof(int));
            for (i = 0; i < sizeB; i++) {
                printf("Enter a number for the second array\n");
                scanf("%d", &B[i]);
            }
            symmetricDiff(A, sizeA, B, sizeB, &pNewSize);
            free(A);
            free(B);
            break;

        default:
            printf("Incorrent input\n");
            printf("\n\n");
            break;
        }
    }
}

תיקנתי את הבעיות סינטקס,אתה צריך לחזור על החומר יש לך חוסר הבנה בפונקציות ופויינטרים,כנ"ל העברה ברפרנס.
ויש טעויות בפונקציה.
בהצלחה!
 
  • פותח/ת השרשור
  • #8
תיקנתי את הבעיות סינטקס,אתה צריך לחזור על החומר יש לך חוסר הבנה בפונקציות ופויינטרים,כנ"ל העברה ברפרנס.
ויש טעויות בפונקציה.
בהצלחה!
עברו שבועיים של מילואים ושבוע של גירבו"צ מאז שלמדתי את את החומר בלי לגעת בו אז אני נזכר דרך הידיים 😆
הבנתי את התיקונים והסתדרתי, תודה.

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

מה עיסוקך בתחום אם אפשר לשאול?
כרגע סטודנט
 
שימו לב! השרשור ישן: לא היו תגובות בשרשור מעל 90 יום.

ייתכן שהתוכן בשרשור כבר אינו רלוונטי ולכן עדיף לפתוח שרשור חדש.
Back
למעלה תחתית