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

Thread: Two Questions

  1. #1
    Join Date
    Dec 2008
    Posts
    1

    Two Questions

    I just need to know two things for my program to work. The first one is i have a user input and i need another method to use those numbers but im unsure how to do so. In my program its lower1 and upper1. The other is i need to print out the arrays element number not whats actually in the element for example in my case the elements are set to true and false. My problems basically have to deal with the last method in showSieve(). Heres my code

    Code:
    import java.util.Scanner;
    
    
    public class test
    {
    public static int upper;
    public static int lower;
    public static boolean[] primes = new boolean[50001];
    public static void main ( String args[] )
    {
    
    initializeSeive();
    processSieve(); 
    getBoundaries(); 
    showSieve();
    
    }
    
    public void initializeSeive() // initializes the sieve.
    {
    int lower = 1;
    int upper = 50000;
    
    primes[0] = primes[1] = false;
    
     for (int i = 2; i < primes.length; i++)
     {
     primes[i] = true;
     }
    }
    
    
    public void processSieve() // run the Sieve algorithm
    {
    
    
    for(int i = 2; (i * i) <= 50000; i++ )
      {
       for (int j = (i * i); j <= 50000; j = j + i) 
       {
          primes[j] = false;
       }
      }
    }
    
    public static void getBoundaries() // get lower and upper boundaries
    {
    
    Scanner in = new Scanner ( System.in );
    int lower1 = 0, upper1 = 0;
    
    do
     {
    
     do
     {
     System.out.print ( "Please enter the lower boundary (between 1 and 50000): " );
     lower1 = in.nextInt();
     }
     while ( ( lower1 < 1 ) || ( lower1 > 50000 ) );
    
     do
     {
     System.out.print ( "Please enter the upper boundary (between 1 and 50000): " );
     upper1 = in.nextInt();
     }
     while ( ( upper1 < 1 ) || ( upper1 > 50000 ) );
    
     if (lower1 > upper1)
     { 
     System.out.println ( "Your upper boundary cannot be smaller than your lower boundary");
     }
    
     }
    while ( lower1 > upper1 );
    }
    
    public void showSieve() // now shows sexy pairs between lower and upper
    {
    
    System.out.printf ( "Here are all of the sexy prime pairs in the range %d to %d, one pair per line: \n", lower1, upper1 ); 
    
    for (int i = lower1; i < upper1; i++) 
       {
        if (primes[i] == true) 
         { 
          if (primes[i + 6] == true)
            {
             System.out.printf( "" + primes[i] + " and " + primes[i + 6] + "\n");      
            }
         }  
       } 
    
    }
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Two Questions

    Quote Originally Posted by nickels5069 View Post
    ... i need another method to use those numbers but im unsure how to do so. In my program its lower1 and upper1.
    Are we supposed to guess what the method will be called and what it will do? If you explain what this method is supposed to do and what part of it you are having trouble with, we may be able to help.

    My problems basically have to deal with the last method in showSieve().
    If you mean the System.out.printf(..) method in showSieve(), why not say so? If not, explain what you mean.

    If you want to print out the index of an item in the array, just print its index... what is the problem with that?

    All truths are easy to understand once they are discovered; the point is to discover them...
    G. Galilie
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

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