Like Tree1Likes
  • 1 Post By CommonTater

Clueless on an Assignment

This is a discussion on Clueless on an Assignment within the C Programming forums, part of the General Programming Boards category; My assignment is to do the following: Write a program that executes multiplication of two integers entered by the user ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    51

    Clueless on an Assignment

    My assignment is to do the following:

    Write a program that executes multiplication of two integers entered by the user , by
    using ONLY the addition operation.
    So, for example if the inputs are 3 and 4, the program will execute 3 times addition of
    4: 4+4+4.

    I have to do it using the "while" function. I understand the basic concept of the while function but I have no clue how to do this. Please help me understand it better.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Well, if you understand the basic function of a while loop why don't you throw some code together. Go ahead and make an educated guess and post some code. We will go from there.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    while isn't a function, it is a loop construct.

    Do it as you'd do it by hand...suppose....( m x n ):
    Count from 1 to n or 0 to n-1.
    Each time add m to the sum, which was initially 0.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Well, if you understand the basic function of a while loop why don't you throw some code together. Go ahead and make an educated guess and post some code. We will go from there.

    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    {
          int num1, num2, sum;
          
          printf ("Enter two numbers to multiply: ");
          num1 = GetInteger();
          num2 = GetInteger();
          sum = 0;
          while (sum < num1 * num2)
          {
                sum = + num1 + num1;
                }
          printf ("product = %d.\n", sum);
          getchar();
          }
    There you are, Andrew. I had this before I asked the question and it wasn't working. I've tried everything that I could think of. That is why I asked the question.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by PYROMANIAC702 View Post
    .......
    There you are, Andrew. I had this before I asked the question and it wasn't working. I've tried everything that I could think of. That is why I asked the question.
    Hmm.....good way to get help.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Try it with a for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Hmm.....good way to get help.
    NO! I wasn't being rude in any way. I'm sorry if i offended you, but I was just pointing out that I tried it before I asked. Please help me.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Code:
     sum = ???? + num1 + num1;
    Should be adding three things.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    You could try this while loop:

    Code:
    while(counter<=num1)
       {
            sum += num2;
       }
    I don't know if this works,,

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jannr View Post
    I don't know if this works,,
    I know it doesn't work, since you never change counter or num1 inside your loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Quote Originally Posted by quzah View Post
    I know it doesn't work, since you never change counter or num1 inside your loop.


    Quzah.
    lol,, I forgot,, haha sorry,,

    Code:
    while(counter<=num1)
    {
        sum += num2;
        counter++;
    }
    I guess this is right,, ^^

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by PYROMANIAC702 View Post
    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    {
          int num1, num2, sum;
          
          printf ("Enter two numbers to multiply: ");
          num1 = GetInteger();
          num2 = GetInteger();
          sum = 0;
          while (sum < num1 * num2)
          {
                sum = + num1 + num1;
                }
          printf ("product = %d.\n", sum);
          getchar();
          }
    There you are, Andrew. I had this before I asked the question and it wasn't working. I've tried everything that I could think of. That is why I asked the question.
    Ok, think about this....

    You have a start value... you have a counter... how can you use them to construct a simple loop?

    Code:
    int x = 234;  // multiply
    int y = 37;  // by
    int z = 0;   // answer
    
    while (y--)
       z += x;
    jannr likes this.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    10
    Quote Originally Posted by jannr View Post
    lol,, I forgot,, haha sorry,,

    Code:
    while(counter<=num1)
    {
        sum += num2;
        counter++;
    }
    I guess this is right,, ^^
    actually it should be like that
    Code:
    sum = 0;
    counter = 1;
    while(counter<=num1)
    {
        sum += num2;
        counter++;
    }
    Last edited by agathery; 07-14-2011 at 07:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a program- I'm clueless
    By godfrey270487 in forum C++ Programming
    Replies: 10
    Last Post: 03-27-2007, 11:13 AM
  2. clueless
    By majornewb in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2004, 08:52 AM
  3. bitmaps i am clueless
    By master2000 in forum C++ Programming
    Replies: 28
    Last Post: 12-22-2002, 10:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21