עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

עזרה בשפת C | מערך \ malloc

Featured Replies

פורסם

שלום לכולם

אני סטודנט שנה א' במדעי המחשב, נתקעתי בנושא מערכים:

כתבו פונקציה יעילה המקבלת מערך בגודל n של מספרים שלמים ומחזירה 1 אם איברי המערך שונים זה מזה ו- 0 אחרת.

זה הקוד שלי:

#include <stdio.h> //ex2

void sort(int arr[], int size);
int check (int arr[], int size);

void main()
{
int size;
int i;
int *arr;
int x;

printf("enter size of the array \n");
scanf("%d", &size);
arr=malloc (size * sizeof(int));
for (i=0;i<size;i++)
{
printf("enter array element\n");
scanf("%d", arr+i);
}

x=check(arr,size);
if (x>0)
printf(" there are no equal values in the array \n");
else
printf( "there are %d eqaul values in the array \n",x);
sort(arr, size); //output of the sorted array
printf(" the sorted array is: \n");

for(i = 0;i<size;i++)
printf("%d ",arr[i]);


}


void sort( int arr[], int size)
{
int i, j, tmp;
for (j = 1; j< size; j++)
{
tmp = arr[j];
i = j-1;
while (i >= 0 && arr[i] > tmp)
{
arr[i+1] = arr[i] ;
i = i-1 ;
}
arr[i+1] = tmp;
}
}
int check (int arr[], int size)
{
int i;
int flag=0;
for (i=0;i<(size-1) || (flag>0) ; i++)
{
if ( *(arr+i)== *(arr+i+1) )
flag++;


}
return flag;
}

אני רוצה להחזיר את הערך של flag ולהשתמש בו בקטע הראשי main. ככל הנראה יש בעיה בפונק' check (לאחר קליטת הערכים למערך מהמשתמש, התוכנית נתקעת.

) במנגנון ההשוואה בין שני תאים סמוכים. אשמח לקבל הכוון בנושא.

תודה!

פורסם

אכן יש לך בעיה ב-check. תנסה להריץ בעצמך (כן, עם דף ועט) את הלולאה על כמה מערכים לדוגמה כדי לראות מה הבעיה.

פורסם
  • מחבר

אני עדין לא מצליח להבין מה הבעיה. אני יושב על זה כבר 4 שעות וללא כיון אפילו..

עריכה

הבעיה נפתרה

תודה

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.