Upload batch 36/306
Browse files- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-12.c +172 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-2.c +164 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-3.c +235 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-4.c +357 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-5.c +137 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-6.c +114 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-7.c +192 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-8.c +181 -0
- שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-9.c +198 -0
- שנה א/מבוא למערכות מחשב/מבחנים/דף נוסחאות.pdf +3 -0
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-12.c
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdio.h>
|
| 2 |
+
#include <stdlib.h>
|
| 3 |
+
|
| 4 |
+
#define R 3
|
| 5 |
+
#define C 3
|
| 6 |
+
#define N 3
|
| 7 |
+
|
| 8 |
+
void inputFromUserToMat(int mat[R][C]) {
|
| 9 |
+
printf("---Matrix Input---\nMatrix size: %dx%d\nRows: %d, Columns: %d\nTotal number: %d\n", R, C, R, C, R * C);
|
| 10 |
+
for (int i = 0; i < R; i++)
|
| 11 |
+
{
|
| 12 |
+
for (int j = 0; j < C; j++)
|
| 13 |
+
{
|
| 14 |
+
printf("Now insteting into mat[%d][%d], Enter value:", i + 1, j + 1);
|
| 15 |
+
scanf_s("%d", &mat[i][j]);
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
void printMat(int mat[R][C]) {
|
| 21 |
+
for (int i = 0; i < R; i++)
|
| 22 |
+
{
|
| 23 |
+
printf("[");
|
| 24 |
+
for (int j = 0; j < C; j++)
|
| 25 |
+
{
|
| 26 |
+
if (j + 1 == C)
|
| 27 |
+
printf("%d", mat[i][j]);
|
| 28 |
+
else
|
| 29 |
+
printf("%d,", mat[i][j]);
|
| 30 |
+
}
|
| 31 |
+
printf("]\n");
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
void matCalculate(int Mat[R][C], int* sum, int* mul, double* avg) {
|
| 36 |
+
for (int i = 0; i < R; i++)
|
| 37 |
+
{
|
| 38 |
+
for (int j = 0; j < C; j++)
|
| 39 |
+
{
|
| 40 |
+
*sum += Mat[i][j];
|
| 41 |
+
*mul *= Mat[i][j];
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
*avg = (double)*sum / (R * C);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
void calcDiagonals(int Mat[N][N], int* sumMain, int* sumSecondary) {
|
| 48 |
+
for (int i = 0; i < N; i++)
|
| 49 |
+
{
|
| 50 |
+
for (int j = 0; j < N; j++)
|
| 51 |
+
{
|
| 52 |
+
if (i == j)
|
| 53 |
+
*sumMain += Mat[i][j];
|
| 54 |
+
if (i + j == N - 1)
|
| 55 |
+
*sumSecondary += Mat[i][j];
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
int rowInMat(int Mat[R][C], int arr[R]) {
|
| 61 |
+
int i = 0; // Row index
|
| 62 |
+
int low, high; // Pointers for binary search
|
| 63 |
+
int found; // Flag to check if the element is found in the current row
|
| 64 |
+
|
| 65 |
+
while (i < R) { // Loop through each row of the matrix
|
| 66 |
+
int curNumber = arr[i]; // The target number to search for in row i
|
| 67 |
+
low = 0; // Initialize the low pointer for binary search
|
| 68 |
+
high = C - 1; // Initialize the high pointer for binary search
|
| 69 |
+
found = 0; // Reset found flag for each row
|
| 70 |
+
|
| 71 |
+
// Perform binary search on the current row
|
| 72 |
+
while (low <= high) {
|
| 73 |
+
int mid = (low + high) / 2; // Find the middle element
|
| 74 |
+
|
| 75 |
+
if (Mat[i][mid] == curNumber) { // Check if mid element matches curNumber
|
| 76 |
+
found = 1; // Mark the number as found
|
| 77 |
+
break; // Exit the binary search loop
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
if (Mat[i][mid] < curNumber) {
|
| 81 |
+
low = mid + 1; // Search in the right half
|
| 82 |
+
}
|
| 83 |
+
else {
|
| 84 |
+
high = mid - 1; // Search in the left half
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// If the number was not found in the current row, return 0
|
| 89 |
+
if (!found) return 0;
|
| 90 |
+
|
| 91 |
+
// Move to the next row
|
| 92 |
+
i++;
|
| 93 |
+
|
| 94 |
+
// Reset binary search pointers for the next row
|
| 95 |
+
low = 0;
|
| 96 |
+
high = C - 1;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
// If all rows satisfy the condition, return 1
|
| 100 |
+
return 1;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
// Function to shift each row of a matrix one position to the right
|
| 104 |
+
void turnMatToRight(int Mat[R][C]) {
|
| 105 |
+
for (int i = 0; i < R; i++) { // Loop through each row
|
| 106 |
+
int last = Mat[i][C - 1]; // Save the last element of the current row
|
| 107 |
+
|
| 108 |
+
// Shift elements of the row to the right, starting from the end
|
| 109 |
+
for (int j = C - 1; j > 0; j--) { // Start at the last column and move backward
|
| 110 |
+
Mat[i][j] = Mat[i][j - 1]; // Move each element to the next position
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Place the saved last element at the first position of the row
|
| 114 |
+
Mat[i][0] = last;
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
int main()
|
| 120 |
+
{
|
| 121 |
+
// 1
|
| 122 |
+
//int mat[R][C];
|
| 123 |
+
//inputFromUserToMat(mat);
|
| 124 |
+
//printMat(mat);
|
| 125 |
+
|
| 126 |
+
// 2
|
| 127 |
+
//int Mat[R][C];
|
| 128 |
+
//inputFromUserToMat(Mat);
|
| 129 |
+
//int sum = 0, mul = 1;
|
| 130 |
+
//double avg = 0;
|
| 131 |
+
//matCalculate(Mat, &sum, &mul, &avg);
|
| 132 |
+
//printf("Sum: %d, Mul: %d, Avg: %.2lf", sum, mul, avg);
|
| 133 |
+
|
| 134 |
+
// 3
|
| 135 |
+
//int Mat[N][N] = {
|
| 136 |
+
// {9,2,1},
|
| 137 |
+
// {3,6,3},
|
| 138 |
+
// {7,2,1}
|
| 139 |
+
//};
|
| 140 |
+
//int sumMain = 0, sumSecondary = 0;
|
| 141 |
+
//calcDiagonals(Mat, &sumMain, &sumSecondary);
|
| 142 |
+
//printf("sumMain : %d, sumSecondary : %d",sumMain, sumSecondary);
|
| 143 |
+
|
| 144 |
+
// 4
|
| 145 |
+
//int M[3][3] = {
|
| 146 |
+
//{2, 4, 9},
|
| 147 |
+
//{3, 7, 11},
|
| 148 |
+
//{1, 2, 3}
|
| 149 |
+
//};
|
| 150 |
+
//int arr[3] = { 4, 7, 1 };
|
| 151 |
+
|
| 152 |
+
//printf("%d\n", rowInMat(M, arr));
|
| 153 |
+
|
| 154 |
+
//int M1[3][3] = {
|
| 155 |
+
//{2, 4, 9},
|
| 156 |
+
//{3, 7, 11},
|
| 157 |
+
//{1, 2, 3}
|
| 158 |
+
//};
|
| 159 |
+
//int arr1[3] = { 4, 7, 5 };
|
| 160 |
+
|
| 161 |
+
//printf("%d\n", rowInMat(M1, arr1));
|
| 162 |
+
|
| 163 |
+
// 5
|
| 164 |
+
//int M[3][3] = {
|
| 165 |
+
//{2, 4, 9},
|
| 166 |
+
//{3, 7, 11},
|
| 167 |
+
//{1, 2, 3}
|
| 168 |
+
//};
|
| 169 |
+
//turnMatToRight(M);
|
| 170 |
+
//printMat(M);
|
| 171 |
+
return 0;
|
| 172 |
+
}
|
שנה א/מ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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdio.h>
|
| 2 |
+
#include <math.h>
|
| 3 |
+
|
| 4 |
+
// Function to calculate and print the diagonal of a square given its side length
|
| 5 |
+
void printDiagonal() {
|
| 6 |
+
double side = 0;
|
| 7 |
+
double diagonal;
|
| 8 |
+
printf("Please enter length of square side:\n");
|
| 9 |
+
scanf_s("%lf", &side);
|
| 10 |
+
|
| 11 |
+
if (side > 0) {
|
| 12 |
+
diagonal = sqrt((side * side) + (side * side));
|
| 13 |
+
printf("Diagonal length is: %.2lf\n", diagonal);
|
| 14 |
+
}
|
| 15 |
+
else {
|
| 16 |
+
printf("Enter a valid number (greater than 0)\n");
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
// Function to calculate and print the perimeter, area, and volume of a circle/sphere given its radius
|
| 21 |
+
void printPerimeter() {
|
| 22 |
+
double PI = 3.141592;
|
| 23 |
+
double radius = 0, perimeter, area, volume;
|
| 24 |
+
printf("Enter radius of circle:");
|
| 25 |
+
scanf_s("%lf", &radius);
|
| 26 |
+
if (radius > 0) {
|
| 27 |
+
perimeter = 2 * PI * radius;
|
| 28 |
+
area = PI * radius * radius;
|
| 29 |
+
volume = radius * radius * radius * PI * (4.0 / 3.0);
|
| 30 |
+
printf("Volume = %.2lf, Perimeter = %.2lf, Area = %.2lf", volume, perimeter, area);
|
| 31 |
+
}
|
| 32 |
+
else {
|
| 33 |
+
printf("Enter a valid number (greater than 0)\n");
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// Function to calculate the nth element in an arithmetic sequence
|
| 38 |
+
double calcAn(double a1, double d, int n) {
|
| 39 |
+
double an = 0;
|
| 40 |
+
if (d != 0.0 && n > 0) {
|
| 41 |
+
an = a1 + (d * (n - 1));
|
| 42 |
+
}
|
| 43 |
+
else {
|
| 44 |
+
printf("Enter a valid number (not 0)\n");
|
| 45 |
+
}
|
| 46 |
+
return an;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// Wrapper to prompt user and print the nth element of an arithmetic sequence
|
| 50 |
+
void printNthElement() {
|
| 51 |
+
double a1 = 1, d = 0.0, an = 0;
|
| 52 |
+
int n = 1;
|
| 53 |
+
|
| 54 |
+
printf("Please enter a1, d, n:");
|
| 55 |
+
scanf_s("%lf %lf %d", &a1, &d, &n);
|
| 56 |
+
|
| 57 |
+
an = calcAn(a1, d, n);
|
| 58 |
+
printf("A%d = %.3lf", n, an);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
// Function to calculate and print the sum of the first n elements in an arithmetic sequence
|
| 62 |
+
void printSumElements() {
|
| 63 |
+
double a1 = 1, d = 0.0, sum = 0.0, an = 0.0;
|
| 64 |
+
int n = 1;
|
| 65 |
+
|
| 66 |
+
printf("Please enter a1, d, n:");
|
| 67 |
+
scanf_s("%lf %lf %d", &a1, &d, &n);
|
| 68 |
+
|
| 69 |
+
an = calcAn(a1, d, n);
|
| 70 |
+
sum = (n * (a1 + an)) / 2;
|
| 71 |
+
printf("S = %.3lf", sum);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
// Function to calculate the nth element in a geometric sequence
|
| 75 |
+
double calcAn1(double a1, double q, int n) {
|
| 76 |
+
double an = 0;
|
| 77 |
+
if (q != 0.0 && n > 0) {
|
| 78 |
+
an = a1 * pow(q, n - 1);
|
| 79 |
+
}
|
| 80 |
+
else {
|
| 81 |
+
printf("Enter a valid number (not 0)\n");
|
| 82 |
+
}
|
| 83 |
+
return an;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// Wrapper to prompt user and print the nth element of a geometric sequence
|
| 87 |
+
void printNthElement1() {
|
| 88 |
+
double a1 = 1, q = 0.0, an = 0;
|
| 89 |
+
int n = 1;
|
| 90 |
+
|
| 91 |
+
printf("Please enter a1, q, n:");
|
| 92 |
+
scanf_s("%lf %lf %d", &a1, &q, &n);
|
| 93 |
+
|
| 94 |
+
an = calcAn1(a1, q, n);
|
| 95 |
+
printf("A%d = %.3lf", n, an);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// Function to calculate and print the sum of the first n elements in a geometric sequence
|
| 99 |
+
void printSumElements1() {
|
| 100 |
+
double a1 = 1, q = 0.0, sum = 0.0, an = 0.0;
|
| 101 |
+
int n = 1;
|
| 102 |
+
|
| 103 |
+
printf("Please enter a1, q, n:");
|
| 104 |
+
scanf_s("%lf %lf %d", &a1, &q, &n);
|
| 105 |
+
|
| 106 |
+
an = calcAn1(a1, q, n);
|
| 107 |
+
sum = (a1 * (pow(q, n) - 1)) / (q - 1);
|
| 108 |
+
printf("S = %.3lf", sum);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// Function to calculate and print the sum of squares of digits for a three-digit number
|
| 112 |
+
void printSumTri() {
|
| 113 |
+
int num = 0;
|
| 114 |
+
int n1, n2, n3, sum;
|
| 115 |
+
printf("Please enter a number between 100 - 999\n");
|
| 116 |
+
scanf_s("%d", &num);
|
| 117 |
+
if (num >= 100 && num <= 999) {
|
| 118 |
+
n1 = num % 10;
|
| 119 |
+
num /= 10;
|
| 120 |
+
n2 = num % 10;
|
| 121 |
+
num /= 10;
|
| 122 |
+
n3 = num % 10;
|
| 123 |
+
sum = pow(n1, 2) + pow(n2, 2) + pow(n3, 2);
|
| 124 |
+
printf("%d", sum);
|
| 125 |
+
}
|
| 126 |
+
else {
|
| 127 |
+
printf("Please enter a VALID number\n");
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// Function to extract and print only the decimal part of a floating-point number
|
| 132 |
+
void printAfterDecimal() {
|
| 133 |
+
double d = 0;
|
| 134 |
+
int whole = 0;
|
| 135 |
+
printf("Please enter a floating number\n");
|
| 136 |
+
scanf_s("%lf", &d);
|
| 137 |
+
whole = (int)d;
|
| 138 |
+
d = d - (double)whole;
|
| 139 |
+
printf("d = %.2lf", d);
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// Function to calculate and print Bob's new salary based on previous salary and compensation
|
| 143 |
+
void printBobSalary() {
|
| 144 |
+
double prevSalary = 0, compansation = 0, salary = 0;
|
| 145 |
+
printf("Please enter: previous salary, compensation\n");
|
| 146 |
+
scanf_s("%lf %lf", &prevSalary, &compansation);
|
| 147 |
+
|
| 148 |
+
salary = ceil((0.75 * prevSalary) + (0.1 * compansation));
|
| 149 |
+
printf("Bob's new salary: %.2lf", salary);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
int main() {
|
| 153 |
+
// Uncomment a function call to test each assignment
|
| 154 |
+
//printDiagonal();
|
| 155 |
+
//printPerimeter();
|
| 156 |
+
//printNthElement();
|
| 157 |
+
//printSumElements();
|
| 158 |
+
//printNthElement1();
|
| 159 |
+
//printSumElements1();
|
| 160 |
+
//printSumTri();
|
| 161 |
+
//printAfterDecimal();
|
| 162 |
+
//printBobSalary();
|
| 163 |
+
return 0;
|
| 164 |
+
}
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-3.c
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
| 6 |
+
double x = 0;
|
| 7 |
+
printf("Please enter a number:\n");
|
| 8 |
+
scanf_s("%lf", &x);
|
| 9 |
+
if (x > 0) {
|
| 10 |
+
printf("%lf\n", x);
|
| 11 |
+
}
|
| 12 |
+
else {
|
| 13 |
+
printf("%lf\n", -x);
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
// Function to calculate Alice's total payment with discounts based on the amount
|
| 18 |
+
void printAlicePay() {
|
| 19 |
+
double price = 0, amount = 0, total = 0;
|
| 20 |
+
printf("Please enter: Price, Amount\n");
|
| 21 |
+
scanf_s("%lf %lf", &price, &amount);
|
| 22 |
+
|
| 23 |
+
if (amount >= 50) {
|
| 24 |
+
total = 0.5 * (price * amount); // 50% discount
|
| 25 |
+
}
|
| 26 |
+
else if (amount >= 25) {
|
| 27 |
+
total = 0.8 * (price * amount); // 20% discount
|
| 28 |
+
}
|
| 29 |
+
else if (amount > 10) {
|
| 30 |
+
total = 0.95 * (price * amount); // 5% discount
|
| 31 |
+
}
|
| 32 |
+
else {
|
| 33 |
+
total = price * amount; // No discount
|
| 34 |
+
}
|
| 35 |
+
printf("Total is: %lf\n", total);
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// Function to check if an integer is "valid" based on given conditions
|
| 39 |
+
void printValidNumber() {
|
| 40 |
+
int num = 0;
|
| 41 |
+
int valid = -1;
|
| 42 |
+
printf("Please enter a positive integer\n");
|
| 43 |
+
scanf_s("%d", &num);
|
| 44 |
+
|
| 45 |
+
if (num % 2 == 0 && num % 3 == 0 && num % 5 != 0) {
|
| 46 |
+
valid = 1;
|
| 47 |
+
}
|
| 48 |
+
if (num % 2 != 0 && num % 7 != 0 && num % 11 != 0) {
|
| 49 |
+
valid = 1;
|
| 50 |
+
}
|
| 51 |
+
if (valid == 1) {
|
| 52 |
+
printf("Valid Number\n");
|
| 53 |
+
}
|
| 54 |
+
else {
|
| 55 |
+
printf("Invalid Number\n");
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// Function to check if three numbers are "valid" based on divisibility rules
|
| 60 |
+
void printVlaidTri() {
|
| 61 |
+
int num1, num2, num3;
|
| 62 |
+
printf("Enter three numbers:\n");
|
| 63 |
+
scanf_s("%d %d %d", &num1, &num2, &num3);
|
| 64 |
+
|
| 65 |
+
int valid = -1;
|
| 66 |
+
if ((num1 % num2 == 0 || num2 % num1 == 0) &&
|
| 67 |
+
(num1 % num3 == 0 || num3 % num1 == 0) &&
|
| 68 |
+
(num2 % num3 == 0 || num3 % num2 == 0))
|
| 69 |
+
valid = 1;
|
| 70 |
+
|
| 71 |
+
if (valid == 1)
|
| 72 |
+
printf("Valid Numbers\n");
|
| 73 |
+
else
|
| 74 |
+
printf("Invalid Numbers\n");
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Function to calculate and print a person's age based on birth date and today's date
|
| 78 |
+
void printAge() {
|
| 79 |
+
int currYear = 2024, currMonth = 11, currDay = 13;
|
| 80 |
+
int birthYear = 0, birthMonth = 0, birthDay = 0, age = 0;
|
| 81 |
+
int hadBirthday = -1;
|
| 82 |
+
|
| 83 |
+
printf("Please enter: Birth year, Birth month, Birth Day\n");
|
| 84 |
+
scanf_s("%d %d %d", &birthYear, &birthMonth, &birthDay);
|
| 85 |
+
|
| 86 |
+
age = currYear - birthYear;
|
| 87 |
+
|
| 88 |
+
if (currMonth > birthMonth || (currMonth == birthMonth && currDay >= birthDay)) {
|
| 89 |
+
hadBirthday = 1; // Already had birthday this year
|
| 90 |
+
if (currMonth == birthMonth && currDay == birthDay) {
|
| 91 |
+
printf("Happy Birthday!\n");
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
if (hadBirthday == -1) age--; // Decrease age if birthday hasn't occurred this year
|
| 96 |
+
|
| 97 |
+
printf("Your age is: %d\n", age);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// Function to determine the quadrant of a point (x, y)
|
| 101 |
+
void printQuadrant() {
|
| 102 |
+
int x = 0, y = 0, quad = 0;
|
| 103 |
+
char xSign = ' ', ySign = ' ';
|
| 104 |
+
|
| 105 |
+
printf("Please enter: x, y:\n");
|
| 106 |
+
scanf_s("%d %d", &x, &y);
|
| 107 |
+
|
| 108 |
+
xSign = (x > 0) ? '+' : '-';
|
| 109 |
+
ySign = (y > 0) ? '+' : '-';
|
| 110 |
+
|
| 111 |
+
if (x > 0 && y > 0) quad = 1;
|
| 112 |
+
else if (x < 0 && y > 0) quad = 2;
|
| 113 |
+
else if (x < 0 && y < 0) quad = 3;
|
| 114 |
+
else quad = 4;
|
| 115 |
+
|
| 116 |
+
printf("(%d,%d) is in Quadrant %d (%c,%c)\n", x, y, quad, xSign, ySign);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
// Function to calculate the distance between two points
|
| 120 |
+
double getDistance() {
|
| 121 |
+
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
|
| 122 |
+
double distance = 0;
|
| 123 |
+
|
| 124 |
+
printf("Please enter: x1, y1, x2, y2:\n");
|
| 125 |
+
scanf_s("%d %d %d %d", &x1, &y1, &x2, &y2);
|
| 126 |
+
|
| 127 |
+
distance = sqrt((pow(x1 - x2, 2)) + (pow(y1 - y2, 2)));
|
| 128 |
+
return distance;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// Function to solve a quadratic equation and print the roots
|
| 132 |
+
void printTrinome() {
|
| 133 |
+
int a = 0, b = 0, c = 0;
|
| 134 |
+
double x1 = 0, x2 = 0, disc = 0;
|
| 135 |
+
|
| 136 |
+
printf("Please enter: a, b, c:\n");
|
| 137 |
+
scanf_s("%d %d %d", &a, &b, &c);
|
| 138 |
+
|
| 139 |
+
disc = b * b - 4 * a * c;
|
| 140 |
+
|
| 141 |
+
if (disc < 0) {
|
| 142 |
+
printf("There are no non-complex solutions\n");
|
| 143 |
+
}
|
| 144 |
+
else {
|
| 145 |
+
x1 = (-b + sqrt(disc)) / (2 * a);
|
| 146 |
+
x2 = (-b - sqrt(disc)) / (2 * a);
|
| 147 |
+
printf("The solutions are: x1 = %.2lf, x2 = %.2lf\n", x1, x2);
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
// Function to check if the given hypotenuse and height form a valid right-angle triangle
|
| 152 |
+
void printRightAngleTri() {
|
| 153 |
+
double hypo = 0, height = 0, area = 0;
|
| 154 |
+
double disc = 0;
|
| 155 |
+
|
| 156 |
+
printf("Please enter: hypotenuse, height\n");
|
| 157 |
+
scanf_s("%lf %lf", &hypo, &height);
|
| 158 |
+
|
| 159 |
+
if (hypo <= 0 || height <= 0) {
|
| 160 |
+
printf("Please enter non-negative numbers\n");
|
| 161 |
+
}
|
| 162 |
+
else {
|
| 163 |
+
area = hypo * height * 0.5;
|
| 164 |
+
disc = hypo * hypo - 4 * height * height;
|
| 165 |
+
if (disc >= 0) {
|
| 166 |
+
printf("Valid Right-angle triangle, Area is: %lf\n", area);
|
| 167 |
+
}
|
| 168 |
+
else {
|
| 169 |
+
printf("Not a Right-angle triangle\n");
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
// Function to print the astrological sign based on the date
|
| 175 |
+
void printAstrologicalSign() {
|
| 176 |
+
int day = 0, month = 0, year = 0;
|
| 177 |
+
|
| 178 |
+
printf("Please enter: Day, Month, Year\n");
|
| 179 |
+
scanf_s("%d %d %d", &day, &month, &year);
|
| 180 |
+
|
| 181 |
+
if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
|
| 182 |
+
printf("Aquarius\n");
|
| 183 |
+
}
|
| 184 |
+
else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
|
| 185 |
+
printf("Pisces\n");
|
| 186 |
+
}
|
| 187 |
+
else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
|
| 188 |
+
printf("Aries\n");
|
| 189 |
+
}
|
| 190 |
+
else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
|
| 191 |
+
printf("Taurus\n");
|
| 192 |
+
}
|
| 193 |
+
else if ((month == 5 && day >= 21) || (month == 6 && day <= 20)) {
|
| 194 |
+
printf("Gemini\n");
|
| 195 |
+
}
|
| 196 |
+
else if ((month == 6 && day >= 21) || (month == 7 && day <= 22)) {
|
| 197 |
+
printf("Cancer\n");
|
| 198 |
+
}
|
| 199 |
+
else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
|
| 200 |
+
printf("Leo\n");
|
| 201 |
+
}
|
| 202 |
+
else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
|
| 203 |
+
printf("Virgo\n");
|
| 204 |
+
}
|
| 205 |
+
else if ((month == 9 && day >= 23) || (month == 10 && day <= 22)) {
|
| 206 |
+
printf("Libra\n");
|
| 207 |
+
}
|
| 208 |
+
else if ((month == 10 && day >= 23) || (month == 11 && day <= 21)) {
|
| 209 |
+
printf("Scorpio\n");
|
| 210 |
+
}
|
| 211 |
+
else if ((month == 11 && day >= 22) || (month == 12 && day <= 21)) {
|
| 212 |
+
printf("Sagittarius\n");
|
| 213 |
+
}
|
| 214 |
+
else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {
|
| 215 |
+
printf("Capricorn\n");
|
| 216 |
+
}
|
| 217 |
+
else {
|
| 218 |
+
printf("Invalid date\n");
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
int main() {
|
| 223 |
+
// Uncomment the function you want to test
|
| 224 |
+
// absolute();
|
| 225 |
+
// printAlicePay();
|
| 226 |
+
// printValidNumber();
|
| 227 |
+
// printVlaidTri();
|
| 228 |
+
// printAge();
|
| 229 |
+
// printQuadrant();
|
| 230 |
+
// printf("Distance is: %lf\n", getDistance());
|
| 231 |
+
// printTrinome();
|
| 232 |
+
// printRightAngleTri();
|
| 233 |
+
// printAstrologicalSign();
|
| 234 |
+
return 0;
|
| 235 |
+
}
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-4.c
ADDED
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdio.h>
|
| 2 |
+
#include <stdlib.h>
|
| 3 |
+
#include <time.h>
|
| 4 |
+
|
| 5 |
+
void fizzBuzz() {
|
| 6 |
+
int n, i = 0;
|
| 7 |
+
|
| 8 |
+
printf("Please enter an Integer\n");
|
| 9 |
+
scanf_s("%d", &n);
|
| 10 |
+
for (i = 1; i <= n; i++) {
|
| 11 |
+
if (i % 15 == 0) {
|
| 12 |
+
printf("FizBuzz, ");
|
| 13 |
+
}
|
| 14 |
+
else if (i % 5 == 0) {
|
| 15 |
+
printf("Buzz, ");
|
| 16 |
+
}
|
| 17 |
+
else if (i % 3 == 0) {
|
| 18 |
+
printf("Fizz, ");
|
| 19 |
+
}
|
| 20 |
+
else {
|
| 21 |
+
printf("%d, ", i);
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
void range() {
|
| 27 |
+
int i, min = 0, max = 0, n = 0;
|
| 28 |
+
printf("Please enter an Integer (0-999)\n");
|
| 29 |
+
scanf_s("%d", &n);
|
| 30 |
+
if (n >= 0 && n < 1000) {
|
| 31 |
+
|
| 32 |
+
for (i = 0; i < 50; i++) {
|
| 33 |
+
min = i * 25;
|
| 34 |
+
max = (i + 1) * 25;
|
| 35 |
+
if (n >= min && n < max) {
|
| 36 |
+
printf("%d is between %d-%d", n, min, max);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
else {
|
| 42 |
+
printf("Enter a valid a number");
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
void change() {
|
| 47 |
+
int n = 0, twenty = 0, ten = 0, five = 0, two = 0, one = 0;
|
| 48 |
+
printf("Please enter an Amount\n");
|
| 49 |
+
scanf_s("%d", &n);
|
| 50 |
+
while (n > 0) {
|
| 51 |
+
while (n % 20 != n) {
|
| 52 |
+
twenty++;
|
| 53 |
+
n -= 20;
|
| 54 |
+
|
| 55 |
+
}
|
| 56 |
+
while (n % 10 != n) {
|
| 57 |
+
ten++;
|
| 58 |
+
n -= 10;
|
| 59 |
+
}
|
| 60 |
+
while (n % 5 != n) {
|
| 61 |
+
five++;
|
| 62 |
+
n -= 5;
|
| 63 |
+
}
|
| 64 |
+
while (n % 2 != n) {
|
| 65 |
+
two++;
|
| 66 |
+
n -= 2;
|
| 67 |
+
}
|
| 68 |
+
while (n % 1 != n) {
|
| 69 |
+
one++;
|
| 70 |
+
n -= 1;
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
printf("Twenty:%d Ten:%d Five:%d Two:%d One:%d\n", twenty, ten, five, two, one);
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
oddNotDivisableBy3() {
|
| 77 |
+
int n = 0, i = 0, num = 0, sum = 0;
|
| 78 |
+
printf("Please enter a number:\n");
|
| 79 |
+
scanf_s("%d", &n);
|
| 80 |
+
for (i = 0; i < n; i++)
|
| 81 |
+
{
|
| 82 |
+
printf("Enter value:\n");
|
| 83 |
+
scanf_s("%d", &num);
|
| 84 |
+
sum += (num % 2 != 0 && num % 3 != 0) ? num : 0;
|
| 85 |
+
}
|
| 86 |
+
printf("The sum is: %d", sum);
|
| 87 |
+
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
int calc() {
|
| 91 |
+
int n = 0, i = 0, sumAddition = 0, sumMultiply = 1;
|
| 92 |
+
double result = 0;
|
| 93 |
+
printf("Please enter a number:\n");
|
| 94 |
+
scanf_s("%d", &n);
|
| 95 |
+
|
| 96 |
+
for (i = 1; i <= n; i++)
|
| 97 |
+
{
|
| 98 |
+
sumAddition += i;
|
| 99 |
+
sumMultiply *= i;
|
| 100 |
+
}
|
| 101 |
+
result = (double)sumMultiply / sumAddition;
|
| 102 |
+
printf("The result is:%lf", result);
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
void printNumbers() {
|
| 106 |
+
printf("Please enter numbers: (-1 to stop)\n");
|
| 107 |
+
int n = 0, amount = 0, min = 10000, max = -10000;
|
| 108 |
+
while (n != -1) {
|
| 109 |
+
scanf_s("%d", &n);
|
| 110 |
+
min = (n < min && n != -1) ? n : min;
|
| 111 |
+
max = (n > max && n != -1) ? n : max;
|
| 112 |
+
if (n != -1)amount++;
|
| 113 |
+
}
|
| 114 |
+
if (amount == 0) {
|
| 115 |
+
printf("0 numbers, no max nor min");
|
| 116 |
+
}
|
| 117 |
+
else {
|
| 118 |
+
printf("%d numbers, max = %d, min = %d", amount, max, min);
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
void decimalToBinary() {
|
| 123 |
+
// Variables:
|
| 124 |
+
// n - input number
|
| 125 |
+
// temp - temporary variable to preserve the input number
|
| 126 |
+
// msb - most significant bit position
|
| 127 |
+
// lsb - least significant bit position (always 0 in binary)
|
| 128 |
+
// i - loop variable
|
| 129 |
+
int n = 0, i, temp, msb = 0, lsb = 0;
|
| 130 |
+
|
| 131 |
+
// Prompt the user to enter a number
|
| 132 |
+
printf("Enter number\n");
|
| 133 |
+
scanf_s("%d", &n);
|
| 134 |
+
|
| 135 |
+
// Preserve the original number
|
| 136 |
+
temp = n;
|
| 137 |
+
|
| 138 |
+
// Find the position of the most significant bit (MSB)
|
| 139 |
+
while (n > 0) {
|
| 140 |
+
// 12,
|
| 141 |
+
n /= 2; // Divide by 2 to shift right
|
| 142 |
+
msb++; // Increment MSB count
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
// Iterate from the MSB to the LSB to print binary digits
|
| 146 |
+
for (i = msb - 1; i >= lsb; i--) {
|
| 147 |
+
// Extract the bit at position i
|
| 148 |
+
if ((temp >> i) % 2 != 0) {
|
| 149 |
+
// If the bit is 1 (result is odd), print 1
|
| 150 |
+
printf("1, ");
|
| 151 |
+
}
|
| 152 |
+
else {
|
| 153 |
+
// If the bit is 0 (result is even), print 0
|
| 154 |
+
printf("0, ");
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
void sumN() {
|
| 160 |
+
int n = 0, i = 0;
|
| 161 |
+
double a1 = 0, q = 1;
|
| 162 |
+
double aN = 0, s = 0, qN = 1, qNminusOne = 1;
|
| 163 |
+
printf("Please enter: a1 (double), q (double), n (int):\n");
|
| 164 |
+
scanf_s("%lf%lf%d", &a1, &q, &n);
|
| 165 |
+
printf("%lf %lf %d\n", a1, q, n);
|
| 166 |
+
for (i = 1; i <= n; i++)
|
| 167 |
+
{
|
| 168 |
+
qN *= q;
|
| 169 |
+
if (i != n)
|
| 170 |
+
qNminusOne *= q;
|
| 171 |
+
}
|
| 172 |
+
aN = a1 * qNminusOne;
|
| 173 |
+
s = (double)(a1 * (qN - 1)) / (q - 1);
|
| 174 |
+
printf("A%d = %.1lf,S = %.1lf", n, aN, s);
|
| 175 |
+
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
void sumOdd() {
|
| 179 |
+
int n = 0;
|
| 180 |
+
int upper = 2, lower = 3;
|
| 181 |
+
double s = 1;
|
| 182 |
+
printf("Please enter a number:\n");
|
| 183 |
+
scanf_s("%d", &n);
|
| 184 |
+
|
| 185 |
+
if (n % 2 == 0) {
|
| 186 |
+
printf("Enter an odd number\n");
|
| 187 |
+
return;
|
| 188 |
+
}
|
| 189 |
+
while (lower <= n) {
|
| 190 |
+
s += (double)upper / lower;
|
| 191 |
+
upper += 2;
|
| 192 |
+
lower += 2;
|
| 193 |
+
}
|
| 194 |
+
printf("%lf", s);
|
| 195 |
+
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
void printPositions() {
|
| 199 |
+
int n = 0, min = 10, max = -10, minP = -1, maxP = -1;
|
| 200 |
+
int i = 0, num = 0;
|
| 201 |
+
printf("Please enter a number greater than zero:\n");
|
| 202 |
+
scanf_s("%d", &n);
|
| 203 |
+
while (n > 0) {
|
| 204 |
+
num = n % 10;
|
| 205 |
+
if (num < min) {
|
| 206 |
+
min = num;
|
| 207 |
+
minP = i;
|
| 208 |
+
}
|
| 209 |
+
if (num == min) {
|
| 210 |
+
if (i > minP) {
|
| 211 |
+
min = num;
|
| 212 |
+
minP = i;
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
if (num > max) {
|
| 216 |
+
max = num;
|
| 217 |
+
maxP = i;
|
| 218 |
+
}
|
| 219 |
+
if (num == max) {
|
| 220 |
+
if (i > maxP) {
|
| 221 |
+
max = num;
|
| 222 |
+
maxP = i;
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
i++;
|
| 226 |
+
n /= 10;
|
| 227 |
+
}
|
| 228 |
+
printf("Max = %d, at position:%d, Min = %d, at position: %d", max, maxP, min, minP);
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
void printPositionsRight() {
|
| 232 |
+
int n = 0, min = 10, max = -10, minP = 100, maxP = 100;
|
| 233 |
+
int i = 0, num = 0;
|
| 234 |
+
printf("Please enter a number greater than zero:\n");
|
| 235 |
+
scanf_s("%d", &n);
|
| 236 |
+
while (n > 0) {
|
| 237 |
+
num = n % 10;
|
| 238 |
+
if (num < min) {
|
| 239 |
+
min = num;
|
| 240 |
+
minP = i;
|
| 241 |
+
}
|
| 242 |
+
if (num == min) {
|
| 243 |
+
if (i < minP) {
|
| 244 |
+
min = num;
|
| 245 |
+
minP = i;
|
| 246 |
+
}
|
| 247 |
+
}
|
| 248 |
+
if (num > max) {
|
| 249 |
+
max = num;
|
| 250 |
+
maxP = i;
|
| 251 |
+
}
|
| 252 |
+
if (num == max) {
|
| 253 |
+
if (i < maxP) {
|
| 254 |
+
max = num;
|
| 255 |
+
maxP = i;
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
i++;
|
| 259 |
+
n /= 10;
|
| 260 |
+
}
|
| 261 |
+
printf("Max = %d, at position:%d, Min = %d, at position: %d", max, maxP, min, minP);
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
void calcEvenOdd() {
|
| 265 |
+
int n = 0;
|
| 266 |
+
int evenSum = 1, oddSum = 1;
|
| 267 |
+
int num = 0;
|
| 268 |
+
double result = 0;
|
| 269 |
+
printf("Please enter a number greater than zero:\n");
|
| 270 |
+
scanf_s("%d", &n);
|
| 271 |
+
while (n > 0) {
|
| 272 |
+
num = n % 10;
|
| 273 |
+
if (num % 2 == 0) {
|
| 274 |
+
evenSum *= num;
|
| 275 |
+
}
|
| 276 |
+
else {
|
| 277 |
+
oddSum *= num;
|
| 278 |
+
}
|
| 279 |
+
n /= 10;
|
| 280 |
+
}
|
| 281 |
+
result = (double)evenSum / oddSum;
|
| 282 |
+
printf("%lf\n",result);
|
| 283 |
+
printf("%.2lf",result);
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
void guessingGame() {
|
| 287 |
+
srand(time(NULL)); // Seed the random number generator with the current time
|
| 288 |
+
int answer = 0, guess = 0, count = 1;
|
| 289 |
+
|
| 290 |
+
// Game instructions
|
| 291 |
+
printf("--- Welcome to the Guessing game! ---\n");
|
| 292 |
+
printf("I've picked a random number from 1-10\nLets see if you can guess it...\n");
|
| 293 |
+
|
| 294 |
+
// Generate a random number between 1 and 10
|
| 295 |
+
answer = (rand() % 10) + 1;
|
| 296 |
+
|
| 297 |
+
// Loop until the user guesses the correct number
|
| 298 |
+
while (guess != answer) {
|
| 299 |
+
printf("Count: %d, Guess:\n", count); // Prompt user for their guess
|
| 300 |
+
scanf_s("%d", &guess);
|
| 301 |
+
|
| 302 |
+
if (guess == answer) {
|
| 303 |
+
// Exit loop if the guess is correct
|
| 304 |
+
break;
|
| 305 |
+
}
|
| 306 |
+
else {
|
| 307 |
+
// Increment count for each incorrect guess
|
| 308 |
+
count++;
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
// Determine the feedback based on the number of guesses
|
| 313 |
+
switch (count) {
|
| 314 |
+
case 1: // Guessed on the first try
|
| 315 |
+
printf("I believe you have cheated...\n");
|
| 316 |
+
break;
|
| 317 |
+
case 2:
|
| 318 |
+
case 3: // Guessed within 2-3 tries
|
| 319 |
+
printf("Excellent game!\n");
|
| 320 |
+
break;
|
| 321 |
+
case 4:
|
| 322 |
+
case 5: // Guessed within 4-5 tries
|
| 323 |
+
printf("You Played well!\n");
|
| 324 |
+
break;
|
| 325 |
+
case 6:
|
| 326 |
+
case 7: // Guessed within 6-7 tries
|
| 327 |
+
printf("Average game.\n");
|
| 328 |
+
break;
|
| 329 |
+
case 8:
|
| 330 |
+
case 9:
|
| 331 |
+
case 10: // Guessed within 8-10 tries
|
| 332 |
+
printf("Poor game\n");
|
| 333 |
+
break;
|
| 334 |
+
default: // Took more than 10 tries
|
| 335 |
+
printf("Seriously? There are only ten choices...\n");
|
| 336 |
+
break;
|
| 337 |
+
}
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
void main()
|
| 342 |
+
{
|
| 343 |
+
//fizzBuzz();
|
| 344 |
+
//range();
|
| 345 |
+
//change();
|
| 346 |
+
//oddNotDivisableBy3();
|
| 347 |
+
//calc();
|
| 348 |
+
//printNumbers();
|
| 349 |
+
//decimalToBinary();
|
| 350 |
+
//sumN();
|
| 351 |
+
//sumOdd();
|
| 352 |
+
//printPositions();
|
| 353 |
+
//printPositionsRight();
|
| 354 |
+
//calcEvenOdd();
|
| 355 |
+
//guessingGame();
|
| 356 |
+
}
|
| 357 |
+
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-5.c
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdio.h>
|
| 2 |
+
#include <math.h>
|
| 3 |
+
|
| 4 |
+
int calculator()
|
| 5 |
+
{
|
| 6 |
+
char op = ' ';
|
| 7 |
+
int num1, num2;
|
| 8 |
+
|
| 9 |
+
printf("Please enter two integers:\n");
|
| 10 |
+
|
| 11 |
+
// Attempt to read two integers
|
| 12 |
+
if (scanf_s("%d %d", &num1, &num2) != 2) {
|
| 13 |
+
printf("Invalid input. Please enter integers only.\n");
|
| 14 |
+
return 0;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
// Clear white-space
|
| 18 |
+
while (getchar() != '\n');
|
| 19 |
+
|
| 20 |
+
printf("Please enter operator (+ , - , * , / , %%):\n");
|
| 21 |
+
|
| 22 |
+
if (scanf_s(" %c", &op, 1) != 1) { // The space before %c ignores any leftover whitespace
|
| 23 |
+
printf("Invalid input. Please enter a valid operator (+ , - , * , / , %%):\n");
|
| 24 |
+
return 0;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
printf("%d %c %d\n", num1, op, num2);
|
| 29 |
+
|
| 30 |
+
switch (op) {
|
| 31 |
+
case '+':
|
| 32 |
+
return num1 + num2;
|
| 33 |
+
case '-':
|
| 34 |
+
return num1 - num2;
|
| 35 |
+
case '*':
|
| 36 |
+
return num1 * num2;
|
| 37 |
+
case '/':
|
| 38 |
+
case '%':
|
| 39 |
+
if (num2 == 0) {
|
| 40 |
+
printf("Can't divide by zero\n");
|
| 41 |
+
return 0;
|
| 42 |
+
}
|
| 43 |
+
if (op == '/')
|
| 44 |
+
return num1 / num2;
|
| 45 |
+
else
|
| 46 |
+
return num1 % num2;
|
| 47 |
+
|
| 48 |
+
default:
|
| 49 |
+
printf("Error: Invalid operator. Please enter a valid operator (+ , - , * , / , %%).\n");
|
| 50 |
+
break;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
return 0;
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
int reverse(int n) {
|
| 59 |
+
int reversed = 0;
|
| 60 |
+
while (n != 0) {
|
| 61 |
+
reversed *= 10;
|
| 62 |
+
reversed += n % 10;
|
| 63 |
+
n /= 10;
|
| 64 |
+
}
|
| 65 |
+
return reversed;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
int divisors(int num) {
|
| 69 |
+
int sum = 0;
|
| 70 |
+
int d = 1;
|
| 71 |
+
|
| 72 |
+
while (d < num) {
|
| 73 |
+
sum += (num % d == 0) ? d : 0;
|
| 74 |
+
d++;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
return sum;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
int prime(int num) {
|
| 81 |
+
int d = 3;
|
| 82 |
+
// check if less than or equal 1, not 2 and not divisable by 2
|
| 83 |
+
if (num <= 1 || (num != 2 && num % 2 == 0))
|
| 84 |
+
return 0;
|
| 85 |
+
|
| 86 |
+
while (d < num) {
|
| 87 |
+
if (num % d == 0) return 0;
|
| 88 |
+
d++;
|
| 89 |
+
}
|
| 90 |
+
return 1;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
int gcd(int num1, int num2) {
|
| 94 |
+
while (num2 != 0) {
|
| 95 |
+
int temp = num2;
|
| 96 |
+
num2 = num1 % num2;
|
| 97 |
+
num1 = temp;
|
| 98 |
+
}
|
| 99 |
+
return num1;
|
| 100 |
+
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
int gcdTri(int num1, int num2, int num3) {
|
| 104 |
+
|
| 105 |
+
int n1 = gcd(num1, num2);
|
| 106 |
+
int n2 = gcd(n1, num3);
|
| 107 |
+
|
| 108 |
+
return n2;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
int main()
|
| 112 |
+
{
|
| 113 |
+
|
| 114 |
+
//int result = calculator();
|
| 115 |
+
//printf("Result: %d\n", result);
|
| 116 |
+
|
| 117 |
+
//int result1 = reverse(5614);
|
| 118 |
+
//printf("Result: %d\n", result1);
|
| 119 |
+
|
| 120 |
+
//int result2 = reverse(320);
|
| 121 |
+
//printf("Result: %d\n", result2);
|
| 122 |
+
|
| 123 |
+
//int result3 = divisors(12);
|
| 124 |
+
//printf("Result: %d\n", result3);
|
| 125 |
+
|
| 126 |
+
//int result4 = divisors(16);
|
| 127 |
+
//printf("Result: %d\n", result4);
|
| 128 |
+
|
| 129 |
+
//int result5 = prime(919);
|
| 130 |
+
//printf("Result: %d\n", result5);
|
| 131 |
+
|
| 132 |
+
int result6 = gcdTri(15, 20, 100);
|
| 133 |
+
printf("Result: %d\n", result6);
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
return 0;
|
| 137 |
+
}
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-6.c
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdio.h>
|
| 2 |
+
|
| 3 |
+
int hasEven(int num) {
|
| 4 |
+
// base case
|
| 5 |
+
if (num % 10 == 0) return 0;
|
| 6 |
+
if ((num % 10) % 2 == 0) return 1;
|
| 7 |
+
|
| 8 |
+
return hasEven(num / 10);
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
int fibo(int n) {
|
| 12 |
+
|
| 13 |
+
// base case
|
| 14 |
+
if (n == 0) return 0;
|
| 15 |
+
if (n == 1) return 1;
|
| 16 |
+
|
| 17 |
+
return (fibo(n - 1) + fibo(n - 2));
|
| 18 |
+
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
int fibo1(int n) {
|
| 22 |
+
|
| 23 |
+
if (n == 0) return 0;
|
| 24 |
+
if (n == 1) return 1;
|
| 25 |
+
int num1 = 1, num2 = 1;
|
| 26 |
+
int i = 2;
|
| 27 |
+
int fib = 0;
|
| 28 |
+
|
| 29 |
+
while (i != n) {
|
| 30 |
+
fib = num1 + num2;
|
| 31 |
+
num1 = num2;
|
| 32 |
+
num2 = fib;
|
| 33 |
+
i++;
|
| 34 |
+
}
|
| 35 |
+
return fib;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
int digits(int num) {
|
| 39 |
+
if (num == 0) return 0;
|
| 40 |
+
return digits(num / 10) + 1;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
int sum(int num) {
|
| 44 |
+
if (num == 0) return 0;
|
| 45 |
+
return sum(num / 10) + num % 10;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
int ascending(int num) {
|
| 49 |
+
if (num < 9) return 1;
|
| 50 |
+
|
| 51 |
+
return (num % 10 < (num / 10) % 10) ? ascending(num / 10) : 0;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
int smallerThan(int num) {
|
| 55 |
+
|
| 56 |
+
int input;
|
| 57 |
+
printf("Please enter an integer:\n");
|
| 58 |
+
scanf_s("%d", &input);
|
| 59 |
+
|
| 60 |
+
// base case:
|
| 61 |
+
if (input == -1) return 0;
|
| 62 |
+
|
| 63 |
+
if (input < num) {
|
| 64 |
+
return 1 + smallerThan(num);
|
| 65 |
+
}
|
| 66 |
+
else {
|
| 67 |
+
return smallerThan(num);
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
void printFromNumDown(int num) {
|
| 72 |
+
if (num == 0) {
|
| 73 |
+
printf(".");
|
| 74 |
+
return;
|
| 75 |
+
}
|
| 76 |
+
printf("%d ", num);
|
| 77 |
+
return printFromNumDown(num - 1);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
void printFromOneToNum(int num) {
|
| 81 |
+
if (num == 1) {
|
| 82 |
+
printf("%d ", num);
|
| 83 |
+
return;
|
| 84 |
+
}
|
| 85 |
+
printFromOneToNum(num - 1);
|
| 86 |
+
|
| 87 |
+
printf("%d ", num);
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
void printFromNumTo1ToNum(int num) {
|
| 91 |
+
if (num == 1) {
|
| 92 |
+
printf("%d ", num);
|
| 93 |
+
return;
|
| 94 |
+
}
|
| 95 |
+
printf("%d ", num);
|
| 96 |
+
printFromNumTo1ToNum(num - 1);
|
| 97 |
+
printf("%d ", num);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
int main()
|
| 101 |
+
{
|
| 102 |
+
//printf("%d", hasEven(13547));
|
| 103 |
+
//printf("%d", fibo1(19)); // 4181
|
| 104 |
+
//printf("%d", digits(123456789)); // 9
|
| 105 |
+
//printf("%d", sum(123)); // 6
|
| 106 |
+
//printf("%d", ascending(123456)); // 0
|
| 107 |
+
//printf("%d", ascending(321)); // 1
|
| 108 |
+
//printf("%d", smallerThan(5));
|
| 109 |
+
//printFromNumDown(5);
|
| 110 |
+
//printFromOneToNum(5);
|
| 111 |
+
//printFromNumTo1ToNum(5);
|
| 112 |
+
|
| 113 |
+
return 0;
|
| 114 |
+
}
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-7.c
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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8 |
+
int i = 0;
|
| 9 |
+
while (i < n) {
|
| 10 |
+
scanf_s("%d", &arr[i]);
|
| 11 |
+
i++;
|
| 12 |
+
}
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
void printArray(int* arr, int n) {
|
| 16 |
+
printf("\nPrinting Array\n");
|
| 17 |
+
int i = 0;
|
| 18 |
+
while (i < n) {
|
| 19 |
+
printf("%d ", arr[i]);
|
| 20 |
+
i++;
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
void arrCalc(int* arr, int n, int* max, int* min, int* sum, double* avg, int* isEven) {
|
| 25 |
+
int i = 0;
|
| 26 |
+
*max = arr[0];
|
| 27 |
+
*min = arr[0];
|
| 28 |
+
*isEven = 0;
|
| 29 |
+
while (i < n) {
|
| 30 |
+
*sum += arr[i];
|
| 31 |
+
*max = (arr[i] > *max) ? arr[i] : *max;
|
| 32 |
+
*min = (arr[i] < *min) ? arr[i] : *min;
|
| 33 |
+
*isEven = (arr[i] % 2 == 0) ? 1 : *isEven;
|
| 34 |
+
i++;
|
| 35 |
+
}
|
| 36 |
+
*avg = (double)(*sum / n);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
int ascending(int* arr, int n, int* isReallyAscending) {
|
| 40 |
+
int i = 0;
|
| 41 |
+
int isAscending = 1;
|
| 42 |
+
*isReallyAscending = 1;
|
| 43 |
+
while (i < n - 1) {
|
| 44 |
+
if (arr[i] > arr[i + 1]) {
|
| 45 |
+
isAscending = 0;
|
| 46 |
+
*isReallyAscending = 0;
|
| 47 |
+
return isAscending;
|
| 48 |
+
}
|
| 49 |
+
if (arr[i] == arr[i + 1]) {
|
| 50 |
+
*isReallyAscending = 0;
|
| 51 |
+
}
|
| 52 |
+
i++;
|
| 53 |
+
}
|
| 54 |
+
return isAscending;
|
| 55 |
+
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
int ascendingR(int* arr, int n, int* isReallyAscending) {
|
| 59 |
+
if (n == 1 || n == 0) {
|
| 60 |
+
*isReallyAscending = 1;
|
| 61 |
+
return 1;
|
| 62 |
+
}
|
| 63 |
+
if (arr[0] > arr[1]) {
|
| 64 |
+
*isReallyAscending = 0;
|
| 65 |
+
return 0;
|
| 66 |
+
}
|
| 67 |
+
if (arr[0] == arr[1]) {
|
| 68 |
+
*isReallyAscending = 0;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
return ascending(arr + 1, n - 1, isReallyAscending);
|
| 72 |
+
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
int sumNighborArr(int* arr, int n) {
|
| 76 |
+
int i = 1;
|
| 77 |
+
while (i < n - 1) {
|
| 78 |
+
if (arr[i - 1] + arr[i + 1] == arr[i]) {
|
| 79 |
+
return 1;
|
| 80 |
+
}
|
| 81 |
+
i++;
|
| 82 |
+
}
|
| 83 |
+
return 0;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
void reverse(int* arr, int n) {
|
| 87 |
+
int i, j;
|
| 88 |
+
int temp = 0;
|
| 89 |
+
for (i = 0, j = n - 1; i < n / 2; i++, j--)
|
| 90 |
+
{
|
| 91 |
+
temp = arr[i];
|
| 92 |
+
arr[i] = arr[j];
|
| 93 |
+
arr[j] = temp;
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
void reverseR(int* arr, int n) {
|
| 98 |
+
// Base case: if the size of the array is 1 or less, stop recursion
|
| 99 |
+
if (n <= 1) return;
|
| 100 |
+
|
| 101 |
+
// Swap the first element (arr[0]) with the last element (arr[n-1])
|
| 102 |
+
int temp = arr[0];
|
| 103 |
+
arr[0] = arr[n - 1];
|
| 104 |
+
arr[n - 1] = temp;
|
| 105 |
+
|
| 106 |
+
// Recursively call reverseR on the inner portion of the array
|
| 107 |
+
// Move the array pointer one step forward (arr + 1)
|
| 108 |
+
// Reduce the size of the problem by 2 (n - 2)
|
| 109 |
+
reverseR(arr + 1, n - 2);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
int main()
|
| 114 |
+
{
|
| 115 |
+
|
| 116 |
+
//Question 1
|
| 117 |
+
//int arr[100];
|
| 118 |
+
//int n = 5;
|
| 119 |
+
//inputArray(&arr, n);
|
| 120 |
+
//printArray(&arr, n);
|
| 121 |
+
|
| 122 |
+
//Question 2
|
| 123 |
+
//int arr[] = { 5, 8, 2, 3, 7 };
|
| 124 |
+
//int n = 5;
|
| 125 |
+
//int max = 0, min = 0, sum = 0, isEven = 0;
|
| 126 |
+
//double avg;
|
| 127 |
+
|
| 128 |
+
//// Call the function
|
| 129 |
+
//arrCalc(arr, n, &max, &min, &sum, &avg, &isEven);
|
| 130 |
+
|
| 131 |
+
//// Print the results
|
| 132 |
+
//printf("Max: %d\n", max);
|
| 133 |
+
//printf("Min: %d\n", min);
|
| 134 |
+
//printf("Sum: %d\n", sum);
|
| 135 |
+
//printf("Average: %.2f\n", avg);
|
| 136 |
+
//printf("Is there an even number? %d\n", isEven);
|
| 137 |
+
|
| 138 |
+
// Question 3: Test ascending (iterative) and ascendingR (recursive)
|
| 139 |
+
//int arr1[] = { 1, 3, 5, 6, 11 };
|
| 140 |
+
//int arr2[] = { 1, 3, 3, 6, 11 };
|
| 141 |
+
//int arr3[] = { 1, 3, 2, 5, 11 };
|
| 142 |
+
//int isReallyAscending;
|
| 143 |
+
|
| 144 |
+
//// Test with iterative function
|
| 145 |
+
//printf("Testing iterative ascending function:\n");
|
| 146 |
+
//isReallyAscending = 0;
|
| 147 |
+
//printf("Array 1: Ascending: %d, Really Ascending: %d\n", ascending(arr1, 5, &isReallyAscending), isReallyAscending);
|
| 148 |
+
|
| 149 |
+
//isReallyAscending = 0;
|
| 150 |
+
//printf("Array 2: Ascending: %d, Really Ascending: %d\n", ascending(arr2, 5, &isReallyAscending), isReallyAscending);
|
| 151 |
+
|
| 152 |
+
//isReallyAscending = 0;
|
| 153 |
+
//printf("Array 3: Ascending: %d, Really Ascending: %d\n", ascending(arr3, 5, &isReallyAscending), isReallyAscending);
|
| 154 |
+
|
| 155 |
+
//// Test with recursive function
|
| 156 |
+
//printf("\nTesting recursive ascending function:\n");
|
| 157 |
+
//isReallyAscending = 0;
|
| 158 |
+
//printf("Array 1: Ascending: %d, Really Ascending: %d\n", ascendingR(arr1, 5, &isReallyAscending), isReallyAscending);
|
| 159 |
+
|
| 160 |
+
//isReallyAscending = 0;
|
| 161 |
+
//printf("Array 2: Ascending: %d, Really Ascending: %d\n", ascendingR(arr2, 5, &isReallyAscending), isReallyAscending);
|
| 162 |
+
|
| 163 |
+
//isReallyAscending = 0;
|
| 164 |
+
//printf("Array 3: Ascending: %d, Really Ascending: %d\n", ascendingR(arr3, 5, &isReallyAscending), isReallyAscending);
|
| 165 |
+
|
| 166 |
+
// Question 4
|
| 167 |
+
//int arr1[] = { 1,2,7,5,9,6 };
|
| 168 |
+
//int arr2[] = { 1,2,8,5,9,6 };
|
| 169 |
+
//int n = 6;
|
| 170 |
+
//printf("Array1: %d\n",sumNighborArr(arr1, n));
|
| 171 |
+
//printf("Array2: %d\n",sumNighborArr(arr2, n));
|
| 172 |
+
|
| 173 |
+
// Question 5
|
| 174 |
+
// A
|
| 175 |
+
//int arr1[] = { 1,3,2,5,6};
|
| 176 |
+
//int n = 5;
|
| 177 |
+
//reverse(arr1,n);
|
| 178 |
+
//for (int i = 0; i < n; i++)
|
| 179 |
+
//{
|
| 180 |
+
// printf("%d,",arr1[i]);
|
| 181 |
+
//}
|
| 182 |
+
// B
|
| 183 |
+
int arr1[] = { 1,3,2,5,6 };
|
| 184 |
+
int n = 5;
|
| 185 |
+
reverseR(arr1, n);
|
| 186 |
+
for (int i = 0; i < n; i++)
|
| 187 |
+
{
|
| 188 |
+
printf("%d,", arr1[i]);
|
| 189 |
+
}
|
| 190 |
+
return 0;
|
| 191 |
+
}
|
| 192 |
+
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-8.c
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdio.h>
|
| 2 |
+
#include <stdlib.h>
|
| 3 |
+
|
| 4 |
+
int* inputArray(int size) {
|
| 5 |
+
int* arr = (int*)malloc(size * sizeof(int));
|
| 6 |
+
if (arr == NULL) {
|
| 7 |
+
printf("Memomry allocation falied.\n");
|
| 8 |
+
return;
|
| 9 |
+
}
|
| 10 |
+
int* start = arr;
|
| 11 |
+
int i;
|
| 12 |
+
for (i = 0; i < size; i++) {
|
| 13 |
+
printf("Enter number for element %d:\n", i + 1);;
|
| 14 |
+
scanf_s("%d", &arr[i]);
|
| 15 |
+
}
|
| 16 |
+
return start;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
int* fibo(int n) {
|
| 20 |
+
// allocate memory
|
| 21 |
+
int* arr = (int*)malloc((n + 1) * sizeof(int));
|
| 22 |
+
if (arr == NULL) {
|
| 23 |
+
printf("Memomry allocation falied.\n");
|
| 24 |
+
return;
|
| 25 |
+
}
|
| 26 |
+
// base case
|
| 27 |
+
if (n == 0) {
|
| 28 |
+
arr[0] = 0;
|
| 29 |
+
return arr;
|
| 30 |
+
}
|
| 31 |
+
if (n == 1) {
|
| 32 |
+
arr[0] = 0;
|
| 33 |
+
arr[1] = 1;
|
| 34 |
+
return arr;
|
| 35 |
+
}
|
| 36 |
+
// Initalize the first two elemnts
|
| 37 |
+
arr[0] = 0;
|
| 38 |
+
arr[1] = 1;
|
| 39 |
+
|
| 40 |
+
for (int i = 2; i <= n; i++)
|
| 41 |
+
arr[i] = arr[i - 2] + arr[i - 1];
|
| 42 |
+
return arr;
|
| 43 |
+
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
int appearsMost(int* arr, int n) {
|
| 47 |
+
// allocate memory
|
| 48 |
+
int arrCount[] = { 0,0,0,0,0,0,0,0,0 };
|
| 49 |
+
int max = -1, maxLoc = -1;
|
| 50 |
+
if (arrCount == NULL) {
|
| 51 |
+
printf("Memomry allocation falied.\n");
|
| 52 |
+
return;
|
| 53 |
+
}
|
| 54 |
+
// count the numbers
|
| 55 |
+
for (int i = 0; i < n; i++) {
|
| 56 |
+
arrCount[arr[i]] += 1;
|
| 57 |
+
}
|
| 58 |
+
// find max
|
| 59 |
+
|
| 60 |
+
for (int i = 0; i < n; i++)
|
| 61 |
+
{
|
| 62 |
+
if (arrCount[i] > max) {
|
| 63 |
+
max = arrCount[i];
|
| 64 |
+
maxLoc = i;
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
printf("The most frequent number is: %d and it appears: %d times", maxLoc, max);
|
| 69 |
+
|
| 70 |
+
return max;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
int appearsInArr(int* arr, int n, int k) {
|
| 74 |
+
int* count = (int*)calloc(k + 1, sizeof(int));
|
| 75 |
+
if (count == NULL) {
|
| 76 |
+
printf("Memomry allocation falied.\n");
|
| 77 |
+
return;
|
| 78 |
+
}
|
| 79 |
+
for (int i = 0; i < n; i++)
|
| 80 |
+
count[arr[i]] += 1;
|
| 81 |
+
|
| 82 |
+
for (int i = 0; i < k; i++)
|
| 83 |
+
{
|
| 84 |
+
if (count[i] != i)
|
| 85 |
+
return 0;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
free(count);
|
| 89 |
+
|
| 90 |
+
return 1;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
int diff(int* arr, int n, int a, int b) {
|
| 94 |
+
// Allocate memory for count array to store frequencies in range [a, b]
|
| 95 |
+
int* count = (int*)calloc((b - a) + 1, sizeof(int));
|
| 96 |
+
if (count == NULL) {
|
| 97 |
+
printf("Memory allocation failed.\n");
|
| 98 |
+
return -1; // Return an error code
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// Variables for tracking max and min frequencies
|
| 102 |
+
int i, max = 0, maxLoc = -1, min = n + 1, minLoc = -1;
|
| 103 |
+
|
| 104 |
+
// Count occurrences of elements in arr within the range [a, b]
|
| 105 |
+
for (i = 0; i < n; i++) {
|
| 106 |
+
if (arr[i] >= a && arr[i] <= b) { // Ensure arr[i] is within [a, b]
|
| 107 |
+
count[arr[i] - a]++; // Update count array at adjusted index
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// Iterate through count array to find max and min frequencies
|
| 112 |
+
for (i = 0; i < (b - a) + 1; i++) {
|
| 113 |
+
if (count[i] > 0) { // Only consider elements that appear at least once
|
| 114 |
+
// Update max if current frequency is greater or equal, and prioritize a higher value for maxLoc
|
| 115 |
+
if (count[i] > max || (count[i] == max && i + a > maxLoc)) {
|
| 116 |
+
max = count[i];
|
| 117 |
+
maxLoc = i + a; // Convert back to the original value
|
| 118 |
+
}
|
| 119 |
+
// Update min if current frequency is smaller or equal, and prioritize a lower value for minLoc
|
| 120 |
+
if (count[i] < min || (count[i] == min && i + a < minLoc)) {
|
| 121 |
+
min = count[i];
|
| 122 |
+
minLoc = i + a; // Convert back to the original value
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
free(count); // Free allocated memory
|
| 128 |
+
|
| 129 |
+
// Return the difference between maxLoc and minLoc
|
| 130 |
+
return maxLoc - minLoc;
|
| 131 |
+
}
|
| 132 |
+
int main() {
|
| 133 |
+
// 1
|
| 134 |
+
//int size = 5; // Define the size of the array
|
| 135 |
+
//int* ptr = inputArray(size);
|
| 136 |
+
|
| 137 |
+
//if (ptr != NULL) {
|
| 138 |
+
// printf("You entered the following numbers:\n");
|
| 139 |
+
// for (int i = 0; i < size; i++) {
|
| 140 |
+
// printf("%d ", ptr[i]); // Print the array elements
|
| 141 |
+
// }
|
| 142 |
+
// printf("\n");
|
| 143 |
+
|
| 144 |
+
// free(ptr); // Free the dynamically allocated memory
|
| 145 |
+
//}
|
| 146 |
+
|
| 147 |
+
// 2
|
| 148 |
+
//int n = 10;
|
| 149 |
+
//int* ptr = fibo(n);
|
| 150 |
+
|
| 151 |
+
//if (ptr != NULL) {
|
| 152 |
+
// printf("Fibonacci sequnce:\n");
|
| 153 |
+
// for (int i = 0; i <= n; i++) {
|
| 154 |
+
// printf("%d ", ptr[i]); // Print the array elements
|
| 155 |
+
// }
|
| 156 |
+
// printf("\n");
|
| 157 |
+
|
| 158 |
+
// free(ptr); // Free the dynamically allocated memory
|
| 159 |
+
//}
|
| 160 |
+
|
| 161 |
+
// 3
|
| 162 |
+
//int arr[] = { 1,6,3,3,3,6,3 };
|
| 163 |
+
//int n = 7;
|
| 164 |
+
//int res = appearsMost(arr,n);
|
| 165 |
+
|
| 166 |
+
// 4
|
| 167 |
+
//int arr[] = { 1,4,4,3,4,2,2,3,3,4 };
|
| 168 |
+
//int arr1[] = { 1,4,4,3,4,2,2,1,3,4 };
|
| 169 |
+
//int n = 10, k = 4;
|
| 170 |
+
//printf("%d\n", appearsInArr(arr, n, k));
|
| 171 |
+
//printf("%d\n", appearsInArr(arr1, n, k));
|
| 172 |
+
|
| 173 |
+
// 5
|
| 174 |
+
//int arr[] = { 5,5,1,4,4,4,7,7 };
|
| 175 |
+
//int arr1[] = { 5,1,6,6,9,9 };
|
| 176 |
+
//printf("%d\n", diff(arr, 8, 1, 8));
|
| 177 |
+
//printf("%d\n", diff(arr1, 6, 1, 9));
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
return 0;
|
| 181 |
+
}
|
שנה א/מבוא למדעי המחשב/עבודות בית/פתרונות לעבודות/מטלה-9.c
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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| 5 |
+
int charCount = 0;
|
| 6 |
+
char* start = s;
|
| 7 |
+
|
| 8 |
+
// Count the number of characters in the input string
|
| 9 |
+
while (*s) {
|
| 10 |
+
charCount++;
|
| 11 |
+
s++;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
// Allocate memory for the new string (doubled + null terminator)
|
| 15 |
+
char* newStr = (char*)malloc((charCount * 2 + 1) * sizeof(char));
|
| 16 |
+
if (newStr == NULL) {
|
| 17 |
+
printf("Memory allocation failed.\n");
|
| 18 |
+
return NULL; // Return NULL to indicate failure
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
char* newStart = newStr; // Preserve the original pointer for return
|
| 22 |
+
|
| 23 |
+
// Fill the first part of the new string
|
| 24 |
+
for (int i = 0; i < charCount; i++) {
|
| 25 |
+
*newStr = *start;
|
| 26 |
+
*(newStr + charCount) = *start; // Fill the second half
|
| 27 |
+
newStr++;
|
| 28 |
+
start++;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
newStr = newStart + (charCount * 2); // Move to the end of the allocated memory
|
| 32 |
+
*newStr = '\0'; // Null terminator
|
| 33 |
+
|
| 34 |
+
return newStart; // Return the original pointer
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
char appearsMost(char* s) {
|
| 38 |
+
// Step 1: Allocate memory for frequency array
|
| 39 |
+
int pCount[26] = { 0 }; // Array for counts of each letter, initialized to 0
|
| 40 |
+
char* ptr = s; // Pointer to iterate through the string
|
| 41 |
+
|
| 42 |
+
// Step 2: Count frequencies of lowercase letters
|
| 43 |
+
while (*ptr) {
|
| 44 |
+
char curChar = *ptr;
|
| 45 |
+
if (curChar >= 'a' && curChar <= 'z') { // Check if the character is lowercase
|
| 46 |
+
int index = curChar - 'a'; // Calculate index based on 'a'
|
| 47 |
+
pCount[index]++; // Increment the corresponding count
|
| 48 |
+
}
|
| 49 |
+
ptr++; // Move to the next character
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
// Step 3: Find the letter with the maximum frequency
|
| 53 |
+
int maxFreq = 0; // Variable to track the highest frequency
|
| 54 |
+
int maxIdx = -1; // Variable to track the index of the letter with max frequency
|
| 55 |
+
for (int i = 0; i < 26; i++) {
|
| 56 |
+
if (pCount[i] > maxFreq) { // Compare frequencies
|
| 57 |
+
maxFreq = pCount[i]; // Update max frequency
|
| 58 |
+
maxIdx = i; // Update the index of the most frequent letter
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// Step 4: Return the result
|
| 63 |
+
if (maxIdx == -1) {
|
| 64 |
+
return '\0'; // If no valid letter found, return '\0'
|
| 65 |
+
}
|
| 66 |
+
return (char)(maxIdx + 'a'); // Convert the index to the corresponding letter and return
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
int validString(char* s) {
|
| 70 |
+
char* ptr = s;
|
| 71 |
+
int flc = (*s >= 'A' && *s <= 'Z') ? 1 : 0; // First letter capitelized
|
| 72 |
+
|
| 73 |
+
if (flc) { // First letter is Capitalized
|
| 74 |
+
ptr++; // Move to second letter
|
| 75 |
+
int slc = (*ptr >= 'A' && *ptr <= 'Z') ? 1 : 0; // Seoncd letter capitelized
|
| 76 |
+
ptr++; // Move to third letter
|
| 77 |
+
if (slc) { // Second letter is Capitalized
|
| 78 |
+
while (*ptr != '\0') { // Check that all ther rest are also capitalized
|
| 79 |
+
if (*ptr != ' ' && *ptr <= 'z' && *ptr >= 'a')
|
| 80 |
+
return 0;
|
| 81 |
+
ptr++;
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
else {// Second letter is NOT Capitalized
|
| 85 |
+
while (*ptr != '\0') { // Check that all ther rest are also not capitalized
|
| 86 |
+
if (*ptr != ' ' && *ptr >= 'A' && *ptr <= 'Z')
|
| 87 |
+
return 0;
|
| 88 |
+
ptr++;
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
else { // first letter is not capitalized, check that the rest are also not capitalized
|
| 93 |
+
while (*ptr != '\0') {
|
| 94 |
+
if (*ptr != ' ' && *ptr >= 'A' && *ptr <= 'Z')
|
| 95 |
+
return 0;
|
| 96 |
+
ptr++;
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
return 1;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
char* removeSpaces(char* s) {
|
| 103 |
+
char* ptr = s; // Pointer to traverse the input string
|
| 104 |
+
int count = 0; // Counter to track the number of non-space characters
|
| 105 |
+
|
| 106 |
+
// Step 1: Count the number of non-space characters
|
| 107 |
+
while (*ptr != '\0') { // Loop until the end of the string
|
| 108 |
+
if (*ptr != ' ') // If the character is not a space
|
| 109 |
+
count++; // Increment the count
|
| 110 |
+
ptr++; // Move to the next character
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Step 2: Allocate memory for the new string
|
| 114 |
+
// Allocate enough space for all non-space characters plus the null terminator
|
| 115 |
+
char* str = malloc((count + 1) * sizeof(char));
|
| 116 |
+
ptr = s; // Reset the pointer to the beginning of the input string
|
| 117 |
+
|
| 118 |
+
if (str == NULL) { // Check if memory allocation failed
|
| 119 |
+
printf("Memory allocation failed.\n");
|
| 120 |
+
return NULL; // Return NULL to indicate failure
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// Step 3: Copy non-space characters to the new string
|
| 124 |
+
char* pStr = str; // Pointer to traverse the new string
|
| 125 |
+
while (*ptr != '\0') { // Loop until the end of the input string
|
| 126 |
+
if (*ptr != ' ') { // If the character is not a space
|
| 127 |
+
*pStr = *ptr; // Copy the character to the new string
|
| 128 |
+
pStr++; // Move to the next position in the new string
|
| 129 |
+
}
|
| 130 |
+
ptr++; // Move to the next character in the input string
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// Step 4: Add the null terminator to the end of the new string
|
| 134 |
+
*pStr = '\0';
|
| 135 |
+
|
| 136 |
+
// Step 5: Return the new string
|
| 137 |
+
return str;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
int palindrome(char* str) {
|
| 141 |
+
char* start = str, * end = NULL; // Two pointers: start points to the beginning of the string, end will point to the last character.
|
| 142 |
+
int count = 0; // Counter to calculate the length of the string.
|
| 143 |
+
|
| 144 |
+
// Step 1: Calculate the length of the string
|
| 145 |
+
while (*start != '\0') { // Traverse the string until the null terminator.
|
| 146 |
+
count++; // Increment the counter for each character.
|
| 147 |
+
start++; // Move to the next character.
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
start = str; // Reset start to point to the beginning of the string.
|
| 151 |
+
end = str + (count - 1); // Set end to point to the last character (count - 1 due to 0-based indexing).
|
| 152 |
+
|
| 153 |
+
// Step 2: Check for palindrome
|
| 154 |
+
while (start < end) { // Loop until the two pointers meet in the middle.
|
| 155 |
+
if (*start != *end) return 0; // If characters at start and end are not equal, it's not a palindrome.
|
| 156 |
+
start++; // Move start pointer forward.
|
| 157 |
+
end--; // Move end pointer backward.
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
return 1; // If the loop completes without finding mismatched characters, the string is a palindrome.
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
int main()
|
| 166 |
+
{
|
| 167 |
+
// 1
|
| 168 |
+
printf("%s\n", stringTwice("abcd"));
|
| 169 |
+
printf("%s\n", stringTwice("adir buskila"));
|
| 170 |
+
|
| 171 |
+
// 2
|
| 172 |
+
printf("%c\n", appearsMost("strings seem silly")); // Expected Output: 's'
|
| 173 |
+
printf("%c\n", appearsMost("abcabcabc")); // Expected Output: 'a'
|
| 174 |
+
printf("%c\n", appearsMost("12345!@#$%z")); // Expected Output: '\0'
|
| 175 |
+
|
| 176 |
+
// 3
|
| 177 |
+
printf("%d\n", validString("This is valid"));
|
| 178 |
+
printf("%d\n", validString("THIS IS VALID"));
|
| 179 |
+
printf("%d\n", validString("this is valid"));
|
| 180 |
+
printf("%d\n", validString("This IS NOT ValiD"));
|
| 181 |
+
|
| 182 |
+
// 4
|
| 183 |
+
printf("%s\n", removeSpaces("I li ve in isra el"));
|
| 184 |
+
printf("%s\n", removeSpaces(" H E LL O WORLD"));
|
| 185 |
+
printf("%s\n", removeSpaces(" "));
|
| 186 |
+
printf("%s\n", removeSpaces("A B C D"));
|
| 187 |
+
|
| 188 |
+
// 5
|
| 189 |
+
printf("%d\n", palindrome("tacocat"));
|
| 190 |
+
printf("%d\n", palindrome("racecar"));
|
| 191 |
+
printf("%d\n", palindrome("level"));
|
| 192 |
+
printf("%d\n", palindrome("rotor"));
|
| 193 |
+
printf("%d\n", palindrome("civic"));
|
| 194 |
+
printf("%d\n", palindrome("civic"));
|
| 195 |
+
printf("%d\n", palindrome("notpalindrome"));
|
| 196 |
+
printf("%d\n", palindrome("shoshi"));
|
| 197 |
+
return 0;
|
| 198 |
+
}
|
שנה א/מבוא למערכות מחשב/מבחנים/דף נוסחאות.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:691ada1aa80af6f00f8670f27a5f834023b1a9763591f288105169b879ff1900
|
| 3 |
+
size 2474177
|