Thread: For my next brilliant question...

  1. #1
    Join Date
    Oct 2004

    For my next brilliant question...

    Alright. I finally finished problem number four (That's the stupid 1+1/2+1/4.....1/2^n one)...And now I've moved on to number five. It requires that we take some "Pseudocode" and make it into a real algorythm.

    Here's the question:

    "Implement the following pseudocode in C++. Apply the algorithm to n = 18. What is the value of s when the algorithm stops?
    Step 1 Set d = 1 and s = 0
    Step 2 while d <= n

    Step 2.1 if n/d is an integer
    Replace s with s + d
    Step 2.2 Replace d with d + 1

    Alright...This is what I have so far:

    #include <iostream>
    int main()
    	float d = 1;
    	int s = 0;
    	int n;
    	std::cout << "Enter value for n: \n";
    	std::cin >> n;
    	while( d <= n)
    		if ( n/d >= 0)
    			s = (s+d);
    		d = (d + 1.0);
    	std::cout << "The value of S is " << s << "\n";
    	std::cout << "D is " << d << "\n";
    	return (0);
    The incorrect part is the 'if" statement:
    if ( n/d >= 0)
    This "if" statement is there to determine whether or not the number n is a whole number, correct? (Because n/d will always eqaul n, because d=1). So my guess is that I need to make up some way to look at "remainders" I need to use modulus "%" in this problem. I'm not exactly sure how to do that.
    To make sure that nobody thinks I'm asking you guys to do it for me, here's a past example where I've dealt with modulus. We had to make a program that took a number grade as input, and outputted a letter grade with a + or - next to it (A+, b,C-, etc.).
    Here's the code, which worked for me:
    int grade;
    	int remainder;
    	char plus[1];
    	std::cout << "Enter numeric grade:\n";
    	std::cin >> grade;
    	remainder = grade%10;
    	if ((remainder == 0) || (remainder >=8))
    		strcpy( plus , "+");
    		if (remainder >= 4)
    			strcpy( plus , "");
    			if (remainder >=1)
    				strcpy( plus , "-");
    	if (grade <= 60)
    		std::cout << "The grade is an F\n";
    		if (grade <=70)
    			std::cout << "The grade is a D" << plus <<"\n";
    			if (grade <=80)
    				std::cout << "The grade is a C" << plus <<"\n";
    				if (grade <=90)
    					std::cout << "The grade is a B" << plus <<"\n";
    					if (grade <= 100)
    						std::cout << "The grade is an A" << plus 
    						std::cout << "Invalid grade\n";
    	return (0);
    In this case, it was simple. All you had to do was divide the grade by 10 using mr. Modulus, and then you could set further parameters about the if the remainder was 0, 8, or 9, you got a "plus", I think.

    Anyways, how would I apply something like this to my little pseudocode problem? You can't just go 'n/d %10', right?
    modulus is a dividing mechanism, right?

    Ug. Sorry. Please point me correctly, oh wise ones.

  2. #2
    Registered User
    Join Date
    Mar 2002
    if modulus of two numbers is 0, then the quotient of the same two numbers must be an integer.

    if a % b == 0
    then a/b must be an int
    Last edited by elad; 10-21-2004 at 10:33 AM.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Suppose that when you divide an integer n by a divisor d, you get the quotient q with remainder r: n = qd + r.

    Then the operations / and % will give the results: n/d == q, n%d == r. So, if n%d == r == 0, then d is a divisor of n.

    The program is asking you to let d go through all the numbers from 1 to n, determine whether d is a divisor of n, and if it is, to add it to a running sum. Thus, at the end, the running sum will be equal to the sum of the divisors of n.

    So one other thing about your program is that you do not want to declare d to be a float. You should use ints when you want to use %. Also, why do you have a break? You do not want the program to skip the d=d+1 step. That's what advances the loop, right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM