Question

In: Computer Science

Today is international "Crazy-Go-Nuts For Functions Day!!" To celebrate we are going to play the traditional...


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.

Solutions

Expert Solution

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


Related Solutions

What’s going in the international news today and how does it correlate to the United States
What’s going in the international news today and how does it correlate to the United States
Communication Case Study Mike's Missed Step Today was going to be a big day for Mike....
Communication Case Study Mike's Missed Step Today was going to be a big day for Mike. He finally had a chance to show management that he had some good ideas about how the company could be improved. Because of a new policy allowing employees to submit written suggestions for changes to top management, he planned to send a memo to the president for consideration. Having worked in the stockroom for three years, he had seen the great amount of time...
This week we are going to look at how benchmarking is used in the current day...
This week we are going to look at how benchmarking is used in the current day health care industry! Please find a current events (within the past year) news article from a reputable news source (MSN, New York Times, Chicago Tribune, etc) that discusses the use of benchmarking by a health care organization. For your original post, please provide a 150+ word summary of your article. Be sure that you demonstrate your understanding of benchmarking by discussing how the benchmarking...
I’m going to go easy on you for this one! We learned a lot about the...
I’m going to go easy on you for this one! We learned a lot about the t-Test for independent samples, and last week you compared estimated speed (in miles) for smashed into group and hit group. This week, I want you to review that assignment and NOW compute the effect size (Cohen’s D using the pooled variance). To make sure you have all the data you need to calculate the effect size, here are the means and standard deviations for...
Suppose we are going to play a game. You have to choose to toss a coin...
Suppose we are going to play a game. You have to choose to toss a coin either 40 times or 400 times (pretend you have a lot of time on your hands!). You win the game if the percentage of heads is between 52.5% and 57.5%.
We have all heard about what is going on in the news today. Think about something...
We have all heard about what is going on in the news today. Think about something you see as falling in the category of “crime” and an instance of “deviance” in the news. Please be clear and fully explain your response using the appropriate terms for the example.
What drives firms to “go” international? We know there can be internal events (i.e., situations or...
What drives firms to “go” international? We know there can be internal events (i.e., situations or happenings within the firm) and external events (i.e., something occurring or stimulating the firm from the outside). Tell me what events (any mix of internal and external examples), might stimulate a firm to “go” international. Your answer may be bullets, but clearly identify five (5) selected events.
Today some people argue that "back in the day" we had the best leaders: MLK, Malcolm...
Today some people argue that "back in the day" we had the best leaders: MLK, Malcolm X, Cesar Chavez, Dolores Huerta, Robert Kennedy, Dennis Banks, John Trudell, Maya Angelou and more. They say if only we had such leaders again we would be making more progress. the Civil Rights movement do you agree or disagree with this assessment? Does this claim rest on the Messiah Myth?
1- According to Socrates, what is the value of justice? Why should we go to the trouble of being just? Is this discussion of justice still relevant today? How it affects economics?
  1- According to Socrates, what is the value of justice? Why should we go to the trouble of being just? Is this discussion of justice still relevant today? How it affects economics? 2- Socrates believed that the "ideal city" should be comprised of three classes. What are these classes, and what does he believe are their proper roles?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT