CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Using an Array in an equation

    I am having trouble trying to figure out this only section of this program. I have an array. To keep this simple lets say the array is int n[] = {1, 2, 3, 4, 5} and I would like each number in the array to go through the equation 30n - 5. I would like the output to have the answer for each number going through. the output is going to look like. After the number was entered into the equation the answer is 25. After the number was entered into the equation the answer is 55. After the number was entered into the equation the answer is 85. After the number was entered into the equation the answer is 115. After the number was entered into the equation the answer is 145. So long question short is how do you make each number of the array go through the equation

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Using an Array in an equation

    You just use an ordinary for loop,

    Code:
    for (int i=0; i<n.length; i++) {
       int a = 30*n[i] - 5;
       System.out.println("After " + n[i] + " was entered into the equation the answer is " + a);
    }
    or maybe preferrably the for-each variation,

    Code:
    for (int x : n {
       int a = 30*x - 5;
       System.out.println("After " + x + " was entered into the equation the answer is " + a);
    }
    Last edited by nuzzle; July 1st, 2012 at 04:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured