נשלח בתאריך: 20 March 2006 בשעה 04:39 | | IP רשוּם
|
|
|
|
מערכים
רמה קלה
תרגיל 1
int sum_array(int a[], int n)
{
int i, sum = 0;
for (i = 0; i
< n; i++) sum += a;
return sum;
}
תרגיל 2
int has_sum(int a[], int n)
{
/* x1 + x2 = x3
*/
int x1, x2, x3;
for (x3 = 0; x3
< n; ++x3)
{
for (x1 =
0; x1 < n; ++x1)
{
if
(x1 == x3) continue;
for
(x2 = 0; x2 < n; ++x2)
{
if
(x2 == x3 || x2 == x1) continue;
if
(a[x1] + a[x2] == a[x3]) return 1;
}
}
}
return 0;
}
תרגיל 3
void Analyze_array(int* arr, int arr_size, int *x1, int
*x2, double *x3)
{
int min =
arr[0];
int max =
arr[0];
int sum = 0;
int counter;
for (counter =
0; counter < arr_size; counter++)
{
if
(arr[counter] < min) min = arr[counter];
if (arr[counter]
> max) max = arr[counter];
sum +=
arr[counter];
}
*x1 = min;
*x2 = max;
*x3 =
(double)sum / arr_size;
}
תרגיל 4
int main()
{
int arr1[10] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int arr2[10] = {
0 };
int arr3[10],
counter;
for (counter =
0; counter < 10; counter++) arr[counter] = 0;
}
אם נדרש לאתחל את כל התאים ל-7, אנו יכולים לעשות זאת בשתי
דרכים: אתחול מפורש תא אחר תא, או לולאה. לא ניתן להשתמש בטריק שהשתמשנו בו במקרה
של arr2.
תרגיל 5
להשלים
תרגיל 6
להשלים
תרגיל 7
int only_zeros(int mat[N][M])
{
int i, j;
for (i = 0; i
< N; ++i)
{
for (j =
0; j < M; ++j)
{
if
(mat[j] != 0) return 0;
}
}
return 1;
}
תרגיל 8
void print_reverese_odd(int mat[N][M])
{
int i, j;
for (i = 0; i
< N; ++i)
{
for (j =
0; j < M; ++j)
{
if
(i % 2 == 0) printf("%d\t", mat[j]);
else
printf("%d\t", mat[M-1-j]);
}
}
}
רמה בינונית
תרגיל 1
להשלים
תרגיל 2
להשלים
תרגיל 3
#define DAYS 7
int main()
{
int
tickets[DAYS] = { 0 };
char
print_array[10][DAYS];
int day, tickets_num;
int min_day = 0,
max_day = 0;
int counter;
double factor;
int i, j;
printf("Enter
day and tickets, -1 -1 to end)\n");
while
(scanf("%d %d", &day, &tickets_num) == 2)
{
if (day ==
-1 && tickets_num == -1) break;
tickets[day-1]
+= tickets_num;
}
for (counter =
1; counter < DAYS; ++counter)
{
if
(tickets[min_day] > tickets[counter])
min_day
= counter;
if
(tickets[max_day] < tickets[counter])
max_day
= counter;
}
printf("Min:
%d\tMax: %d\n", min_day, max_day);
factor = 10.0 /
tickets[max_day];
for (counter =
1; counter < DAYS; ++counter)
tickets[counter]
*= factor;
for (i = 0; i
<10; ++i)
for (j =
0; j < DAYS; ++j) print_arra[j] =
' ';
for (counter =
0; counter < DAYS; counter++)
{
for (i =
0; i < tickets[counter]; ++i)
{
print_array[counter]
= '*';
}
}
for (i = 9; i
>= 0; --i)
{
for (j =
0; j < DAYS; ++j)
{
printf("%c\t",
print_array[j]);
}
putchar('\n');
}
return 0;
}
תרגיל 4
#include
<stdio.h>
#define X 10
#define Y 20
#define SUCCESS 1
#define FAILURE 0
#define FREE 0
#define OCCUPIED 1
typedef int Cinema[X][Y];
int FindPlaces(Cinema theCinema, int tickets, int* x, int*
y);
int NextCellsAreFree(Cinema theCinema, int tickets, int i,
int j);
void MarkPlacesAsFull(Cinema theCinema, int tickets, int i,
int j);
int main()
{
Cinema theCinema
= { 0 };
int tickets;
int x, y;
while
(scanf("%d", &tickets) == 1)
{
if
(FindPlaces(theCinema, tickets, &x, &y) == SUCCESS)
{
MarkPlacesAsFull(theCinema,
tickets, x, y);
printf("The
places are from (%d, %d) to
(%d, %d)\n", x, y, x, y+tickets);
}
else
printf("Not enough place in the cinema\n");
}
return 0;
}
int FindPlaces(Cinema theCinema, int tickets, int* x, int*
y)
{
int i, j, k;
for (i = 0; i
< X; ++i)
{
for (j =
0; j < Y; ++j)
{
if
(theCinema[j] == FREE &&
NextCellsAreFree(theCinema,
tickets, i, j))
{
*x
= i;
*y
= j;
return
SUCCESS;
}
}
}
return FAILURE;
}
int NextCellsAreFree(Cinema theCinema, int tickets, int i,
int j)
{
int counter;
for (counter =
0; counter < tickets; ++counter)
{
if
(j+counter >= Y) return FAILURE;
if
(theCinema[j+counter] != FREE) return FAILURE;
}
return SUCCESS;
}
void MarkPlacesAsFull(Cinema theCinema, int tickets, int i,
int j)
{
int counter;
for (counter =
0; counter < tickets; ++counter)
{
theCinema[j+counter]
= OCCUPIED;
}
}
__________________ מספר האייסיקיו שלי ו/או כתובת ה-MSN שלי אינם מהווים מוקד תמיכה
|