14,880,436 members

See more:

<Hi, I am supposed to write a code for my project. There are 3 functions: getRandom, getSummation, and printFactor. The getRandom function has two parameters of integer type, min and max. It generates and returns a random number between min and max, with both inclusive. The 2nd function is called getSummation. The function has one parameter of integer type. It calculates and return the summation from 1 to num inclusive. This means that it will take the number generated by getRandom and will take it's factorial, but instead of multiplying, it will add. E.x. number generated: 4. getSummation: 1 + 2 + 3 + 4

The 3rd function is calle printFactors. The function has one parameter of integer type. It will print all of the factors of the random number generated by getRandom. The 4th and final function is the main function. It asks the user to enter the min and max, checks if the two numbers are valid.

The numbers are valid when both numbers are positive and min is not greater than max. If the numbers are not valid, then print a message. Otherwise, get a random number num between min and max using function getRandom. Then get the summation of num by calling function getSummation and print the summation, and finally Print all factors of num by calling function printFactors. Do not use goto statement, recursion, or global variables.

Here is my code:

**What I have tried:**

I have tried changing the variables for getRandom, I have changed the rand() statement, I have changed the min max variables, I have tried changing the brackets around and I have tried a for loop and a while loop, I have tried rearranging the code around so that the int main code would go first, and I have also tried moving various bits of code from void to int main to see if maybe they would work there. Honestly, at this point, I have tried just about everything.

The 3rd function is calle printFactors. The function has one parameter of integer type. It will print all of the factors of the random number generated by getRandom. The 4th and final function is the main function. It asks the user to enter the min and max, checks if the two numbers are valid.

The numbers are valid when both numbers are positive and min is not greater than max. If the numbers are not valid, then print a message. Otherwise, get a random number num between min and max using function getRandom. Then get the summation of num by calling function getSummation and print the summation, and finally Print all factors of num by calling function printFactors. Do not use goto statement, recursion, or global variables.

Here is my code:

C++

Copy Code

#include <stdio.h> #include <time.h> #include <stdlib.h> void getRandom(int min, int max) { min = 2; max = 36; int number = rand() % 36 + 2; return; } void getSummation(int sum) { int min; int max; int number = rand() % 36 + 2; int Summation= 0; for(int number = 1; number <= sum; number++) { Summation = sum; Summation = 1 + (number*(number + 1)); } return; } void printFactor(int num) { int number = rand() % 36 + 2; num = number; for(num = 1; number <= num/ 2; num++) { if(num % number == 0) { printf("The Factors are: %d %d \n", num, number); } } return; }

I have tried changing the variables for getRandom, I have changed the rand() statement, I have changed the min max variables, I have tried changing the brackets around and I have tried a for loop and a while loop, I have tried rearranging the code around so that the int main code would go first, and I have also tried moving various bits of code from void to int main to see if maybe they would work there. Honestly, at this point, I have tried just about everything.

Comments

never mind. apparently this solution was of no value.

v4

Comments

Thank you for clearing that up. So when I type return, I return the value that is in the brackets right? so for example, if I did int getSummation(int sum), then I would do return sum?

I have tried your fix, and I am still having troubles with the output and getting some weird outputs.

I can see why. The code doesn't make very much sense. See my revised solution. That's one way they can be done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

If this is supposed to "run", then you're missing a "main" entry point.

https://opensource.com/article/19/5/how-write-good-c-main-function

2. When printFactor returns the factors of the Random number, they are wrong. Please help me fix this.

3. I am having issues with getSummation either turning up 0, or some really long negative or positive decimal number.