C/C++ printf() before scanf() issue

Your output is being buffered. You have 4 options: explicit flush fflush after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly. fflush( stdout ); have the buffer only buffer lines-wise useful for when you know that it is enough to print only complete lines setlinebuf(stdout); disable the buffer setbuf(stdout, NULL); … Read more

Given an array of integers and a target integer k. Check if the elements in the array sum to the target [closed]

if you have to sum only any 2 number then it is simpler to do it else ..you will have to crack your head for that … int[] ara={2,3,6,2,5,1}; for(int i=0;i<ara.length;i++) { for(int j=0;j<ara.length;j++) { if((ara[i]+ara[j])==8) { System.out.println(ara[i]+”,”+ara[j]); } } } the output will be 2,6 //number 2 which is present at index 0 3,5 … Read more

Randomly output text from array to view – RAILS [closed]

It is not really well formated question … You should be more precise about what you want, what you are using, etc. Assuming you are using Rails 3, ERB for HTML generation: # in your controller: file_as_array = IO.readlines(‘filename.txt’) random_line = rand(file_as_array.size) @my_random_string = file_as_array[random_line] # in your view: <%= @my_random_string %>