I'm writing a program for my university that if the user input is between 10 and 45 (inclusive). It adds the sum of the numbers between the input and 10. Secondly, if user input is greater than 45 it divides the user input by 13 and gives the remainder (This is the part I'm stuck on). And lastly (Not on this part yet) If the user inputs a non-negative value less than 10, then you will compute product of the values between 1 and the number. Any advice is appreciated!
#include <iostream>
usingnamespace std;
int myPrompt(void);
int mySum(int uno);
int myFact(int);
int myMod(int);
void myPrint(int sum, int product);
int main()
{
int uno, sum, product;
uno = myPrompt();
sum = mySum(uno);
product = myMod(uno);
myPrint(sum, product);
system("pause");
return 0;
}
int myPrompt(void)
{
int uno = -1;
while (uno < 0)
{
cout << "Enter a non-negative integer (-9999 to end): ";
cin >> uno;
}
return uno;
}
int mySum(int uno)
{
int sum = 0;
for (int i = 10; i <= uno; i++)
{
sum = sum + i;
}
return sum;
}
int myMod(int uno)
{
int product = -1;
while (uno >= 45)
{
product = uno % 13;
}
return product;
}
/*int myFact(int)
{
}
*/
void myPrint(int sum, int product)
{
cout << sum << endl;
cout << product << endl;
}
Line 4. The "void' is not needed just empty ()s. The same on line 24.
In the function "myPrompt", line 29, The prompt says "-9999 to end", but this will cause an endless while loop until you enter a positive number and you do not check for "-9999" or do anything about it.
You could also deal with the input being < 10 or > 45 here. Or return the value and call another function to deal with it.
When the times comes yourVarableName = uno % 13;. This will give you the remainder. You will also most likely have to use normal division at some point, but be careful with integer division. The result is a whole number only.
And for the part you are not at yet, I am thinking of another function to deal with that.
My thoughts for now. I have not worked with the code yet.
While I was working with the program I discovered the function "myMod". As I started to understand what you are doing I realized that it is all wrong. According to the instructions you only need to do line 50 once and display the answer.
Before I found "myMod" I had written this:
1 2 3 4 5 6 7 8 9 10
void GreaterThan45(int uno)
{
int remainder{};
remainder = uno % 13;
std::cout << "\n The remainder of "<<uno<<" divided by 13 is: " << remainder << std::endl;
std::cout << std::endl;
}
You can either use the "cout" statement in the function or back in main. I think I would leave it in the function.
I did notice that the function "myFact" and "myProduct" are missing from your code. If the number is less than 45 you will need "myProduct".
Ended up being able to get my functions to properly work but I'm having a little trouble with myPrint function in that I can't get it to output only one of those bodies of text depending on the user input. I also don't know how to implement that sentinel value into the program. Any advice on these two topics would be very much appreciated.
All these functions I am required to use to turn in my program so if there are ways that are easier than what i'm using sorry this is what i've been instructed to use.
//Prog04.cpp
//Dalton Phelps
//COSC 1030 Fall Semester 2018
//Programming Assingment 4
//Sep 26, 2018
#include <iostream>
usingnamespace std;
int myPrompt();
int mySum(int uno);
int myFact(int);
int myMod(int uno);
void myPrint(int sum, int product, int fact, int uno);
int main()
{
int uno, sum, product, fact;
uno = myPrompt();
sum = mySum(uno);
product = myMod(uno);
fact = myFact(uno);
myPrint(sum, product, fact, uno);
system("pause");
return 0;
}
int myPrompt()
{
int uno = -1;
while (uno < 0)
{
cout << "Enter a non-negative integer (-9999 to end): ";
cin >> uno;
}
return uno;
}
int mySum(int uno)
{
int sum = 0;
for (int i = 1; i <= uno; i++)
{
sum = sum + i;
}
return sum;
}
int myMod(int uno)
{
int product = -1;
if (uno >= 45)
{
product = uno % 13;
}
return product;
}
int myFact(int uno)
{
int fact = 1;
for (int x = 1; x <= uno; x++)
{
fact = fact * x;
}
return fact;
}
void myPrint(int sum, int product, int fact, int uno)
{
if (uno >= 45)
{
cout << "The remainder of " << uno << " / " << "13 is " << product << endl;
}
if (uno <= 9)
{
cout << "The product from 1 to " << uno << " is " << fact << endl;
}
if (uno <= 44)
{
cout << "The sum from 1 to " << uno << " is " << sum << endl;
}
}
#include <iostream>
constint SENTINEL = -9999 ;
int get_int();
int sum_10_to_n( int uno ); // uno > 9 && uno < 46
int product_1_to_n( int uno ); // uno < 10
int mod_13( int uno ); // uno > 45
void print_sum( int uno ); // uno > 9 && uno < 46
void print_product( int uno ); // uno < 10
void print_mod( int uno ); // uno > 45
void print_result( int uno ) ; // non-negative uno
int main()
{
constint uno = get_int() ;
if( uno != SENTINEL ) print_result(uno) ;
// else sentinel value was entered; do nothing
}
int get_int()
{
int uno = -1;
while( uno < 0 && uno != SENTINEL ) // repeat if negative, except quit if SENTINEL
{
std::cout << "Enter a non-negative integer (" << SENTINEL << " to end): ";
std::cin >> uno; // we assume that the user always enters an integer
}
return uno;
}
int sum_10_to_n( int uno )
{
int sum = 0;
for( int i = 10 ; i <= uno; ++i ) sum += i ;
return sum;
}
int mod_13( int uno ) { return uno%13 ; }
int product_1_to_n( int uno )
{
if( uno == 0 ) return 0 ;
int fact = 1;
for (int x = 1; x <= uno; ++x ) fact *= x ;
return fact;
}
void print_sum( int uno )
{
std::cout << "The sum of numbers from 10 to " << uno << " is "
<< sum_10_to_n(uno) << '\n' ;
}
void print_product( int uno )
{
std::cout << "The product of numbers from 1 to " << uno << " is "
<< product_1_to_n(uno) << '\n' ;
}
void print_mod( int uno )
{
std::cout << "The remainder of " << uno << " / 13 is "
<< mod_13(uno) << '\n' ;
}
void print_result( int uno )
{
if( uno > 45 ) print_mod(uno) ;
elseif( uno < 10 ) print_product(uno) ; // note: else if
else print_sum(uno) ; // note: else
}