Thread: simple for loop function question

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    5

    simple for loop function question

    ok, so i compiled this program- it has no errors.. but it keeps returning the first number i put in as a variable..
    the assignment was to write a for loop that returns the product (number times a number) of two numbers.. so if i put in 5 and 3, it should return 15.. but instead it returns 5.

    there was a catch to the assignment.. i could not use the multiplication operator.. and i had to use a for loop.. here is what i did.. it seems as if for some reason the for loop is just being ignored.. ugh.. :-/

    Code:
    #include <stdio.h>
    int product(int x, int y){
            int i;
            int result;
            for (i=0; i<y; i++)
                    x += x;
            result= x;
            return result;
    }
    
    int main (){
            int x, y;
            printf("Type a number");
            scanf("%d", &x);
            printf("Type another number");
            scanf("%d", &y);
            printf("%d", product(x,y));
            return 0;
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    x +=x;
    Big, but not obvious mistake. Using 5 and 3 for x and y, the first time through that will read x (5) += 5;, and x will equal 10. But next time, you get: x (10) += 10. And x is 20. Not quite what you wanted. Use result in the loop, and don't modify x.
    The changed code:
    Code:
    #include <stdio.h>
    int product(int x, int y){
            int i;
            int result = 0;
            for (i=0; i<y; i++)
                    result += x;
            return result;
    }
    Also, you don't handle negative numbers, but I'm not sure you need to. It will work if x is negative, but not if y is negative. (It will say 0.) You could also say int i, result = 0;, just to save a line. ;-) No errors compiling, good job.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    5
    Quote Originally Posted by Cactus_Hugger
    Code:
    x +=x;
    Big, but not obvious mistake. Using 5 and 3 for x and y, the first time through that will read x (5) += 5;, and x will equal 10. But next time, you get: x (10) += 10. And x is 20. Not quite what you wanted. Use result in the loop, and don't modify x.
    The changed code:
    Code:
    #include <stdio.h>
    int product(int x, int y){
    int i;
    int result = 0;
    for (i=0; i<y; i++)
    result += x;
    return result;
    }
    Also, you don't handle negative numbers, but I'm not sure you need to. It will work if x is negative, but not if y is negative. (It will say 0.) You could also say int i, result = 0;, just to save a line. ;-) No errors compiling, good job.

    thank you for your suggestion.. but even with it- the return was still the first number i put in..

    and here's what my professor suggested- i'm not sure why... but:

    b) do not return inside the loop
    c) declare a variable called "result" or whatever
    insdie product()
    d) have the product code put the multiplication
    result into result
    e) return result after the loop

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    5
    i modified it this way- to include what u suggested and what he said.. it still is giving me the dingie 3 as the product.. lol.. ARGH! *pulls hair out*

    Code:
    #include <stdio.h>
    int product(int x, int y){
    		int i;
    		int result= 0;
    		int answer;
    		for (i=0; i<y; i++) 
    				result += x;
    		answer= result;
    		return answer;
    }

    also he told us to assume that the user always enters a positive integer.. here's the full assignment if u care to read it-HOMEWORK #7
    Write a program that reads in two non-negative integers and prints their product.
    BUT THERE'S A CATCH!
    You must NOT use the multiplication symbol *.
    Instead, use a for loop that does this calculation using repeated addition (see below for details). Note: CodeLab will accept your program even if you just use *, but I WILL NOT!
    MULTIPLICATION IS REPEATED ADDITION. Maybe you remember from third grade: multiplication is repeated addition. That is, "three times five" means "add three to zero five times":
    0 + 3 + 3 + 3 + 3 + 3
    Algorithmically, this means "start with 0 and keep adding the first operand as many times as the second operand tells you".

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
    int product ( int x, int y ) {
      return 12345;
    }
    If that doesn't print 12345, then look at what you're doing
    - are you editing the right file?
    - are you saving it?
    - are you recompiling it?
    - did it recompile without any warnings or errors?
    When you get it to print 12345, then replace it with Cactus_Hugger's original suggestion.
    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.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    5
    Quote Originally Posted by Salem
    Code:
    int product ( int x, int y ) {
    return 12345;
    }
    If that doesn't print 12345, then look at what you're doing
    - are you editing the right file?
    - are you saving it?
    - are you recompiling it?
    - did it recompile without any warnings or errors?
    When you get it to print 12345, then replace it with Cactus_Hugger's original suggestion.
    yes to everything u asked

    and oddly enough - when i put it to return 12345.. it STILLL returned three, when i typed three in as my first variable.. is there something wrong with my compiler?

    does it compile correctly when u guys do it?

    :-/

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I dunno - what is the name of your compiler.

    Works for me
    Code:
    $ cat bar.c && gcc bar.c && ./a.out
    #include <stdio.h>
    int product(int x, int y){
      int i;
      int result = 0;
      for (i=0; i<y; i++)
        result += x;
      return result;
    }
    
    int main (){
      int x, y;
      printf("Type a number");
      scanf("%d", &x);
      printf("Type another number");
      scanf("%d", &y);
      printf("%d", product(x,y));
      return 0;
    }
    Type a number12
    Type another number34
    408$
    The lack of some newlines on your printf is a minor problem
    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.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    5
    i'm doing it on unix..
    i think it might be unix.. or something.. i dunno cuz i submitted the home work on the website and it works.. but my compiler still returns the wrong answer.. hm

    either way

    thank you both- so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 07:48 AM
  3. A simple question of a C++ function
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2007, 06:49 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. multidimensional array function - simple question
    By boltz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2005, 04:24 PM