# simple for loop function question

• 05-06-2006
felicityxiv
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; }```
• 05-06-2006
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.
• 05-06-2006
felicityxiv
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
• 05-06-2006
felicityxiv
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".
• 05-06-2006
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.
• 05-06-2006
felicityxiv
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.

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?

:-/
• 05-06-2006
Salem
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
• 05-06-2006
felicityxiv
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! :) :)