Copy i-value from array triple time - c

I have an array[768] but now I have only 256 (from 0 to 255) samples in this array. I want to copy each value from 0 to 255 and fill this array better, I mean:
[1][2][3] - > [1][1][1][2][2][2][3][3][3]
How I can do that? Is there a library function that can do this?

I don't recall any known library function capable of doing this.
If you want to do it in-place, I'd do it from right to left (i.e. tail to head), I think this is the only way to do it in-place:
int i, j;
for (i = 255, j = 767; i > 0; i--) {
for (int k = 0; k < 3; k++) {
array[j--] = array[i];
}
}
If you don't need to do it in-place, this would suffice:
for (int i = 0, j = 0; i < 256; i++) {
for (int k = 0; k < 3; k++)
new_array[j++] = array[i];
}

You can do this:
for (int i = 0; i < 768; ++i)
new_array[i] = array[i/3];
The index of the right hand side of the assignment will vary only every three steps.

Related

C - Insertion Sort Algorithm

I'm trying to sort a set of numbers like this:
A[]={3,6,7,2,9,1,2,7,2}
A[]={3,6,7,2,2,2,9,1,7}
So I've made this:
void sort_min(int* point, int size_array, int min_n){
int i = 0;
int j = 0;
int k = 0;
while(point[i] != min_n){
i++;
}
j = i+1;
while(point[j] != min_n){
j++;
}
k = j;
for (j-1; j > i; j--){
point[j] = point[j-1];
}
point[j] = min_n;
j = k+1;
}
Like you can notice I've never used the int size_array cause I don't know the way to match an iterative function like a for or a while (That's the question. How to solve it?). I've done that, of course, but I've got a Segmentation fault like an answer.
The main concept is looking for a number int min_n and up to that point sort that number at each occurrence in the array.
Thanks for all.
You need use size_array like below, if you are asking about this.
You need compare i and j with size_array in while.
while (i < size_array && point[i] != min_n) {
i++
}
Need check value of i, j after while. They may be larger or equal to size_array.
while (i < size_array && point[i] != min_n) {
i++
}
// I guess when you don't find min_n, function can just return.
if (i >= size_array)
return;
j = i+1;
while(j < size_array && point[j] != min_n){ // Also need check j's value.
j++;
}
// Also guess when can't find the second min_n position, function can return.
if (j >= size_array)
return;
k = j
for (; j > i; j--) // No need j-1.
point[j] = point[j-1];
// This is useless. When code come here, j == i and point[i] == min_n;
point[j] = min_n;
j = k+1;
Try the Insertion Sort with the code as follows..
void sort_min(int point[], int size_array)
{
int i, key, j;
for (i = 1; i < size_array; i++)
{
key = point[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && point[j] > key)
{
point[j+1] = point[j];
j = j-1;
}
point[j+1] = key;
}
}

Beginner in C - Array of strings lost content after initialization in cyklus

I am trying to make array of strings each representing one card of a poker deck from 2 strings (ranks, colors). If I try print card immidietly after assignment it's ok but if I try it after all assignments nothing happend.
My "code":
int main(void)
{
char rank[] = "23456789TJQKA";
char color[] = "cdhs";
char deck[52][3];
int k = 0;
for (int i = 0; i < 13; i++) {
for(int j = 0; j < 4; j++) {
deck[k][0] = rank[i];
deck[k][1] = color[j];
deck[k][2] = 0;
k++;
printf("%s\n",deck[k-1]); // this print works
}
}
printf("%s\n",deck[0]); //this does nothing (even if I change index)
//-------------------------- here I am trying make all possible pairs but deck is now empty :(
k = 0;
char allPairs[1327][5];
for (int i = 0; i < 51; i++) {
for (int j = 0; j < 52; j++) { //**edit** - thanks ;)
allPairs[k][0] = deck[i][0];
allPairs[k][1] = deck[i][1];
allPairs[k][2] = deck[j][0];
allPairs[k][3] = deck[j][1];
allPairs[k][4] = 0;
k++;
}
}
}
All seems to work now thanks guys!
What you need to do is replace i++ with j++ in the following statement
for (int j = 0; j < 3; i++)
and also comment out the following line as it is printing 2c again:
printf("%s\n",deck[0]); //this does nothing (even if I change index)

Is the statement *a[i]++ /= K valid?

Array initialization Code:
int m = 100;
int n = 50;
int i = 0, j = 0;
float **a = (float**)malloc(m*sizeof(float*));
for (i = 0; i < m; i++)
{
a[i] = (float*)malloc(n*sizeof(float));
for (j = 0; j < n; j++)
a[i][j] = i + j;
}
a is a 2D array and I want to traverse and update the elements of the 1D array a[0]
Say I want to divide all elements of a[0] by 2:
for (i = 0; i < n; i++)
*a[0]++ /= 2; // instead of a[0][i] /= 2;
This doesn't seem to work..
I guess a is a 2 dimensional array like int a[10][20], then the given statement a[i]++ is "invalid".
The reason is that since a[i] being an array a[i] is a non-modifiable 'lvalue'.
In the above case *a[i] is valid but not the a[i]++
Yes, although it look like a homework. If you want more interesting code, you could write this without spaces, as *a[i]++/=K, and finally you could attach this to the containing loop, as for example while(p=a[i]++)*p/=K; to make things more compressed. ;-)

