vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   Community Lounge (https://vborg.vbsupport.ru/forumdisplay.php?f=13)
-   -   Need a little Java help... (https://vborg.vbsupport.ru/showthread.php?t=50125)

Iguana 03-14-2003 01:43 AM

Need a little Java help...
 
How would I do this:

1. Generate an array of 20 random integers from zero to nine. search for the first occurence, if any, of the number seven, and report its position in the array.

2. Repeat the computation of part a 1000 times and for each position in the array, report the number of times that the first occurence of a seven in the array is at that position.

amykhar 03-14-2003 01:24 PM

1.

Code:

    Random generator = new Random();
    int integerArray[20];
    int foundSeven=0;
    int i;
    for (i=0; i<20; i++) {
    integerArray[i] = generator.nextInt();
    if ((integerArray[i]==7)&&(foundSeven==0)) {
        foundSeven = 1;
        println("Found the number 7 at position " + i);
    }// end if
 } // end for


amykhar 03-14-2003 01:32 PM

Random generator = new Random();
int integerArray[20];
int counterArray[20];
int foundSeven=0;
int i,j;
for (j=0;j<1000; j++) {
for (i=0; i<20; i++) {
integerArray[i] = generator.nextInt();
if ((integerArray[i]==7)&&(foundSeven==0)) {
foundSeven = 1;
counterArray[i]++;
println("Found the number 7 at position " + i);
}// end if
} // end for

} // end for

for (i=0;i<20;i++) {
println("Found at position " + i + " " + counterArray[i] + " times");
}

filburt1 03-14-2003 02:07 PM

Of course you'll need the 20+ MB runtime, import a million classes, and have to wait half a year for it to compile. Not that there's anything wrong with that. ;)

Iguana 03-14-2003 05:48 PM

Thanks everyone.

@Filb: I'm not going to compile it, I'm just helping out a friend.


All times are GMT. The time now is 12:53 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.00997 seconds
  • Memory Usage 1,722KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete