שפת C/מערכים/תרגילים
מתוך ויקיספר, אוסף ספרי הלימוד והמדריכים החופשי.
תוכן עניינים |
[עריכה] מערכים חד מימדיים
[עריכה] הדפסת ערכי מערך בשורה
כתבו תוכנית שנתון לה מערך תווים בגודל מסוים (5), ועליה להדפיס אותו בשורה. לדוגמה, אם נתון המערך הבא:
char array[5] = {'1', '3', '5', '7', '9'};
היא תדפיס:
1 3 5 7 9
פתרון
#include <stdio.h> int main() { char array[5] = {'1', '3', '5', '7', '9'}; int i; for(i = 0; i < 5; ++i) printf("%c \n", array[i]); return 0; }
[עריכה] קליטת ערכי מערך
כתבו תוכנית הקולטת ערכי מערך בגודל מסוים (5). לבדיקה, הדפיסו את המערך בשורות, כבתרגיל הדפסת ערכי מערך בשורה. השתמשו במערך של מספרים שלמים.
פתרון
#include <stdio.h> int main() { int array[5]; int i; for(i = 0; i < 5; ++i) { int input; printf("Please enter element %d: ", i); scanf("%d", &input); array[i] = input; } for(i = 0; i < 4; ++i) printf("%d ", array[i]); return 0; }
[עריכה] חישוב היסטוגרמה
כתבו תוכנית שנתון לה מערך מספרים שלמים בין 0 ל9 בגודל מסוים (15), ועליה להדפיס היסטוגרמה שלו, כלומר כמה פעמים מופיע כל מספר. לדוגמה, אם נתון המערך הבא:
int array[15] = {1, 3, 5, 7, 9, 1, 3, 5, 8, 9, 1, 3, 5, 8, 9};
היא תדפיס:
0 appears 0 times 1 appears 3 times 2 appears 0 times 3 appears 3 times 4 appears 0 times 5 appears 3 times 6 appears 0 times 7 appears 1 times 8 appears 2 times 9 appears 3 times
פתרון
#include <stdio.h> int main() { int array[15] = {1, 3, 5, 7, 9, 1, 3, 5, 8, 9, 1, 3, 5, 8, 9}; int hist[10]; int i; for(i = 0; i < 10; ++i) hist[i] = 0; for(i = 0; i < 15; ++i) ++hist[array[i]]; for(i = 0; i < 10; ++i) printf("%d appears %d times\n", i, hist[i]); return 0; }
[עריכה] מערכים רב מימדיים
[עריכה] הדפסת ערכי מטריצה (מערך דו-מימדי) בשורות
כתבו תוכנית שנתונה לה מטריצת תווים בגודל מסוים (4 על 3), ועליה להדפיס אותה בשורות ועמודות. לדוגמה, אם נתונה המטריצה הבאה:
char matrix[4][3]= { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
היא תדפיס:
1 2 3 4 5 6 7 8 9 a b c
(רמז: השתמש בלולאה כפולה, כלומר לולאה בתוך לולאה).
פתרון
#include <stdio.h> int main() { char matrix[4][3]= { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}}; int i, j; for(i = 0; i < 4; ++i) { for(j = 0; j < 3; ++j) printf("%c ", matrix[i][j]); printf("\n"); } return 0; }
[עריכה] קליטת ערכי מטריצה
כתבו תוכנית הקולטת ערכי מטריצה בגודל מסוים (4 על 3). לבדיקה, הדפיסו את המטריצה בשורות, כבתרגיל הדפסת ערכי מטריצה (מערך דו-מימדי) בשורות. השתמשו במטריצה של מספרים שלמים.
פתרון
#include <stdio.h> int main() { int matrix[4][3]; int i, j; for(i = 0; i < 4; ++i) for(j = 0; j < 3; ++j) { int input; printf("Please enter element (%d, %d): ", i, j); scanf("%d", &input); matrix[i][j] = input; } for(i = 0; i < 4; ++i) { for(j = 0; j < 3; ++j) printf("%d ", matrix[i][j]); printf("\n"); } return 0; }
[עריכה] שאילתות על ערכי מטריצה
כתבו תוכנית שנתונה לה מטריצת תווים בגודל מסוים (4 על 3), ועליה להדפיס אותה בשורות ועמודות. הנח שהמשתמש סופר מ1, ולא מ0.
לדוגמה, אם נתונה המטריצה הבאה:
char matrix[4][3]= { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
והמשתמש בוחר בשורה 1 ועמודה 2, אז יש להדפיס '2'.
פתרון
#include <stdio.h> int main() { char matrix[4][3]= { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}}; unsigned int row, column; printf("Please enter a row and column: "); scanf("%d %d", &row, &column); /* We need to decrease these variables, since C arrays are 0-based, not 1-based. */ --row; --column; /* Always check for access violation! */ if(row >= 4 || column >= 3) { printf("Illegal choice!"); return -1; } printf("%c\n", matrix[row][column]); return 0; }
[עריכה] חישוב סכומי שורות ועמודות במטריצה
כתבו תוכנית שנתונה לה מטריצת מספרים שלמים בגודל מסוים (4 על 3), ועליה להדפיס את סכום השורות והעמודות.
לדוגמה, אם נתונה המטריצה הבאה:
int matrix[4][3]= { {1,2,3} , {4,5,6} , {7,8,9} , {10, 11, 12}};
אז התוכנית תדפיס שסכומי השורות הם 6, 15, 24, ו33, וסכומי העמודות הם 22, 26, ו30.
פתרון
#include <stdio.h> int main() { int matrix[4][3]= { {1,2,3} , {4,5,6} , {7,8,9} , {10, 11, 12}}; int i, j, sum; for(i = 0; i < 4; ++i) { sum = 0; for(j = 0; j < 3; ++j) sum += matrix[i][j]; printf("The sum of row %d is %d\n", i, sum); } for(j = 0; j < 3; ++j) { sum = 0; for(i = 0; i < 4; ++i) sum += matrix[i][j]; printf("The sum of column %d is %d\n", j, sum); } return 0; }
[עריכה] הדפסת ערכי מטריצה (מערך דו-מימדי) בצורה מעגלית
כתבו תוכנית שנתונה לה מטריצה בגודל מסוים (4 על 3), ועליה להדפיס אותה בצורה מעגלית. לדוגמה, אם נתון המערך הבא:
char matrix[4][3]= { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
שנראה כך:
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| a | b | c |
היא תדפיס:
1 2 3 6 9 c b a 7 4 5 8
פתרון
#include <stdio.h> #define UP 0 #define RIGHT 1 #define DOWN 2 #define LEFT 3 int main() { int dir; //direction int x,y; //posiotion int u,r,d,l; //limits: up, right, down, left int count; //just counts the cells that printed char arr[4][3]= { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}}; //at first, direction set to be right dir=RIGHT; //we start at this corner x=0; y=0; //at first, limits are edges of array u=1; r=3-1; d=4-1; l=0; for(count=0;count<3*4;count++) { printf("%c ", arr[x][y]); switch(dir){ case UP: //move to direction x--; //if we are on the limit: move limit one step to center & change direction if(x===u) { u++; dir=(dir+1)%4; } break; case RIGHT: //move to direction y++; //if we are on the limit: move limit one step to center & change direction if(y===r) { r--; dir=(dir+1)%4; } break; case DOWN: //move to direction x++; //if we are on the limit: move limit one step to center & change direction if(x===d) { d--; dir=(dir+1)%4; } break; case LEFT: //move to direction y--; //if we are on the limit: move limit one step to center & change direction if(y===l) { l++; dir=(dir+1)%4; } break; } } return 0; }