Find the largest number and its index in the random array - loops

This is my current code for generating a set of 20 random numbers:
int[] array_1 = new int[20];
Random rand = new Random();
for (int i = 0; i < array_1.Length; i++)
{
array_1[i] = rand.Next(10,100);
Console.Write("{0} ", array_1[i]);
}

int highest = 0;
int highestIndex = 0;
for (int i = 0; i < array_1.Length; i++)
{
if (array_1[i] > highest)
{
highest = array_1[i];
highestIndex = i;
}
}

Related

Reverse an Array without using reverse Function and other array the same array to be reverse all elements in the same array. size fix

// I am tring but cant do it
int[] arr = {1,3,4,2,5,6};
int n = arr.length,x = 0;
for (int i = 0 ; i < n; i++ ) {
x++;
System.out.println(arr[i]);
}
//try this:
public class reverseArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[] { 1,3,4,2,5,6 };
System.out.print("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
// method #1: by using extra array
System.out.println();
System.out.print("Reversed array method 1: ");
int j = 0;
int revArr[] = new int[] { arr.length };
for (int i = arr.length - 1; i >= 0; i--) {
revArr[j] = arr[i];
System.out.print(revArr[j] + " ");
}
// method #2: without using extra array
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
System.out.println();
System.out.print("Reversed array method 2: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

number of subarray with average greater than average of the rest of the elements of array

We are given a array of size < 2000
and A[i]< 10^6.I know the bruteforce approach.Can we do better i.e in linear time ?
I am checking each subarray and comparing its average with the other elements.
public class FindingSubArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
double avg1 = getAverage(i,j,arr);
double avg2 = getAverageOfRest(i,j,arr);
//System.out.println(avg1+" "+avg2);
if(avg1 > avg2) {
a.add(i+1);
b.add(j+1);
}
}
}
System.out.println(a.size());
for(int i=0;i<a.size();i++){
System.out.println(a.get(i)+" "+b.get(i));
}
}
private static double getAverageOfRest(int i, int j, int[] arr) {
double result = 0;
int count = 0;
for(int k=0;k<i;k++) {
result += arr[k] ;
count ++;
}
for(int k=j+1;k<arr.length;k++) {
result += arr[k] ;
count ++;
}
if(count > 0)
return result/count;
else
return 0;
}
private static double getAverage(int i, int j, int[] arr) {
double result = 0;
int count = 0;
for (int k = i; k <= j; k++) {
result += arr[k] ;
count ++;
}
if(count > 0)
return result/count;
else
return 0;
}
}

How to sort an array in descending order

I have this code but this is not showing me the required result. that is to merge 2 arrays and print it in descending order. I wan to merge 2 sorted arrays taking input from user. user will tell the size of array the elements of array too and then my program should merge and sort descendingly and print
int main()
{
int num1,num2,i,tem;
printf("Number of elements in first array:");
scanf("%d",&num1);
printf("Number of elements in second array:");
scanf("%d",&num2);
int array1[num1],array2[num2],merge[num1+num2];
printf("Elements for array 1 \n");
for ( i = 0; i < num1; i++)
{
printf("Element:");
scanf("%d",&array1[i]);
}
printf("Elements for second array\n");
for ( i = 0; i < num2; i++)
{
printf("Element:");
scanf("%d",&array2[i]);
}
for ( i = 0; i < num1; i++)
{
merge[i] = array1[i];
}
for ( i = 0; i < num2; i++)
{
merge[i+num1] = array2[i];
}
for ( i = 0; i < num1 + num2; i++ )
{
if ( merge[i] < merge[i+1] )
{
tem = merge[i];
merge[i] = merge[i+1];
merge[i+1] = tem;
}
}
printf("Merge:");
for ( i = 0; i < num1 + num2; i++ )
{
printf("%d ",&merge[i]);
}
return 0;
}
int count1 = 10;
int count2 = 15;
int arNums1[];
int arNums2[];
int arMergeNums[];
arNums1 = new int[count1];
arNums2 = new int[count2];
arMergeNums = new int[count1 + count2];
//POPULATE YOUR FIRST TWO ARRAYS HERE...
//FILL YOUR MERGED ARRAY LIKE THIS:
for (int i = 0; i < (count1 + count2); i++)
{
if ( i < count1 )
arMergeNums[i] = arNums1[i];
else arMergeNums[i] = arNums2[i];
}
//THEN SORT IT LIKE THIS:
for (int i = 0; i < (count1 + count2); i++)
{
for (int j = i + 1; j < (count1 + count2); j++)
{
if (arMergeNums[i] < arMergeNums[j])
{
int temp = arMergeNums[i];
arMergeNums[i] = arMergeNums[j];
arMergeNums[j] = temp;
temp = null;
}
}
}
That's it...
$(document).ready(function(){
var cars = [4,3,9,6];
for(i=0;i<cars.length;i++){
//var carsrev=cars[cars.length-i-1];
//alert(carsrev);
$('<li>'+cars[cars.length-1-i]+'</li>').appendTo("ul.demo");
//alert($("ul.demo li").length);
}
});
//Out Put is 6 9 3 4 not 9 6 4 3 its just reverse

