עבור לתוכן
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

Featured Replies

פורסם
  • מחבר

כתבתי את הקוד הבא:


#include <stdio.h>
#include <stdlib.h>
#define N 4

int main()
{
int matrix[N][N];
static int i=0, j=0, counter=0;
int num;

while ( (scanf("%d",&num)) ==1 )
{

if (counter > N*N)
{
printf("Sliding Error !");
exit(0);
}

else if (i!=N)
{
matrix[i][j]=num;
i++;
counter++;
}
else if ( (i==N) && (j!=N) )
{
matrix[i][j]=num;
i=0;
j++;
counter++;
}

else if ( (i==N) && (j==N) )
{
matrix[i][j]=num;
counter++;
}
}

/*while may finished because one of the following: 1. EOF 2. a char */
if (isdigit(num)==0)
{
printf("Not a number !!");
exit(0);
}



/*Runs all over the matrix values and print them*/
/*==========================================================*/
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
printf("%d\t", matrix[i][j]);

printf("\n");
}


return 0;
}

עדיין לא עובד...

אשמח לקבל פידבק...

תודה.

  • תגובות 35
  • צפיות 8.1k
  • נוצר
  • תגובה אחרונה
פורסם

חביבי, הסתבכת. הנה הרעיון הכללי לאיך לעבור על מטריצה באמצעות לולאת while אחת:

while (/* some condition */) {
if (i >= N) {
/* i has reached the end - terminate loop */
break;
}

/* some code goes here */

j++;
if (j >= N) {
/* j has reached the end - go to next i, reset j */
i++;
j = 0;
}
}

יש מבין?

פורסם
  • מחבר

מקווה שהבנתי...


#include <stdio.h>
#include <stdlib.h>
#define N 4

int main()
{

int matrix[N][N];
static int i=0, j=0;
int num;


while (scanf("%d",&num) ==1)
{

if (i >= N) /* i has reached the end - terminate loop*/
{
break;
}

matrix[i][j]=num;
j++;

if (j >= N)
{
/* j has reached the end - go to next i, reset j*/
i++;
j = 0;
}

} /*while*/

for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
printf("%d\t", matrix[i][j]);

printf("\n");
}
return 0;
} /*main*/

עבור מספרים מ-1 עד-16, קיבלתי פלט תקין.

עכשיו רק חסר לי משהו קטן לפני שאני מתחיל לעשות מניפולציות על המטריצה... אם קיבלתי משהו ששונה מ-int איפה אני ממקם את ה-

printf("NaN !!!");
exit(0);

??

תודה.

פורסם

למה אתה מתעקש לא להשתמש בfor פשוט? האם לממש for באמצעות while זה חלק מהתרגיל?

(רמז: הטיעון הקודם שלך ללמה זה לא יעבוד לא נכון)

לגבי מה לעשות אם קיבלת משהו שונה מ-int - במקרה כזה scanf תחזיר ערך שגיאה.

פורסם
  • מחבר

למה אתה מתעקש לא להשתמש בfor פשוט?

אם אני מעוניין לממש ע"י for איך ייראה המבנה ? ומבחינת תנאי עצירה, אני מניח שאתה מתכוון ל-for מקונן, אז איך אני אקבע את תנאי העצירה שלי ?

יש לי בעיקרון 2 תנאים:

scanf("%d",&num) !=1 && (i==N)

ונניח בלולאה הפנימית:

j==N 

האם אני צודק ?

פורסם

אם אני מעוניין לממש ע"י for איך ייראה המבנה ? ומבחינת תנאי עצירה, אני מניח שאתה מתכוון ל-for מקונן, אז איך אני אקבע את תנאי העצירה שלי ?

תחשוב מתי אתה צריך לעצור, האם זה דורש תנאי עצירה מיוחד, ואם כן - באילו מקרים, ומה לעשות אחרי זה, ותוכל לענות לעצמך על השאלה. :)

ארכיון

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

דיונים חדשים

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.