Thread: Array problem.

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    15

    Array problem.

    create a loop of your choice that will subtract the number from itself the number of times equal to the number itself.
    for example if i enter a 3 it will do 3-3-3 = -3 or 4 4-4-4-4= -8
    thank you for the help in advance
    Code:
    #include <stdio.h>
     
    int main()
    {
        int base, exponent, counter, result = 1;
        printf("The number you entered is \n");
        scanf("%d", &base);
         
        for(counter = 0; counter <base; counter++)
        {
            result = result-base;   //  this giving me the wrong number  
        }  
        
        printf("The result is %d" , result);
    
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you start with result = 1? It seems to make more sense to start with result = base, though of course you have to do this at the right place. You may also need to adjust the number of iterations of the loop since the number of times you should be subtracting is one less than the base.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    That's where i 'm confuse on knowing where to start with. Can you explain this to me in better way because i'm new to programing i don't really understand it. Thank you

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's take the example with 4. You begin the value of 4 (i.e., the initial value of result), then you subtract 4 from that value 3 times. Since you are required to use a loop, that means your loop will loop 3 times.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    So on line 5 where i'm declaring my variables result should = base

    on line 9 for my loop for(counter =0; counter < base; counter++
    this is saying the counter is less then the base enter to add one to it correct?
    whats the counter = 0 doing ?
    and on line 11 that s doing what should take my result and subtract the base (my initial number correct)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ElJefe05
    this is saying the counter is less then the base enter to add one to it correct?
    whats the counter = 0 doing ?
    Test your program and examine the output. What does the output tell you?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    Code:
    #include <stdio.h>
     
    int main()
    {
        int base, exponent, counter, result = 0;
        printf("The number you entered is \n");
        scanf("%d", &base);
         
        for(counter = 1; counter <base; counter++)
    	{
            result = result-base;   
        }  
        
        printf("The result is %d" , result);
    
    
        return 0;
    }
    for 3 i get -6 and for 4 - 12

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Great. Ask yourself why, and keep debugging. It may help to trace through the loop iteration by iteration to see what is happening to the value of result.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    trust me i've done tried everything and still getting nowhere. I've been at this for like 6hrs i have looked at a few youtube videos but not helping at all. But thank you.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, let's go over your code in post #7 with the input value of 4.

    Before the loop:
    result = 0; base = 4

    The loop begins:
    counter = 1; 1 < 4; so counter is incremented to 2 and control enters the loop body.

    In the loop body:
    result = 0 - 4 = -4

    At this point, you should have noticed that something is wrong. You are trying to evaluate 4 - 4 - 4- 4, not 0 - 4 - 4 - 4. So, why do you have 0 - 4?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    I understand that part now.
    ok so start my for(counter = 1; counter <base; counter++)

    this means
    i start at 1 if enter a 4 its going to check to see the my counter is less than the base if its true then the counter ++ will up the counter by one
    so when it does 2 does statment if 2 <4
    then 3 < 4
    then 4 < 4 then it stops here ,my loop correct
    so once it gets to that will print out my result
    My question is what is the result = result-base doing is that taking 4-4=0
    How to i get it to subtract from itself that's where im lost. If i was going to multiply the number by itself like 4*4*4*4 i would do result = result*base . I've done this already but i don't get why subracting it be anymore different.
    T

    bY THE WAY thank you for the help.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ElJefe05
    If i was going to multiply the number by itself like 4*4*4*4 i would do result = result*base .
    Let's write a version of your program from post #7 to solve this problem that you have proposed:
    Code:
    #include <stdio.h>
    
    /* This program reads in an integer (assumed to be non-negative) into base and
     * computes base to the power of base */
    
    int main(void)
    {
        int base, counter, result = 0;
        printf("The number you entered is \n");
        scanf("%d", &base);
    
        for (counter = 1; counter < base; counter++)
        {
            result = result * base;
        }
    
        printf("The result is %d\n", result);
    
        return 0;
    }
    Compile and run the above program. Test with a few input values, e.g., 2, 3 and 4. Does it work correctly? Why or why not? If it doesn't work, how would you fix it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    no it wont work bc if you set the counter = 1 if counter = 0 will go thru the loop 3times 0 1 2 3x3x3 = 27
    it will go counter 1 base 3 ==
    counter 2 base 3
    result = result*base if i put in 3 is giving me 9 which is wrong b/c its only going thru the loop 2x should be 27

  14. #14
    Registered User
    Join Date
    Mar 2017
    Posts
    15
    for subtraction my result should be result = result - base . Do i need to change my counter++ to counter -- to subtract one less since we are not trying to add them.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ElJefe05
    result = result*base if i put in 3 is giving me 9 which is wrong b/c its only going thru the loop 2x should be 27
    Look, I told you to "Compile and run the above program", not write another version of your program, the source which you did not provide, and then tell me what you think is wrong with that unknown program. The program I posted in post #12 utilises the exact same logic as the program that you posted in post #7, except that subtraction was changed to multiplication.

    This is important because if you had run and compiled the above program, you would have seen that if you put in 3, you get a result of 0, not 9, not 27. 0. Zero. ZERO.

    You get a result of 0 because you are multiplying with 0. As in you are computing 0 * 3 * 3 = 0

    Control should only go through the loop twice when the input is 3. You want to compute 3 * 3 * 3, not 3 * 3 * 3 * 3. Likewise, you want to compute 3 - 3 - 3, not 3 - 3 - 3 - 3 (although personally that sounds more like "subtract the number from itself the number of times equal to the number itself"). You can get the correct answer for subtraction by computing 0 - 3, or in the case of 4, 0 - 4 - 4, but that doesn't exactly simulate what your assignment wants you to simulate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-04-2014, 09:51 AM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  4. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  5. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM

Tags for this Thread