c program 1. Create this routine as a function, sum_divisors() and call it from
ID: 3761389 • Letter: C
Question
c program
1. Create this routine as a function, sum_divisors() and call it from main. 2. By successively applying sum_divisors() you can compute the trajectory of the function starting with a particular value. For example, if we compute the trajectory of function on the value 1, we get 1 0 0 . . .since n is 1, sum_divisors(n) is 0, sum_divisor(sum_divisors(n)) is 0, etc. Clearly, once we see any value repeated in the sequence, we know the entire pattern. For 1, this happens after two calls to the function. 3. Prime numbers, which immediately reduce to 1 always, have a short trajectory. For example, from the value 43 we have: 43 1 0 0...which loops at 3 applications of sum_divisors(). 4. Perfect numbers repeat immediately 6..6…6…6 ...so only one application of sum_divisor() detects a cycle. 5. Enhance your program so that for each integer read, it prints the number of times sum_divisor() is called in order to detect a cycle. 6. The final program( the program that should be submitted) should allow the user to enter up to 10 integers. If the user enters: 23 -4 6 45 the output should be the following: 23, 1, 0, 0 3 cycles -4 , 0, 0 2 cycles 6, 6, 1 cycle 45, 33, 15, 9, 4, 3, 1, 0, 0 8 cycles If the user enters an invalid input (number greater than 100 or a character) the program should end, exit(1) command ends the program.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
int sum = 0, cases = 0, i, j, buff;
printf(“enter the number of integers”);
scanf("%d", &cases); //Number of tests
int *n,*res; n = (int*) malloc(cases * sizeof(int));
//Defining array for numbers
for(int i=0;i<n;i++)
{
res= sum_divisors(n);
printf(“%d”,n[i]) for(int j=0;j<res;j++)
{
printf(“%d”,res[j])
}
}
}
int[] sum_divisors(int *n)
{
int *res; res = (int*) malloc(cases * sizeof(int));
count=0;
for(i=1;i<=n;++i)
{
if(n%i==0) res[count]=i;
count++;
printf("%d ",i);
}
return res;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.