Click here to Skip to main content
14,880,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
 #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;
}


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.
Posted
Updated 10-Feb-21 15:14pm
v2
Comments
ProgStudent 10-Feb-21 21:01pm
   
Thank you for the edit and for making my question better!
Patrice T 10-Feb-21 21:02pm
   
You should try to fond a tutorial and watch it.
ProgStudent 10-Feb-21 21:05pm
   
Tutorial on what subject?
Patrice T 10-Feb-21 21:18pm
   
Tutorial on C/C++ beginner.
ProgStudent 10-Feb-21 21:19pm
   
If you have any in mind, I would be more than happy to watch them. But I am not sure how they would help.
Patrice T 10-Feb-21 21:20pm
   
go to YouTube and type 'C tutorial beginner'
Gerry Schmitz 10-Feb-21 21:18pm
   
What are the "3 issues"? Other than the title, there is not a single other "question".

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
ProgStudent 10-Feb-21 21:21pm
   
1. When I run the code, and enter min and max, I get 3 numbers when I only need to get 1.
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.

1 solution

never mind. apparently this solution was of no value.
   
v4
Comments
ProgStudent 10-Feb-21 21:31pm
   
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?
ProgStudent 10-Feb-21 22:01pm
   
I have tried your fix, and I am still having troubles with the output and getting some weird outputs.
Rick York 10-Feb-21 23:49pm
   
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