I can't display correctly my arrays, I don't understand what the problem is.
Here is my code :
int increment = 1;
int para_1_courant = 10;
int para_2_courant = 4;
int para_1_min = 5;
int para_1_max = 10;
int para_2_min = 1;
int para_2_max = 4;
int tab_para_automate[2][2] = {{0}};
int tab_para_application[1][3] = {{0}};
tab_para_automate[0][0] = para_1_min;
tab_para_automate[0][1] = para_1_max;
tab_para_automate[1][0] = para_2_min;
tab_para_automate[1][1] = para_2_max;
printf("coucou1");
tab_para_application[0][0] = para_1_courant;
tab_para_application[0][1] = para_2_courant;
tab_para_application[0][2] = increment;
printf("coucou2\n");
int k,l;
for (k=0 ; k<1 ; k++)
{
for (l=0 ; l<1 ; l++)
{
printf("%d\n", tab_para_automate[k][l]);
}
}
In output I only get this : "coucou1", "coucou2", 5
The loop:
for (k=0 ; k<1 ; k++)
goes until k < 1, so just for k = 0, and similarly it goes for l = 0, so you get just one iteration to display the element at tab_para_automate[0][0].
You should make both loops condition < 2 if you wnat to show all elements.
for (k=0 ; k<2 ; k++)
{
for (l=0 ; l<2 ; l++)
{
printf("%d\n", tab_para_automate[k][l]);
}
}
You mistyped size of matrix
You should rewrite the code on this way to take care about index 1
for (k=0 ; k<=1 ; k++)
{
for (l=0 ; l<=1 ; l++)
{
printf("%d\n", tab_para_automate[k][l]);
}
}
Related
First I generate some random numbers and then i need to exchange them like I discribte in the following lines.
I tryed it iut with the for loobs.
I have to exchange the numbers of the array
Change number 1 and 2, 3 and 4,.....29 and 30
Change number 1 and 3, 4 and 7,.....27 and 30
Thank you for your Help
srand(time(NULL));
for ( i = 0; i < SIZE; i++ )
{
mainArray[i] = rand() % ( UPPERBOUND - LOWERBOUND + 1 ) + LOWERBOUND;
}
for ( i = 0; i < SIZE; i++)
{
if ( countDigitChange == 2 )
{
digitChanger1 = workArray2[i];
i++;
digitChanger2 = workArray2[i];
workArray2[i] = digitChanger1;
i--;
workArray2[i] = digitChanger2;
countDigitChange = 0;
}
countDigitChange++;
}
for ( i = 0; i < SIZE; i++)
{
if ( countDigitChange % 3 == 0 )
{
digitChanger1 = workArray3[i];
i += 2;
digitChanger2 = workArray3[i];
workArray3[i] = digitChanger1;
i += 2;
workArray3[i] = digitChanger2;
countDigitChange = 0;
}
countDigitChange++;
}
This seems much simpler:
Declare a helper function:
void swap_int(int* x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
Then in your code that needs to shuffle the array:
int i, j; // declare this up top with all your other variable declarations
// for each pair of elements swap them
for (i=0, j=1; j < SIZE; i+=2, j+=2)
{
swap_int(&mainArray[i], &mainArray[j]);
}
// swap array[0] with array[2], then swap array[3] with array[5], etc...
for (i=0, j=2; j < SIZE; i+=3, j+=3)
{
swap_int(&mainArray[i], &mainArray[j]);
}
I am trying to find the location of a target inside of a 1-D array that acts like a table with rows and cols. I could do it using divide and mod, but I am stuck on finding it using nested loops. specifically, I can't seem to assign values inside the nested loop.
here is my code:
#include <stdio.h>
int main()
{
int arr[9] = // act as a 3 X 3 table
{ 2, 34, 6,
7, 45, 45,
35,65, 2
};
int target = 7;// r = 1; c = 0
int r = 0; // row of the target
int c = 0; // col of the target
int rows = 3;
int cols = 3;
for (int i = 0; i < rows; i++){
for (int j = 0; j + i * cols < cols + i * cols; i++ ){
if (arr[j] == target){
c = j; // columns of the target
r = i; // rows of the target
}
}
}
printf ("%d, %d",c, r);
return 0;
}
The code outputs: 0,0.
The problem isn't with the assignment, it's with the wrong loop and if condition.
The outer loop should loop over the i rows
The inner loop should loop over the j columns
within both loops, the cell to evaluate is i * cols + j
Put it all together and you'll get:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++ ) {
if (arr[i * cols + j] == target) {
c = j; // columns of the target
r = i; // rows of the target
}
}
}
Since arr is 1D array and inside for loop, for any i value j will reach upto max 3 only so its not checking after arr[3]
To avoid this problem take int pointer and points to arr and do the operation as below
int *p = arr;
for (i = 0; i < rows; i++){
for ( j = 0; j < cols ; j++ ){
if (p[j] == target){
c = j; // columns of the target
r = i; // rows of the target
}
}
p = p + j;/*make p to points to next row */
}
A better solution would use only one loop:
for (int i = 0; i < rows * cols; i++){
if (arr[i] == target){
r = i / 3;
c = i % r;
}
}
I have a 3d array of ints stored in a struct which represents a tetris block:
typedef struct
{
int orientations[4][4][4];
dimension dimensions[4];
int i;
int x;
int y;
}block;
orientations is filled with every possible position for the block and dimension is a struct that provides information for collision checking:
typedef struct
{
int left, right, bottom;
}dimension;
each orientation and dimension should be linked by the block's i value. For some reason orientations (but not dimensions) seems to be reversed. Does anybody know why this is?
here is how I assign values to dimensions:
block* shape = malloc(sizeof(block));
shape->dimensions[0].left = 0;
shape->dimensions[0].right = 3;
shape->dimensions[0].bottom = 1;
shape->dimensions[1].left = 2;
shape->dimensions[1].right = 2;
shape->dimensions[1].bottom = 3;
shape->dimensions[2].left = 0;
shape->dimensions[2].right = 3;
shape->dimensions[2].bottom = 2;
shape->dimensions[3].left = 1;
shape->dimensions[3].right = 1;
shape->dimensions[3].bottom = 3;
and orientations:
int first[4][4] = {{0,0,0,0}, {2,2,2,2}, {0,0,0,0}, {0,0,0,0}};
int second[4][4] = {{0,0,2,0},{0,0,2,0},{0,0,2,0},{0,0,2,0}};
int third[4][4] = {{0,0,0,0},{0,0,0,0},{2,2,2,2},{0,0,0,0}};
int fourth[4][4] = {{0,2,0,0},{0,2,0,0},{0,2,0,0},{0,2,0,0}};
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[0][i][j] = first[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[1][i][j] = second[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[2][i][j] = third[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[3][i][j] = fourth[i][j];
}
}
here is how I'm accessing the array:
shape->orientations[current->i][i][j]
when I attempt to access shape->orientations[3] it returns the values I set to shape->orientations[0] earlier. Any help would be greatly appreciated, thanks in advance.
The reason is that your array is indexed [orientation][row][column], but you are interpreting it as [orientation][x][y] when you should be interpreting it as [orientation][y][x]. So you are getting a pattern with the x and y swapped, which appears as if it has been rotated to a different orientation (actually it's equivalent to a rotation and a flip).
int main(array<System::String ^> ^args)
{
int arr[]={1,2,3,2,9,8,1,2,3,9};
int a[9];
int size = sizeof(arr)/sizeof(arr[0]);
int str[256]= {'\0'};
int i = 0;
for(i ; i < size ; i++)
{
if(str[arr[i]] == 0 )
str[arr[i]]= 1;
}
for( i = 0 ; i < size ; i++)
{
if( str[arr[i]] == 1)
{
a[i] = arr[i];
}
}
for(i=0 ; i < size ; i++)
{
printf("%d->",a[i]);
}
return 0;
}
still in the new array a,I am getting old data...not sure wats missing here...
Any help would be highly appreciated.
Thanks in advance.
Logical mistakes
the final array won't go till size
the indexing for a is wrong
Simply use one loop to achieve this, analyze following :
int i = 0,j=0;
for( ; i < size ; i++)
{
if(str[arr[i]] == 0 )
{
str[arr[i]]= 1;
a[j++] = arr[i];
}
}
Now iterate till j on final array a
Hello i have a little problem trying to "mix" two string arrays, i have searched about it but i have only found how to merge them or concatenate them, but thats not what i need.
i have two int arrays like this:
int no_items = 5;
int parent1[no_items], parent2[no_items];
if the arrays contains for example:
parent1[0] = 1;
parent1[1] = 2;
parent1[2] = 3;
parent1[3] = 4;
parent1[4] = 5;
and:
parent2[0] = 5;
parent2[1] = 1;
parent2[2] = 2;
parent2[3] = 3;
parent2[4] = 4;
given a "cross" point, for example 2:
parent1 should have his 2 first elements and the rest of parent2, and parent2 should have his first 2 elements and the rest of parent1. So the result should be:
parent1: 1,2 | 5,3,4
parent2: 5,1 | 2,3,4
where "|" is the break point index and the rest of elements should not be repeated.
How can i get this kind of mixing two int arrays? Thanks you!
at the moment i have this:
for(i = 0; i < cross_point; i++)
{
sprintf(buffer, "%d,", parent1[i]);
strcat(line1, buffer);
}
for(i = 0; i < cross_point; i++)
{
sprintf(buffer, "%d,", parent2[i]);
strcat(line2, buffer);
}
but i donĀ“t know how to go further than the cross point.
int *find(int *begin, int *end, int value)
{
int *p = begin;
for ( ; p != end; ++p)
if (*p == value) break;
return p;
}
int i, j;
int output1[no_items] = {0};
int output2[no_items] = {0};
int crosspoint = 3;
memcpy(output1, parent1, crosspoint * sizeof(int));
for (i = crosspoint, j = 0; i < no_items; ++i)
{
while (find(output1, output1+i, parent2[j]) != output1+i) ++j;
output1[i] = parent2[j];
}
memcpy(output2, parent2, crosspoint * sizeof(int));
for (i = crosspoint, j = 0; i < no_items; ++i)
{
while (find(output2, output2+i, parent1[j]) != output2+i) ++j;
output2[i] = parent1[j];
}
memcpy(parent1, output1, sizeof(parent1));
memcpy(parent2, output2, sizeof(parent2));
Demo: http://ideone.com/xUt6nQ
something like this should do it if you aren't concerned about creating temporaries:
int no_items = 5;
int output1[no_items];
int output2[no_items];
for (int i = 0; i < no_items; i++){
if(i < crosspoint){
output1[i] = parent1[i];
output2[i] = parent2[i];
}else{
output1[i] = parent2[i];
output2[i] = parent1[i];
}
}
If you are concerned about temporaries you need to swap the values, the logic should be fairly similar to above.
int no_items = 5;
int temp = 0;
for (int i = 0; i < no_items; i++){
if(i < crosspoint){
/* don't need to do anything here */
}else{
temp = parent1[i];
parent1[i] = parent2[i];
parent2[i] = temp;
}
}