Code to store indices of occurrences doesn't work

In the program that I'm writing, I currently have a for-loop that goes through an array, num[5], and checks to see if there are any 1's in that array, which looks like:
int counter = 0;
for (int i = 1; i <= 5; i++)
if (num[i] == 1)
counter++;
This works successfully, but I'm now trying to go through the array and see what the indices of the 1's in the program are. So, if I have 01001, I want to make an array that holds the positions of the 1's. The following is what I've tried so far:
int b[counter];
for (int k = 0; k <= counter; k++) {
for (i = 0; i <= 5; i++) {
if (num[i] == 1) {
b[k] = i;
}
}
}
but this doesn't produce the desired result. When I type the string in, say 1001, I get 444. Can someone tell me what I'm doing incorrectly?
For each value of k, for each occurrence of a 1, you're setting b[k] to the index of the 1. Thus each b[k] will have the index of the last 1.
Try:
int b[counter];
int k = 0;
for (i = 0; i <= 5; i++) {
if (num[i] == 1) {
b[k++] = i;
}
}
So, whenever it gets a 1, it assigns b[k] to the index and then increases k.
Then you should also use k, not counter, when trying to print out b.
The problem lies in this part of your code.
int b[counter];
for (int k = 0; k <= counter; k++) {
**for (i = 0; i <= 5; i++) {
if (num[i] == 1) {
b[k] = i;
}**
}
}
Suppose you get a 1 at the index 1 of the array as in 01001. You assign b[k] = 1;
This is perfectly valid. But as the loop continues you get another 1 at the index 4. Thus the command b[k] = 4; is again executed.
Note that your value of k is constant in both the statements and hence you get the array b as 44.
So what you need to do is break the inner for loop as soon as you get a 1.
Here is the modified code. You also need to keep a track of the iterator i and I have done that here using the variable- new_pos // new position.
int b[counter];
int new_pos=0; //to keep track of the iterator
for (int k = 0; k <= counter; k++) {
for (i = new_pos; i <= 5; i++) {
if (num[i] == 1) {
b[k] = i;
new_pos = i+1;
break;
}
}
}
The code provided by Dukeling is also perfect, but I am just giving another way to make your own code work.

What is the best way to loop through a 2D sub-array of a 2D array?

If I have a 2D array, it is trivial to loop through the entire array, a row or a column by using for loops. However, occasionally, I need to traverse an arbitrary 2D sub-array.
A great example would be sudoku in which I might store an entire grid in a 2D array but then need to analyse each individual block of 9 squares. Currently, I would do something like the following:
for(i = 0; i < 9; i += 3) {
for(j = 0; j < 9; j += 3) {
for(k = 0; k < 3; k++) {
for(m = 0; m < 3; m++) {
block[m][k] == grid[j + m][i + k];
}
}
//At this point in each iteration of i/j we will have a 2D array in block
//which we can then iterate over using more for loops.
}
}
Is there a better way to iterate over arbitrary sub-arrays especially when they occur in a regular pattern such as above?
The performance on this loop structure will be horrendous. Consider the inner most loop:
for(m = 0; m < 3; m++) {
block[m][k] == grid[j + m][i + k];
}
C is "row-major" ordered, which means that accessing block will cause a cache miss on each iteration! That's because the memory is not accessed contiguously.
There's a similar issue for grid. Your nested loop order is to fix i before varying j, yet you are accessing grid on j as the row. This again is not contiguous and will cache miss on every iteration.
So a rule of thumb for when dealing with nested loops and multidimensional arrays is to place the loop indices and array indices in the same order. For your code, that's
for(j = 0; j < 9; j += 3) {
for(m = 0; m < 3; m++) {
for(i = 0; i < 9; i += 3) {
for(k = 0; k < 3; k++) {
block[m][k] == grid[j + m][i + k];
}
}
// make sure you access everything so that order doesn't change
// your program's semantics
}
}
Well in the case of sudoku couldn't you just store 9 3x3 arrays. Then you don't need to bother with sub arrays... If you start moving to much larger grids than sudoku you would improve cache performance this way as well.
Ignoring that, your code above works fine.
Imagine you have a 2D array a[n][m]. In order to loop a subarray q x r whose upper right corner is at position x,y use:
for(int i = x; i < n && i < x + q; ++i)
for(int j = y; j < m && j < y + r; ++j)
{
///
}
For your sudoku example, you could do this
for(int i = 0; i<3; ++i)
for(int j = 0; j < 3; ++j)
for(int locali = 0; locali < 3; ++locali)
for(int localj = 0; localkj <3; ++localj)
//the locali,localj element of the bigger i,j 3X3 square is
a[3*i + locali][3*j+localj]

Resources