Skip to content

Commit

Permalink
Histogram problem done
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypatel-208 committed Nov 24, 2023
1 parent 8dd317d commit e78697b
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Between0and1115.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Write a code fragment that prints true if the double variables x and y are both
* strictly between 0 and 1 and false otherwise.
*/
public class Between0and1115 {
public static void main(String[] args) {
// Check if there are exactly 2 command-line arguments
if (args.length != 2) {
System.out.println("Please provide exactly three integer command-line arguments.");
return;
}

try {
double X = Double.parseDouble(args[0]);
double Y = Double.parseDouble(args[1]);

System.out.println(X < 1.0 && X > 0.0 && Y < 1.0 && Y > 0.0);
} catch (NumberFormatException e) {
System.out.println("Please provide valid integer values as command-line arguments.");
}
}
}
27 changes: 27 additions & 0 deletions src/Equal113.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Write a program that takes three integer command-line arguments and prints
* equal if all three are equal, and not equal otherwise.
*/
public class Equal113 {
public static void main(String[] args) {
// Check if there are exactly 3 command-line arguments
if (args.length != 3) {
System.out.println("Please provide exactly three integer command-line arguments.");
return;
}

try {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);

if (a == b && b == c) {
System.out.println("equal");
} else {
System.out.println("not equal");
}
} catch (NumberFormatException e) {
System.out.println("Please provide valid integer values as command-line arguments.");
}
}
}
34 changes: 34 additions & 0 deletions src/Histogram1115.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.Arrays;

/**
* Write a static method histogram() that takes an array a[] of int values and
* an integer M as arguments and returns an array of length M whose ith entry is the number
* of times the integer i appeared in the argument array. If the values in a[] are all
* between 0 and M–1, the sum of the values in the returned array should be equal to
* a.length.
*/
public class Histogram1115 {
public static int[] histogram(int[] a, int M) {
int[] result = new int[M];

for (int value : a) {
if (value >= 0 && value < M) {
result[value]++;
} else {
System.out.println("Value " + value + " is out of range (0 to M-1). Ignoring.");
}
}

return result;
}

public static void main(String[] args) {
int[] inputArray = {1, 2, 3, 2, 1, 0, 4, 4, 2, 0};
int M = 5;

int[] resultArray = histogram(inputArray, M);

// Print the histogram array
System.out.println("Histogram Array: " + Arrays.toString(resultArray));
}
}
19 changes: 19 additions & 0 deletions src/NewtonRaphsonSquareRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class NewtonRaphsonSquareRoot {
public static double squareRoot(double S, double epsilon) {
double x = S / 2.0;

while (Math.abs(x * x - S) > epsilon) {
x = 0.5 * (x + S / x);
}

return x;
}

public static void main(String[] args) {
double number = 16.0;
double precision = 0.01;

double result = squareRoot(number, precision);
System.out.println("Square root of " + number + " is : " + result);
}
}
11 changes: 11 additions & 0 deletions src/Print116.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Print116 {
public static void main(String[] args) {
int f = 0;
int g = 1;
for (int i = 0; i <= 15; i++) {
System.out.println(f);
f = f + g;
g = f - g;
}
}
}
15 changes: 15 additions & 0 deletions src/PrintSagar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Print 1 to 100 without using any for loop
*/
public class PrintSagar {
private static void printDigits(int n) {
if (n <= 100) {
System.out.println(n);
printDigits(n + 1);
}
}

public static void main(String[] args) {
printDigits(1);
}
}
20 changes: 20 additions & 0 deletions src/Printed117.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Printed117 {
public static void main(String[] args) {
double t = 9.0;
while (Math.abs(t - 9.0/t) > .001)
t = (9.0/t + t) / 2.0;
System.out.printf("%.5f\n", t);

int sum = 0;
for (int i = 1; i < 1000; i++)
for (int j = 0; j < i; j++)
sum++;
System.out.println(sum);

int sum1 = 0;
for (int i = 1; i < 1000; i *= 2)
for (int j = 0; j < 5; j++)
sum1++;
System.out.println(sum1);
}
}
27 changes: 27 additions & 0 deletions src/Representation119.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Write a code fragment that puts the binary representation of a positive integer N
* into a String s.
*/
public class Representation119 {
public static void main(String[] args) {
int N = 15;
String binaryRepresentation = getBinaryRepresentation(N);
System.out.println("Binary representation of " + N + " is: " + binaryRepresentation);
}

private static String getBinaryRepresentation(int N) {
if (N == 0) {
return "0";
}

StringBuilder binary = new StringBuilder();

while (N > 0) {
int remainder = N % 2;
binary.insert(0, remainder);
N = N / 2;
}

return binary.toString();
}
}
39 changes: 39 additions & 0 deletions src/ReverseArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Reverse an array in java
*/
public class ReverseArray {
public static void reverse(int[] array) {
int start = 0;
int end = array.length - 1;

while (start < end) {
//Swap element at start and end indices
int temp = array[start];
array[start] = array[end];
array[end] = temp;

//Moving toward to center
start++;
end--;
}
}

public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5};

System.out.println("Original array:");
for (int num : myArray) {
System.out.print(num + " ");
}

// Reverse the array
reverse(myArray);

// Print reversed array
System.out.println();
System.out.println("Reversed array:");
for (int num : myArray) {
System.out.print(num + " ");
}
}
}
46 changes: 46 additions & 0 deletions src/Transposition113.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Write a code fragment to print the transposition (rows and columns changed)
* of a two-dimensional array with M rows and N columns.
*/
public class Transposition113 {
public static void printTranspose(int[][] array) {
int rows = array.length;
int columns = array[0].length;

// create new array to store the transposition
int[][] transposedArray = new int[columns][rows];

// fill the transposed array with values from the original array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transposedArray[j][i] = array[i][j];
}
}

// print the transposed array
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}

public static void main(String[] args) {
// create 3x4 two-dimensional array
int[][] exampleArray = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

System.out.println("Original Array:");

for (int[] row :exampleArray){
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}

// Print the transposed array
System.out.println("\nTransposed Array:");
printTranspose(exampleArray);
}
}
33 changes: 33 additions & 0 deletions src/Two_Dimensional1111.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Write a code fragment that prints the contents of a two-dimensional boolean
* array, using * to represent true and a space to represent false. Include row and column
* numbers.
*/
public class Two_Dimensional1111 {
public static void printBooleanArray(boolean[][] array) {
for (int i = 0; i < array.length; i++) {
//Print row numbers
System.out.print(i + ": ");

for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] ? '*' : ' ');

System.out.print(" ");
}
System.out.println();
}
//Print column numbers at bottom
System.out.println(" ");
for (int j = 0; j < array[0].length; j++) {
System.out.print(j + " ");
}
System.out.println();
}

public static void main(String[] args) {
boolean[][] exampleArray = {
{true, false, true, false}, {false, true, false, true}, {true, true, false, false}, {false, false, true, false}
};
printBooleanArray(exampleArray);
}
}

0 comments on commit e78697b

Please sign in to comment.