Difference between c and java on simple array functions? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Besides some obvious difference like difference [] placement, are there any fundamental difference between array in java and in c?
I wrote small program that calculates sum to compare them.
//java
public static int arraySum(int[] a) {
int sum = 0;
for (int i = 0; i < a.length ; i++) {
sum = sum + a[i];
}
return sum;
}
public static void main(String[] args) {
int[] tryArray = {1,2,3,4,5};
System.out.println(arraySum(tryArray));
}
//c
int sum_array(int a[], int len) {
int sum = 0;
for (int i = 0; i < len; ++i) {
sum += a[i];
}
return sum;
}
int main(void) {
int my_array[5] = {1,2,3,4,5};
trace_int(sum_array(my_array, 5)); // any ways to remove the 5(length) from the parameter?
}
Also, if I use char arrays in c, it looks like string in java? char posi_string[3] = "str"
Can I use this method to manipulate string in c?

Any ways to remove the length from the parameter?
No. When you pass an array to a function in C, it is no longer assumed as an array but a pointer. Since it's a pointer, you can no longer evaluate its size with sizeof(arr) / sizeof(arr[0]) – thus, you just need to pass another parameter, for example, size to tell the array size to the function.
If I use char arrays in C, does it looks like a string in Java?
No. Java has a String class that is rich in built-in code. Unlikely in C, char is a primitive data type, and it has no functions, you need to create one manually.

Related

How to create a 2D Array in Haskell? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm struggling to create a 2D list in Haskell with elements are formed by their indices of row and column. I searched but did not find any solutions that can track the indices and use them to calculate the values to push in the array. The recursion traversal x:xs is not suitable for this problem. Please help. Thank you.
Function in Java:
public static int[][] create2DArray(int r, int c) {
int[][] arr = new int[r][c];
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
arr[i][j] = i + j;
}
}
return arr;
}
Haskell:
create2DArray:: Int -> Int -> [[Int]]
...
Output:
0 1 2
1 2 3
2 3 4
create2DArray r c = [ [i+j | j<-[0..c-1]] | i<-[0..r-1] ]
In Haskell, if you really want an array similar to Java's counterpart, Data.Array is what you want. For your example, the array can be contructed as follows:
create2DArray :: Int -> Int -> Array (Int, Int) Int
create2DArray r c = array ((0,0), (r-1,c-1)) [((i,j), i+j) | i <- [0..r-1], j <- [0..c]]

How do you take in characters at a time from a string and set up a for loop, runs the number of the second character to print out the 1st character [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am looking to write a function that prints out a character a certain amount of times.
I am thinking to take in the first two characters from a string and setting up a for loop that runs the number of the second character to print out the first character.
I have this so far but seem to be getting nowhere
public static String output(String a, String b,String c,String d) {
int a= Integer.parseInt(a.substring(0, 2));
for(int i=0;i<a;i++) {
}
If you divide the length of your String by two, you can get at the base number of letter/number pairs that you have. Then you can use a for loop which increments by two each time, and pulls out the next set of two chars and processes them. Something like this:
public static String output(String a) {
String output = "";
int length = a.length();
for (int i = 0; i < length; i+=2) {
String tempString = a.substring(i,i+2);
int x = Integer.parseInt(tempString.substring(1));
for (int t = 0; t < x; t++) {
output += tempString.substring(0,1);
}
}
return output;
}

How to initialize this array so that it works in the while loop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to initialize an array above a while loop and then use the array set the values of the array in the while loop. But netbeans keeps giving me an error message.
public static void main(String[] args) {
int a=999;
int b=999;
int c=0;
int[] array = new int[5];
int[] array2= new int[6];
while(c<=998001){
c=a*b;
if(c<100000){
array[]={c%100000,c%10000,c%1000,c%100,c%10}
//netbeans keeps telling me that this array is "not a statement"
";" expected. Why does it tell me this?//
}
if(array[0]==array[5] & array[1]==array[3]){
int d=c;
}
}
}
Because your syntax isn't valid Java. It seems you want to create a new 5 element array with your statement. You could do so like
array = new int[] { c % 100000, c % 10000, c % 1000, c % 100, c % 10 };
But since you always have five elements, it would probably be more efficient to use a loop and re-use the same array. Like,
if (c < 100000) {
int fac = 100000;
for (int i = 0; i < array.length; i++) {
array[i] = c % fac;
fac /= 10;
}
}

List the factors of an integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public int[] factors(int n) {
int count[] = new int[n];
for (int i = 1; i <= count.length; i++) {
count[i] = (count.length % i);
}
return count;
}
}
Anyone have an idea how to list all the factors?
set int i = 0; as array starts from 0
Factor can only be considered if value % i==0 so add if condition
You need one more variable to increment array Index as you can't use i for that.So add one more variable to increment array index to add factor.
Think more on your code, debug especially if possible,check out the values ,add print statements if needed to see what's going on and that's it you will definitely win the task.
Other than that if you can use SOF you can use Google as well :)
You should use a List instead of array. Since you can't initialize the array without knowing the size.
Then you can try something like following
public List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>(); // initialize the list
for (int i = 1; i <= n; i++) {
if (n % i == 0) { // decide the factor.
list.add(i); // now i is a factor, needs to add to list
}
}
return list; // return list contains factors.
}

Populate int array with for loop in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array named numbers that I want to populate with a for loop:
int[] numbers;
for ( int i = 0; i <=10; i++)
{
// want to populate the array with a sequence of 0-10
}
How can I populate the 11 values generated from the above for loop into my array?
If you are using Java 7 or lower, do this:
int[] numbers = new int[11];
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}
For Java 8, there is a more concise way to do this:
int[] numbers = IntStream.rangeClosed(0, 10).toArray()
First you need to define what numbers is, you have only declared it.
int[] numbers = new int[11];
Then insert the values you want.
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}

Categories