Array index out of bounds in sieve of eratosthenes

I am having trouble with ArrayIndexOutOfBounds.
I am new to coding and I don't fully understand this problem and I am unable to fix it. Any help is appreciated.
public class Primes {
public static void main(String[] args) {
final int SIZE = 10000;
boolean[] numberIsPrime = new boolean[SIZE];
int rowCounter = 0;
for( int index = 1; index <= SIZE; index++) {
numberIsPrime[index] = true;
}
for( int index = 2; index <= SIZE; index++) {
if( numberIsPrime[index] = true){
for( int i = index; i <= SIZE; i++){
numberIsPrime[index * i] = false;
}
}
}
for( int index = 1; index <= SIZE; index++){
if( numberIsPrime[index] = true){
System.out.println(index + " ");
rowCounter++;
if( rowCounter == 10){
System.out.println();
}
}
}
}
}
You should tag the question with the language you're using, but I'm gonna assume it's Java. The problem is
boolean[] numberIsPrime = new boolean[SIZE];
...
for( int index = 1; index <= SIZE; index++) {
numberIsPrime[index] = true;
}
The first line declares numberIsPrime as an array of size 10000. That means you can access numberIsPrime[0], numberIsPrime[1], ... numberIsPrime[9999]. You can't access numberIsPrime[10000].

flipped 2D array not filling properly

My 2D array will not fill properly I want the original array to stay unmodified, which it does, but then the next array to be flipped. For example 111, 222, 333 should be 333, 222, 111. If anyone could help it would be appreciated.
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int rows = 0, cols = 0;
System.out.println("Please enter the size or your rows first and then columns:");
Scanner input = new Scanner (System.in);
rows = input.nextInt();
cols = input.nextInt();
int [][] my2DArray = new int [rows][cols];
System.out.print("Now fill the array with the numbers you would like: ");
for(int i = 0; i < my2DArray.length; i++)
{
for(int j = 0; j < my2DArray[i].length; j++)
{
my2DArray[i][j] = input.nextInt();
}
System.out.println("");
}
System.out.println("Here is the origional unmodified array.");
for(int i = 0; i < my2DArray.length; i++)
{
for(int j = 0; j < my2DArray[i].length; j++)
{
System.out.print(my2DArray[i][j] + " ");
}
System.out.println("");
}
flipMy2DArray(my2DArray);
}
public static int[][] flipMy2DArray(int[][] inMy2DArray)
{
int [][] flipMy2DArray = new int [inMy2DArray.length][inMy2DArray.length];
for(int row = flipMy2DArray.length-1; row > 0; row--)
{
for(int cols = 0; cols < flipMy2DArray[row].length; cols++)
{
flipMy2DArray[row][cols] = inMy2DArray[row][cols];
}
System.out.println("");
}
for(int row = 0; row < flipMy2DArray.length; row ++)
{
for(int cols = 0; cols < flipMy2DArray[row].length; cols++)
{
System.out.print(flipMy2DArray[row][cols]+ " ");
}
System.out.println("");
}
return flipMy2DArray;
}
}

Resources