c++ program i made. Having a problem.

c++ program i made. Having a problem.

Post by zlass on Thu Jun 09, 2011 2:09 pm
([msg=58304]see c++ program i made. Having a problem.[/msg])

Code: Select all
// implement square() without using the multiplication operator; that is, do the
// x*x by repeated addition (start a variable result at 0 and add x to it x times).
//Then run some version of "the first program" using that square().

#include<iostream>
using namespace std;

int square(int i)
{
int result=0;                                         //initializing the result
        for (int count=0; count <= i; ++count) {       //while count is less than i; result = result + i:

        result += i;

        }
cout<< result;
}

int main()
{

        for (int i=0; i<= 5; ++i ) {
        cout<< i <<'\t'<< square(i) << endl;
        }
}



As i wrote in the program; I am trying to create a square function without actually multiplying the numbers but rather by adding i to result i times.

I'm sure there is an easy solution to this, but i cant for the love of god see it.
Any hints (not quite ready to give up just yet) are appreciated. :)

I believe this is the first time i post on these forums as well.. So hello. ;P
zlass
New User
New User
 
Posts: 5
Joined: Mon Jan 12, 2009 3:47 am
Blog: View Blog (0)


Re: c++ program i made. Having a problem.

Post by b33tr00t on Thu Jun 09, 2011 3:26 pm
([msg=58308]see Re: c++ program i made. Having a problem.[/msg])

i'm no c++ guru at all but it seems your function is not returning the results to main. instead of cout result write return result;
User avatar
b33tr00t
New User
New User
 
Posts: 25
Joined: Thu Mar 17, 2011 1:29 pm
Blog: View Blog (0)


Re: c++ program i made. Having a problem.

Post by Cethin on Thu Jun 09, 2011 4:24 pm
([msg=58312]see Re: c++ program i made. Having a problem.[/msg])

You weren't returning a value from your function. Instead of cout << result; you should have used return result;

Also, if you initialize return to 0, you have to do count < i, not count <= i. Here is the code I got working. Let me know if you have any more questions:

Code: Select all
// implement square() without using the multiplication operator; that is, do the
// x*x by repeated addition (start a variable result at 0 and add x to it x times).
//Then run some version of "the first program" using that square().

#include <iostream>

using namespace std;

int square(int i)
{
    int result = 0;                                         //initializing the result
    for (int count = 0; count < i; ++count)     //while count is less than i; result = result + i:
        result += i;

    return result;
}

int main()
{

    for (int i = 0; i <= 5; ++i)
        cout << i << "\t" << square(i) << endl;

    return 0;
}
Cethin
New User
New User
 
Posts: 4
Joined: Wed May 11, 2011 5:16 pm
Blog: View Blog (0)


Re: c++ program i made. Having a problem.

Post by zlass on Fri Sep 09, 2011 5:16 pm
([msg=61483]see Re: c++ program i made. Having a problem.[/msg])

Took a hell of a long time for me to answer, but yes your tip worked. Got it working properly so thanks. :)
zlass
New User
New User
 
Posts: 5
Joined: Mon Jan 12, 2009 3:47 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests