Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

How do I get a series of integers squared with C++ ?

Options
  • 02-04-2009 11:48pm
    #1
    Closed Accounts Posts: 7,134 ✭✭✭


    Hi Guys

    Im all at sea here


    int i;

    for (i=0; i <=10; i++)

    cout << i << endl ;

    ..................

    how do i square 'i' ? [ie from 0-10]


«1

Comments

  • Registered Users Posts: 8,438 ✭✭✭RedXIV


    square i? as in i*i?


  • Registered Users Posts: 6,440 ✭✭✭jhegarty


    Hi Guys

    Im all at sea here


    int i;

    for (i=0; i <=10; i++)

    cout << i << endl ;

    ..................

    how do i square 'i' ? [ie from 0-10]

    instead of outputting "i" use "i * i"


  • Closed Accounts Posts: 636 ✭✭✭NADA


    #include <iostream>

    using namespace std;


    int main(){
    int j=0;
    for(int i =1; i<11;i++){
    j=i*i;

    cout<<j<<endl;
    }
    }


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    also note, that your for loop will output the squares of 0 - 11

    easy way to remember this is, if you start with zero it's always less than the amount of times you want to do something: f(i = 0 ; i < times_you_want_to_do_something ; i++) or in your case there's not much point squaring 0 cause you'll just get zero so you'd do something like this for(i = 1 ; i <= 10 ; i++ ) this will do 1-10


  • Closed Accounts Posts: 7,134 ✭✭✭x in the city


    Yes its i^2 = i * i


    I want to list the numbers 1-10

    and then the corresponding squares,

    0 > 0
    1 > 1
    2 > 4
    3 > 9
    4 > 16
    5
    .
    .
    .

    I cant substitute that i=i*i here >

    i <=10...

    10*10 != 10 ..!!


  • Advertisement
  • Registered Users Posts: 1,916 ✭✭✭ronivek


    I'm pretty confused about what it is you're trying to do; but assuming you want this type of output;
    0 : 0*0=0
    1 : 1*1=1
    ....

    You would just use something like;
    int main(){
        int i_squared=0;
        for(int i=0; i<11;i++){
            i_squared = i*i;
            cout << i << ": " << i << "^2=" << i_squared << endl;
        }   
    } 
    


  • Closed Accounts Posts: 7,134 ✭✭✭x in the city


    this is what im trying to do

    [not homework btw...:pac:]


    cout << "integer "; cout << "square"; cout << "cube\n\n";

    int i;
    int i2 = i*i;
    int i3 = i*i*i;

    for ( i=0; i <=10; i++)

    cout << i ; cout << i2; cout << i3 << endl;


    got a problem with the last bit...???


    ****
    thanks guys

    got it done, the i3= i*i*i part etc, didnt know this had to be declared after the for statement

    cout << "integer " ; cout << "squared"; cout << " cube\n\n";

    int i;
    int i2;
    int i3;
    for( i=0; i<11;i++){
    i2 = i*i;
    i3 = i*i*i;
    cout << i << "\t" << i2 << "\t" << i3 << endl;
    }


    cout << "\n" << endl;


    system("PAUSE");

    return EXIT_SUCCESS;
    }


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Irrespective of whether it's homework or not; you don't seem to be putting much thought into what it is you're trying to do.

    It won't help you much in the long run if people just put code up for you to copy; especially considering you don't seem to be looking at the code that HAS been posted up and trying to make it work for you.

    Is that the actual code you're trying to run?


  • Registered Users Posts: 3,945 ✭✭✭Anima


    You should avoid using "\n" in C++ btw. You're loop isn't updating the answer, its just printing the same values out over and over. You're trying to use the 'i' variables like functions. They'll only store the number that "i*i" evaluates to, not the sum itself. Should be something like below:

    [PHP]
    cout << "integer " << "square" << "cube" << endl;

    for ( i=0; i <=10; ++i)
    cout << i << i^2 << i^3 << endl;
    [/PHP]


  • Closed Accounts Posts: 7,134 ✭✭✭x in the city


    ronivek wrote: »
    Irrespective of whether it's homework or not; you don't seem to be putting much thought into what it is you're trying to do.

    It won't help you much in the long run if people just put code up for you to copy; especially considering you don't seem to be looking at the code that HAS been posted up and trying to make it work for you.

    Is that the actual code you're trying to run?


    Thats the code I was working on and ammended. And it does the job exactly as is in the question in the Dietel book

    I was stuck where I assigned i2 and i3:. I had these at the very start, all my i's assigned.


  • Advertisement
  • Registered Users Posts: 1,916 ✭✭✭ronivek


    You keep editing your posts after I've replied! You should really just post a new reply to stop things getting confusing.

    I'm just trying to get you thinking about why things aren't working before asking the question; apologies if I came across a little harsh.


  • Closed Accounts Posts: 7,134 ✭✭✭x in the city


    Anima wrote: »
    You should avoid using "\n" in C++ btw. You're loop isn't updating the answer, its just printing the same values out over and over. You're trying to use the 'i' variables like functions. They'll only store the number that "i*i" evaluates to, not the sum itself. Should be something like below:

    [PHP]
    cout << "integer " << "square" << "cube" << endl;

    for ( i=0; i <=10; ++i)
    cout << i << i^2 << i^3 << endl;
    [/PHP]



    whats wrong with \n ?

    I just wanted to leave a gap before the ....press any key


    a n0b question here, but how do you know when to use braces?

    whenever you have a statement such as 'for' or 'while'

    you would encapsulate in braces?


  • Registered Users Posts: 3,945 ✭✭✭Anima


    "\n" is a C thing. Its good practice to use "endl" instead.

    You use braces when there is more than one line in your loop or conditional statement. I tend to always add them as I think it makes it look clearer.


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    whats wrong with \n ?

    I just wanted to leave a gap before the ....press any key


    a n0b question here, but how do you know when to use braces?

    whenever you have a statement such as 'for' or 'while'

    you would encapsulate in braces?

    Strictly speaking there's nothing wrong with \n; but there are some subtle issues concerning portability. endl is more portable but if overused can introduce performance issues.

    The braces are used for delimiting blocks of code; and for you I would highly recommend using braces for all your conditionals and loops.

    You've mentioned the Deitel & Deitel book; it should all be explained there?


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Using endl flushes the stream; \n doesn't.


  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    OP, how experienced are you?


  • Closed Accounts Posts: 7,134 ✭✭✭x in the city


    I just started a few weeks ago, why.

    and that endl vs \n ?


    I used \n to leave a space between the last digits that were output as it was to close to the press any key to continue

    how can endl create a space?


  • Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,527 Mod ✭✭✭✭BossArky


    Off topic:

    Can someone suggest a C++ compiler which I can download (free) which hopefully dosen't take up gbs of space? Thanks.


  • Closed Accounts Posts: 7,134 ✭✭✭x in the city


    Gigs of space...??

    Dev c++ Uses less than 60Meg


  • Registered Users Posts: 8,438 ✭✭✭RedXIV


    codeblocks?

    link here -> http://www.codeblocks.org/


  • Advertisement
  • Registered Users Posts: 3,945 ✭✭✭Anima


    I used \n to leave a space between the last digits that were output

    "\n" and endl are the same thing visually. It just puts the next set of characters on a new line.

    I recommend codeblocks anyways, very customizable and you can use a few different compilers with it easily.


  • Registered Users Posts: 695 ✭✭✭DaSilva


    Anima wrote: »
    You should avoid using "\n" in C++ btw. You're loop isn't updating the answer, its just printing the same values out over and over. You're trying to use the 'i' variables like functions. They'll only store the number that "i*i" evaluates to, not the sum itself. Should be something like below:

    [PHP]
    cout << "integer " << "square" << "cube" << endl;

    for ( i=0; i <=10; ++i)
    cout << i << i^2 << i^3 << endl;
    [/PHP]

    ^ is the xor operator. You wanna use pow(,) function I'm pretty sure.


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    Gigs of space...??

    Dev c++ Uses less than 60Meg

    Don't recommend horrible outdated software please :p


  • Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,527 Mod ✭✭✭✭BossArky


    Gigs of space...??

    Dev c++ Uses less than 60Meg

    Yep, Borland C++ took up about 4 gb when I used to have it on my pc back at uni years ago.

    So is Dev C++ a valid recommendation?


  • Registered Users Posts: 3,945 ✭✭✭Anima


    ^ is the xor operator. You wanna use pow(,) function I'm pretty sure.

    Yeah sorry was mixing it up with this version of C is use.


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    BossArky wrote: »
    Yep, Borland C++ took up about 4 gb when I used to have it on my pc back at uni years ago.

    So is Dev C++ a valid recommendation?

    why-you-shouldnt-use-dev-c

    :)

    Use code::blocks with MinGW or Visual Studio c++ 2008 express if on Windows.


  • Closed Accounts Posts: 7,134 ✭✭✭x in the city


    I never knew Dev c++ was so bad.

    Im downloading Visual C++ now.

    I am onto chapter 3 in deitel, but its wayy too much classes and objects, very confusing.. seems like we went from easy ish stuff into something far more difficult in a page.!


  • Closed Accounts Posts: 1,015 ✭✭✭Epic Tissue


    You could also use Eclipse with C/C++ plugin and MinGW


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    You might be better off playing about with C a bit before jumping into OOP if you've never programmed before and are having trouble with the concepts.

    Any particular reason you've settled on C++ as a language to learn?
    I never knew Dev c++ was so bad.

    Im downloading Visual C++ now.

    I am onto chapter 3 in deitel, but its wayy too much classes and objects, very confusing.. seems like we went from easy ish stuff into something far more difficult in a page.!


  • Advertisement
  • Closed Accounts Posts: 7,134 ✭✭✭x in the city


    Yeh, well my degree is in electronics all the jobs or most of the interesting ones need C++ now

    So I guess no pain, no gain.

    C++ would be used for all embedded programming now. Microcontrollers et al.


Advertisement