הערה (קובץ קוד)
קובץ זה הוא קובץ קוד מסוג C.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define DONT_KNOW "I_dont_know"
void printIDontKnow()
{
printf("%s", DONT_KNOW);
exit(0);
}
int midterm_quiz_q3(int arr[], int n);
// Do not change read_arr!!!
int *read_arr(int *n)
{
if (scanf("%d", n) != 1)
return NULL;
int *arr = malloc((*n) * sizeof(int));
if (!arr)
return NULL;
for (int i = 0; i < *n; i++)
{
if (scanf("%d", arr + i) != 1)
{
free(arr);
return NULL;
}
}
return arr;
}
//Uncomment the first line in main if you don't know the answer.
//Otherwise, do not change main!!!
int main()
{
// printIDontKnow();
int n;
int *arr = read_arr(&n);
if (!arr)
return 1;
printf("%d", midterm_quiz_q3(arr, n));
free(arr);
return 0;
}
/*
* Gets a number and finds out if he is perfect or not.
* input:
* 1. int num: the number to be checked.
* output(bool):
* if the number is perfect, returns true.
*/
bool isPerfect(int num)
{
int sumOfDenominators = 0;
for (int i = 1; i < num; i++)
{
double temp = (double)num / i;
if (((int)temp) == temp)
{
sumOfDenominators += i;
}
}
return (sumOfDenominators == num);
}
/*
* This function gets an array of numbers and
* checks how many of them are perfect.
* input:
* 1. int arr[]: an array of numbers to be checked.
* 2. int n: the length of the array.
* output(int):
* how many numbers are perfect.
*/
int midterm_quiz_q3(int arr[], int n)
{
int numberOfPerfects = 0;
for (int i = 0; i < n; i++)
{
if (isPerfect(arr[i]))
{
numberOfPerfects++;
}
}
return numberOfPerfects;
}