In: Computer Science
Today is international "Crazy-Go-Nuts For Functions Day!!" To
celebrate we are going to play the traditional CGNFFD game. The
rules of the game are:
1. I give you a bunch of coding problems and you code them in a
single file (lab7.c) so that, when you are finished, main executes
all of them sequentially.
2. Each member of your team should be at the keyboard for half of
the functions. Write the name of the person who was “driving” at
the top of each function.
3. You are not allowed to have any while loops (or any other loops
for that matter,) if statements or any other control flow
statements in main().
4. You can create as many functions as you like.
5. Functions, other than main, are allowed to have control flow
statements but they are not allowed to have any calls to scanf or
printf unless otherwise specified.
6. In main(), you are not allowed to call any functions you did not
write other than scanf and printf.
7. Lots of repetitive code will be penalized (repetitive code means
it should be a function).
8. Make sure all of your output is nicely formatted and easy to
understand.
Problem 1
Ask the user to enter a positive integer. Calculate the floor of
the inputted value's square root (i.e. calculate the closest
integer value equal to, or less than, its square root.) Output the
results.
Problem 2
Ask the user to enter two positive integer. Calculate the greatest
common divisor of the two numbers. Output the results.
Problem 3
A perfect number is a positive integer that is equal to the sum of
its factors excluding itself. For example, 6 is a perfect number
because 1+2+3 = 6. Ask the user to enter a positive integer, then
display the next largest perfect number. If the user entered 5, you
should print 6, if the user entered 6 you should return 28.
HINT: This one might be easier if you wrote more than one function.
Break the problem down into individual steps.
Problem 4
Create and initialize an array of 10 integers. Print them out. To
complete this task you are allowed to call printf in 1 function
outside of main.
Problem 5
A vector dot product is an algebraic operation on two equal-length
vectors of numbers. The dot product of two vectors a = [a1, a2,
a3,...an] and b = [b1, b2, b3,...bn] is: a1*b1 + a2*b2 + a2*b3 +
...+ an*bn. Notice an integer array is a vector. Create and
initialize two integers arrays that contain 10 numbers a piece.
Calculate and display their dot product. You are not allowed to use
printf but you can use your solution to problem 4.
Problem 6
Create and initialize a 10 by 10 2D array. Display it. You are not
allowed to use printf but you are allowed to use your solution from
problem 4.
Note that I have only answered the first 4 questions. I am sorry for the inconvenience but it is according to the rules. You can always post the other two questions or try them yourself and learn. The code starts from here:
#include <math.h>
#include <stdio.h>
/*function for problem 1*/
int square_root(double N)
{
double result=sqrt(N);
return floor(result);
}
/*function for problem 2*/
int findGCD(int a, int b)
{
if (a == 0)
return b;
return findGCD(b % a, a);
}
/*function for problem 3*/
int nextPerfect(int n)
{
long long int sum = 1;
for (long long int i=2; i*i<=n; i++)
{
if (n%i==0)
{
if(i*i!=n)
sum = sum + i + n/i;
else
sum=sum+i;
}
}
if (sum == n && n != 1)
return n;
return nextPerfect(n+1);
}
/*function for problem 4*/
void print_array(int arr[10]){
for(int i=0;i<10;i++){
printf("%d ",arr[i]);
}
}
/*main function*/
int main()
{
int N;
printf("Enter The Number : ");
scanf("%d",&N);
printf("\nThe floor value of square root of the number
is: %d ", square_root(N));
int num1, num2, gcd;
printf("\nEnter two positive numbers : ");
scanf("%d%d", &num1, &num2);
gcd = findGCD(num1, num2);
printf("\nThe GCD of %d and %d is %d\n", num1, num2, gcd);
int perfect;
printf("Enter The Number Next to Which you want to find the perfect
number : ");
scanf("%d",&perfect);
printf("\nThe required perfect number is: %d ",
nextPerfect(perfect));
int arr[10]={11,12,13,14,15,16,17,18,19,20};
printf("\nThe numbers in the array are: ");
print_array(arr);
return 0;
}
Here's how the output is supposed to look