Welcome to Dream.In.Code
Become a Java Expert!

Join 150,374 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,684 people online right now. Registration is fast and FREE... Join Now!




Iteration sequence problem

 
Reply to this topicStart new topic

Iteration sequence problem

CrazyJ
5 Feb, 2008 - 07:03 AM
Post #1

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
Hello all,
I have a program with which I am having difficulty, I am not asking that anyone write it for me but am hoping that someone can help me to understand the algorithm/logic of the following:

The program should ask the user for an input value for n, where that value is an integer. Then the program should output the first n elements of the following sequence as well as the sum of all those elements: 1, -2, 4, -8, 16, -32, 64....
If n=3, then the output would be 1, -2, 4 and the sum would be 3.

I don't know where to begin with this, can someone please explain how one would achieve this output? Thanks in advance.
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Iteration Sequence Problem
5 Feb, 2008 - 07:20 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
So, n is the how many numbers are in your list.
You could create an int array with space for n ints. Then, you have to fill it somehow. Finally, you can iterate through the array and simply add each value together.
User is online!Profile CardPM
+Quote Post

bhandari
RE: Iteration Sequence Problem
5 Feb, 2008 - 07:45 AM
Post #3

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
If n=3, then the output would be 1, -2, 4


Each number is got by multiplying -2 with previous number. (With first number being 1)
User is offlineProfile CardPM
+Quote Post

CrazyJ
RE: Iteration Sequence Problem
6 Feb, 2008 - 09:15 AM
Post #4

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
QUOTE(bhandari @ 5 Feb, 2008 - 08:45 AM) *

QUOTE
If n=3, then the output would be 1, -2, 4


Each number is got by multiplying -2 with previous number. (With first number being 1)

Gotcha! Thank you! icon_up.gif
User is offlineProfile CardPM
+Quote Post

CrazyJ
RE: Iteration Sequence Problem
6 Feb, 2008 - 12:08 PM
Post #5

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
One more thing... I realize that this program is a simple iteration sequence:
CODE

int ii;
        for(ii = 1;ii <= n;ii++)
            System.out.println("The sequence is: "+ii);

This will simply output the numbers 1 through n. But, how can I make it output the result of each multiplied by -2? 1*-2= -2 then it (-2) by -2, -2*-2=4 and so on. This should be simple but I am just not sure of the syntax and cannot find anything in my book that approaches this type of iteration.

PS I need to do this without using an array

This post has been edited by CrazyJ: 6 Feb, 2008 - 12:19 PM
User is offlineProfile CardPM
+Quote Post

CrazyJ
RE: Iteration Sequence Problem
6 Feb, 2008 - 08:12 PM
Post #6

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
Yep, me again, remaining true to the thread. Anyway, I need some help on how to achieve the output on this program (if user enters 3 for n, output will be 1, -2, 4 and sum will be 3) I can't seem to figure out where to implement the * -2. Please help?
CODE

int n;
//        ======
        System.out.println("This program will take input for the value n");
        System.out.println("and output the sequence.");
        System.out.println("=======================================");
//        ==============================================================
        Scanner scannerObject = new Scanner(System.in);
//        ===============================================
        System.out.println("Please input an integer: ");
        n = scannerObject.nextInt();
        System.out.println("Integer entered = " +n);
//        ==========================================
        int ii;
        for(ii = 1*-2;ii <= n;ii++)
            System.out.println("Sequence is: "+ii);
    
    }// main


I'll take whatever hints I can get! crazy.gif Thanks.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month