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 5 years ago.
Improve this question
If i want to generate 0,1,2 randomly in a 2D matrix but i want to control the count of 0,1,2. Is this can be done by any inbuilt c function??(I am using C and new to programming.)
Let's suppose that you have a 3x3 matrix and you want three each of 0, 1, 2. The most practical way to do this is to fill in the matrix with the right number of numbers in a non-random way, like
0 0 0
1 1 1
2 2 2
and then use a Fisher-Yates shuffle to put them in a random order. You will have to implement the shuffle yourself, but you can use rand() in the normal way in your implementation.
This works for any situation where a fixed number of fixed values need to be put into random positions within a list.
Related
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 3 years ago.
Improve this question
An array of integer is given of size n. We have to make all the equal. For this we can add 1, 2 or 5 to the array element any time and to any element. We have to find minimum no of operation to do so?
For example
Array. 2 2 3 7
Output should be 2
Explanation
In first operation we add 1 to 2,2,7
After that array will be as 3 3 3 8
Now in 2nd operation we add 5 to 3,3,3
After that array will be as 8,8,8,8
You could first think about how many operations it would cost to get all values to the highest value in the list.
In a second step you can think if there could be a better solution if you try to reach highest-value+1, highest-value+2, highest-value+3, highest-value+4 .
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 6 years ago.
Improve this question
So I created a linked List in C using structs and it stores ints. My mission is to calculate the average of the values in the list without using recursion or loops.
I already have the list's item count I just need the sum.
Any ideas?
Simply have two varibles - count, total; Update them in Add and Delete.
When u want avg, just return total/count.
The list is not length bounded, but i found a solution.
I create a variable in the list's ateuct to save the sum of the list, and i change the sum of the list each time I add or remove a cell. When I want to calculate the sums I'll just divide the sum by the count.
Thank you anyway for your help :)
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 6 years ago.
Improve this question
I want to print out each integer of an int individually using a for loop. I am doing this because I want to print things in between some of the single ints. So if the number was 4564, I want to print out 4 5 6 4. Is there a quick way to do this?
I know how to do it in java but I am new to C and am not sure.
Something like this shall help
while(num!=0) {
printf("%d", num%10); //your last digit, you can store it in an array of characters as well
num = num/10 ;
}
Note : you've got to reverse the order while using the digits
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 a matrix og 9061x16 i want to reshape to 6x24x??x16. currently the 9061x16 explains this: 16 is the amount of customers.. and 9061 is how much data each customer have.
What i want from the reshape:
since the data is collected every 10 min. for a duration of 62 days and some hours which the ?? is. Since in 1 hour there is collected 6 data. the matrix will therefore be like 6x24xdaysx16..
please do help i really need this :(.
the x.mat is what i reshaped 9061x16 from the V.mat
(The data can be downloaded from this post: Data of x.mat and v.mat
You could extend x to a number of rows multiple of 6*24, and then use reshape:
x(ceil(size(x,1)/144)*144, end) = 0; %// extend x, filling with zeros
x = reshape(x, 6, 24, [], 16); %// reshape x into desired form
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 9 years ago.
Improve this question
I'm trying to decimate a given vector with a logarithmic rule instead of a linear one.
e.g.:
The given vector has 100 elements. I want to reduce it to a vector of 10 elements that are the same elements of the starting vector taken with a logarithmic rule on the int interval [0,99].
I hope to be clear enough. Any help is appreciated !
Have a nice day !
In pseudo-code :
int n; // Length of subsampled array
for i:
new_array[i] = array[(int)(log(i)/log(n)*length(array))]
Is that what you are trying to do ?