C data format redoing - c

I have data stored as
float testdata3[][7] = {
{171032, 0.4448, -0.3032, -0.7655, -1.3428, 13.5803, -73.0743},
{172292, 0.0099, 0.1470, -0.7301, -17.2272, 7.0038, -11.7722},
{173547, 0.0576, 0.1333, -0.8163, -2.7847, -9.5215, 8.1177 },
...
}
where I am only interested in the second, third and fourth indexes. How can I create a function where it would return for example first, second and third index as their own table from the testdata I have.
For example
testdata_x = {0.4448, 0.099, 0.0576, ...}
testdata_y = {-0.3032, 0.1470, 0.1333, ...}
testdata_z = {-0.7655, -0.7301, -0.8163, ...}
Help would be much appreciated. I am trying to read sensor values from test data and im only interested in acceleration values in x, y and z directions.

You didn't post any code so I'm going to be a bit more vague than normal.
If we create an array of 7 elements such as: int arr[7] = {1, 2, 3, 4, 5, 6, 7};
Accessing the 2nd, third, and 4th elements is as simple as arr[1], arr[2], arr[3] respectively.
That should help with gleaning the data from the existing arrays, but as for making your new array. I don't see any reason to use a multidimensional array instead of 3 regular arrays. All you need to do from here is find out the amount of arrays in testData in order to know how much to allocate and you should be on your way.

So you want to transpose a matrix? then just iterate in the reverse order
#include <stdio.h>
int main(void)
{
double arr[][7] = {
{171032, 0.4448, -0.3032, -0.7655, -1.3428, 13.5803, -73.0743},
{172292, 0.0099, 0.1470, -0.7301, -17.2272, 7.0038, -11.7722},
{173547, 0.0576, 0.1333, -0.8163, -2.7847, -9.5215, 8.1177},
};
#define ROWS sizeof arr / sizeof arr[0]
#define COLS sizeof arr[0] / sizeof arr[0][0]
double test[COLS][ROWS]; /* 7 x 3 */
for (size_t i = 0; i < COLS; i++) {
for (size_t j = 0; j < ROWS; j++) {
test[i][j] = arr[j][i];
}
}
return 0;
}

I don't know what you are looking for but here are several approaches starting from easiest one:
1) You can directly assign a value using indexes to the new array
2) If you need function you can allocate new array inside it assign all
values and return it from a function
It's supposed that indexes never change for x,y,z.
#include <stdio.h>
#include <stdlib.h>
enum
{
X,
Y,
Z
};
float testdata3[][7] = {
{171032, 0.4448, -0.3032, -0.7655, -1.3428, 13.5803, -73.0743},
{172292, 0.0099, 0.1470, -0.7301, -17.2272, 7.0038, -11.7722},
{173547, 0.0576, 0.1333, -0.8163, -2.7847, -9.5215, 8.1177 },
};
float *get_dataa(int coordinate)
{
float *data;
switch(coordinate) {
case X:
data = malloc(sizeof(float) * 3);
data[0] = testdata3[0][1];
data[1] = testdata3[1][1];
data[2] = testdata3[2][1];
return data;
default:
return NULL;
}
}
int main(void)
{
/* first way */
float testdata_X[] = { testdata3[0][1], testdata3[1][1], testdata3[2][1] };
printf("Test number 1 for X %0.4f, %0.4f, %0.4f\n", testdata_X[0], testdata_X[1], testdata_X[2]);
/* second */
float *testdata_xX = get_dataa(X);
printf("Test number 2 for X %0.4f, %0.4f, %0.4f\n", testdata_xX[0], testdata_xX[1], testdata_xX[2]);
free(testdata_xX);
return 0;
}
output:
Test number 1 for X 0.4448, 0.0099, 0.0576
Test number 2 for X 0.4448, 0.0099, 0.0576

Related

Compilation error - Array index is past the end of array blah.groupsToTrace[LENGTH_OF_A_MACRO] = ToTrace[number];

#include <stdio.h>
#define LENGTH_OF_A_MACRO 3
struct blahS
{
unsigned int groupsToTrace[LENGTH_OF_A_MACRO];
} blahS;
int main()
{
//Value hardcoded to 7 just for testing purpose. Otherwise value is assigned from another function
signed int trace = 7; //trace reads value of range [0-7] from a function
unsigned int ToTrace[LENGTH_OF_A_MACRO];
unsigned int number = 0;
unsigned int noOfGroups = 100;
if (trace != 0)
{
if ((trace == 1)) //b'001
{
ToTrace[number] = noOfGroups / 8;
number++;
}
if ((trace == 4)) //b'100
{
ToTrace[number] = noOfGroups / 2;
number++;
}
if ((trace == 7)) //b'111
{
ToTrace[number] = noOfGroups * 7 / 8;
number++;
}
}
struct blahS blah;
blah.groupsToTrace[LENGTH_OF_A_MACRO] = ToTrace[number]; //Compilation-error
return 0;
}
Basically this is an if-loop which checks and decides groupToTrace based on bit-mapping for a trace value allocated. At the last line i got a compilation error saying - Array index 3 is past the end of the array. I am assigning the calculated groupToTrace values to newPointer_p->groupsToTrace[3] where groupsToTrace[3] is stored in a struct
Question is I got a compilation error as mentioned above with out of bounds access to the array as i understand. But i don't understand where is the mistake.
Compiler version gcc.x86_64 4.8.5-39.el7 #GSS-RHEL7
Any clues or hints highly appreciated. Thanks in Advance!
You seem to misunderstand what this line is doing:
blah.groupsToTrace[LENGTH_OF_A_MACRO] = ToTrace[number];
This is not copying the entire contents of one array to another. It is copying index number of ToTrace to index LENGTH_OF_A_MACRO of blah.groupsToTrace. Both of these indices have the value 3 which is out of bounds for both arrays, as an array of size 3 has indices 0, 1, and 2.
You either need a loop to copy the elements:
int i;
for (i=0; i<number; i++) {
blah.groupsToTrace[i] = ToTrace[i];
}
Or you could use memcpy:
memcpy(blah.groupsToTrace, ToTrace, sizeof(blah.groupsToTrace));

passing a pointer to a structure as a parameter

I have a structure that I've set up, and a pointer to the structure, that is basically an array. I want to create a function that modifies specific values within the structure but can't seem to figure out how to pass in the structure pointer as a parameter.
struct deviations {
int switch;
int x;
int y;
};
struct deviations *pdevs = malloc(24 * sizeof(int));
for(int i = 0; i < 8; i++) {
(pdevs + i)->switchez = 1;
(pdevs + i)->x = 0;
(pdevs + i)->y = 0;
}
int top[3] = {0, 1, 2};
int bottom[3] = {5, 6, 7};
int left[3] = {0, 3 ,5};
int right[3] = {2, 4, 7};
for(int i = 0; i < 3; i++) {
(pdevs + top[i])->y = -1;
}
I have multiple (8) for loops like above and in each of them the basic structure is the same, except the array ("top"), lvalue ('y') and rvalue ('-1') change in each. I can't figure out how to declare a function with the structure/pointer to structure properly.
I currently have it around 26 lines of (8 for loops repeating) code and am pretty sure I can compress it down to a tidy little function if I can figure out how to pass in the pointer to the structure. Any help would be much appreciated!
This snippet is part of a larger function/program that determines whether or not to check the surrounding items (3 up top, 3 at bottom, one on each side). I have set up a structure with an on/off switch based on the position of the base item, and an x/y offset. I am trying to shift each individual cell by a certain amount +1/-1/ or 0 in the x or y position. And I am trying to flip the switch on or off depending on certain conditions about the x or y of the original cell. Malloc is probably unnecessary, but am unsure whether or not this array of structs will be used again later, if not, I will remove the call to malloc.
Thanks!
So you want a generic function that can take an array of 3 indexes, the structure member to fill in, and the value.
The array and value are simple. To handle a structure member generically, you can use the offsetof() macro.
void fill_structs(struct deviations *pdevs, size_t offset, int indexes[3], int value) {
for (int i = 0; i < 3; i++) {
*((int *)((char *)&pdevs[indexes[i]] + offset)) = value;
}
}
Then you call it like:
fill_structs(pdevs, offsetof(struct deviations, y), top, -1);
offsetof() returns the offset in bytes of a structure member from the base of the structure. So in the function you have to convert the address of the array element to char * so you can add the offset to it, then convert it to int * so you can dereference it and assign to the int member there.
BTW, you should get out of the habit of using things like
(pdevs + i) -> x
If you're using a pointer as the base of an array, use array syntax:
pdevs[i].x
It's much easier to tell that i is an array index this way.
This answer serves purely to challenge your thinking about your current approach.
Personally, unless there's some special logic involved, I would just ditch the loops completely and initialize your struct like this.
const struct deviations devs[8] = {
{ 1, -1, -1 }, // top-left
{ 1, 0, -1 }, // top
{ 1, 1, -1 }, // top-right
{ 1, -1, 0 }, // left
{ 1, 1, 0 }, // right
{ 1, -1, 1 }, // bottom-left
{ 1, 0, 1 }, // bottom
{ 1, 1, 1 } // bottom-right
};
If you then decide that you really need to allocate that dynamically then you could just copy it:
struct deviations *pdevs = malloc(sizeof(devs));
if (pdevs) memcpy(pdevs, devs, sizeof(devs));
But if you really wanted to generate this stuff in a loop, why not something like this?
int ii = 0;
for(int y = -1; y <= 1; ++y) {
for(int x = -1; x <= 1; ++x) {
if (x == 0 && y == 0) continue;
pdevs[ii].switch = 1;
pdevs[ii].x = x;
pdevs[ii].y = y;
++ii;
}
}

C Pointer to an Array example stops working

I'm working on an example and my code is stopped woking when I run it.(Note that I translated the question to English so sorry for grammar mistakes.)
Here is the question:
myShrink() function's prototype is like this:
void myShrink(int *param1, const int param2);
myShrink() function should find the average of the array param1 then should increment the element by 1 if its less than average, or should decrement the element by 1 if it's more than average. param2 is a number of elements in an array.
Here is the content of the main() function:
void myStretch(int *param1, const int param2);
int main () { int myArray[] = {2, 4, 2, 4, 2, 4};
int index;
myStretch(myArray, 6);
printf("UPDATED ARRAY: ");
for(index = 0; index < 6; index++){
printf("%d\t", myArray[index]);
}
return 0;
}
---------The result when we run the program:-----
UPDATED ARRAY: 1 5 1 5 1 5
Now this was the question part what we need to is basically programming the content of myShrink() function and here is my work:
void myShrink(int *param1, const int param2)
{
int array[param2];
param1=array;//Using pointer to point elements of array
int average=0;
int i,j;
for(i=0;i<param2;i++)
{
average+=*(param1 + i);//Adding each element of array
}
average=average/param2;//Then dividing result to param2 to find average
for(j=0;j<param2;j++)
{
if(*(param1+j)<average)//if below average
{
*(param1+j)+=1;//increment
}
else if(*(param1+j)>average)//if above average
{
*(param1+j)-=1;//decrement
}
}
}
But its stops working when I run it. Where I did make mistake and how can I fix it?
Just do this:
#include<stdio.h>
void myStretch(int *param1, const int param2);
int main () { int myArray[] = {2, 4, 2, 4, 2, 4};
int index;
myStretch(myArray, 6);
printf("UPDATED ARRAY: ");
for(index = 0; index < 6; index++){
printf("\n%d\t", myArray[index]);
}
return 0;
}
void myStretch(int *param1, const int param2)
{
int average=0;
int i,j;
for(i=0;i<param2;i++)
{
average+=*(param1 + i);//Adding each element of array
}
average=average/param2;//Then dividing result to param2 to find average
for(j=0;j<param2;j++)
{
if(*(param1+j)<average)//if below average
{
(*(param1+j))+=1;//increment
}
else if(*(param1+j)>average)//if above average
{
printf("%d ",(*(param1+j)));
(*(param1+j))-=1;//decrement
}
}
}
Now first thing: your result is wrong. It should be 3 3 3 3 3 3. As the average in this case is 3 and therefore 2 will become 3 and 4 will also become 3. Secondly you don't have to assign a new array inside the function. Just do it this way. Third mistake:
*(param1+j)>average
should be replaced by
(*(param1+j))>average
and similarly for less than sign.
UPDATE: Also note that many people argue that array[param2] is legal or not. When you declare such an array they are called Variable Length Array(VLA). This is allowed in modern C compilers but older compilers like Turbo C don't allow.
In below myShrink function, after 'param1=array;' statement param1 start pointing to array which has garbage inside.
void myShrink(int *param1, const int param2){
int array[param2];
param1=array;//Using pointer to point elements of array
int average=0;
int i,j;
//debug code to shows param1 points to array which has garage inside
int index =0 ;
for(index = 0; index < 6; index++){
printf("myDebug: %d\n", param1[index]);
}
for(i=0;i<param2;i++)
{
average+=*(param1 + i);//Adding each element of array
}
average=average/param2;//Then dividing result to param2 to find average
for(j=0;j<param2;j++)
{
if(*(param1+j)<average)//if below average
{
*(param1+j)+=1;//increment
}
else if(*(param1+j)>average)//if above average
{
*(param1+j)-=1;//decrement
}
}}

How to find which value is closest to a number in C?

I have the following code in C:
#define CONST 1200
int a = 900;
int b = 1050;
int c = 1400;
if (A_CLOSEST_TO_CONST) {
// do something
}
What is a convenient way to check whether if a is the closest value to CONST among a,b and c ?
Edit:
It doesn't matter if I have 3 variables or an array like this (it could be more than 3 elements):
int values[3] = {900, 1050, 1400};
This works for three variables:
if (abs(a - CONST) <= abs(b - CONST) && abs(a - CONST) <= abs(c - CONST)) {
// a is the closest
}
This works with an array of one or more elements, where n is the number of elements:
int is_first_closest(int values[], int n) {
int dist = abs(values[0] - CONST);
for (int i = 1; i < n; ++i) {
if (abs(values[i] - CONST) < dist) {
return 0;
}
}
return 1;
}
See it working online: ideone
Compare the absolute value of (a-CONST), (b-CONST) and (c-CONST). Whichever absolute value is lowest, that one is closest.
Here is a generalized method. The min_element() function takes an int array, array size, and pointer to a comparison function. The comparison predicate returns true if the first values is less than the second value. A function that just returned a < b would find the smallest element in the array. The pinouchon() comparison predicate performs your closeness comparison.
#include <stdio.h>
#include <stdlib.h>
#define CONST 1200
int pinouchon(int a, int b)
{
return abs(a - CONST) < abs(b - CONST);
}
int min_element(const int *arr, int size, int(*pred)(int, int))
{
int i, found = arr[0];
for (i = 1; i < size; ++i)
{
if (pred(arr[i], found)) found = arr[i];
}
return found;
}
int main()
{
int values[3] = {900, 1050, 1400};
printf("%d\n", min_element(values, 3, pinouchon));
return 0;
}
I m adding something in Mark Byres code.....
int is_first_closest(int values[]) {
int dist = abs(values[0] - CONST),closest; //calculaing first difference
int size = sizeof( values ) //calculating the size of array
for (int i = 1; i < size; ++i) {
if (abs(values[i] - CONST) < dist) { //checking for closest value
dist=abs(values[i] - CONST); //saving closest value in dist
closest=i; //saving the position of the closest value
}
}
return values[i];
}
This function will take an array of integers and return the number which is closest to the CONST.
You need to compare your constant to every element. (works well for 3 elements but it's a very bad solution for bigger elementcount, in which case i suggest using some sort of divide and conquer method). After you compare it, take their differences, the lowest difference is the one that the const is closest to)
This answer is a reaction to your edit of the original question and your comment.
(Notice that to determine the end of array we could use different approaches, the one i shall use in this particular scenario is the simplest one.)
// I think you need to include math.h for abs() or just implement it yourself.
// The code doesn't deal with duplicates.
// Haven't tried it so there might be a bug lurking somewhere in it.
const int ArraySize = <your array size>;
const int YourConstant = <your constant>;
int values[ArraySize] = { ... <your numbers> ... };
int tempMinimum = abs(YourArray[0] - YourConstant); // The simplest way
for (int i = 1; i < ArraySize; i++) { // Begin with iteration i = 1 since you have your 0th difference computed already.
if (abs(YourArray[i] - YourConstant) < tempMinumum) {
tempMinumum = abs(YourArray[i] - YourConstant);
}
}
// Crude linear approach, not the most efficient.
For a large sorted set, you should be able to use a binary search to find the two numbers which (modulo edge cases) border the number, one of those has to be the closest.
So you would be able to achieve O(Log n) performance instead of O(n).
pseudocode:
closest_value := NULL
closest_distance := MAX_NUMBER
for(value_in_list)
distance := abs(value_in_list - CONST)
if (distance < closest_distance)
closest_value := value_in_list
closest_distance := distance
print closest_value, closest_distance

Transposing a matrix

I want to transpose a matrix, its a very easy task but its not working with me :
UPDATE
I am transposing the first matrix and
storing it in a second one
The two
arrays point to the same structure
I
need two arrays (target and source)
so I can display them later for
comparison.
struct testing{
int colmat1;
int rowmat1;
float mat[64][64];
};
int testtranspose(testing *test,testing *test2){
int i,j;
test2->colmat1 = test->rowmat1;
test2->rowmat1 = test->colmat1
for(i=0;i<test->rowmat1;i++){
for(j=0;j<test->colmat1;j++){
test2->mat[i][j] = test->mat[i][j];
}
printf("\n");
}
}
I thought this is the correct method of doing it, but apparently for a matrix such as :
1 2
3 4
5 6
7 8
I get :
1 2 0 0
3 4 0 0
What is the problem ?
Please help,
Thanks !
To transpose the matrix, you need to change rows and columns. So you need to use:
targetMatrix[i][j] = sourceMatrix[j][i];
Note how the order of i,j is changed, since one matrix's rows are another's columns.
By the way, instead of (*a).b, you can write a->b. This is the normal way of accessing a field of a struct pointer.
Try this...
struct testing{
int colmat;
int rowmat;
float mat[64][64];
};
int testtranspose(testing *test,testing *test2){
int i,j;
test2->colmat = test->rowmat;
test2->rowmat = test->colmat;
for(i=0;i<test->rowmat;i++){
for(j=0;j<test->colmat;j++){
test2->mat[j][i] = test->mat[i][j];
}
}
return 0;
}
int printmat(testing* mat)
{
for(int i=0;i<mat->rowmat;i++)
{
printf("\n");
for(int j=0;j<mat->colmat;j++)
printf((" %f"),mat->mat[i][j]);
}
return 0;
}
// 2
// main.cpp
int _tmain(int argc, _TCHAR* argv[])
{
testing mat1, mat2;
memset(&mat1,0,sizeof(testing));
memset(&mat2,0,sizeof(testing));
mat1.colmat =2;
mat1.rowmat =3;
for(int i=0;i<mat1.rowmat;i++)
{
for(int j=0;j<mat1.colmat;j++)
mat1.mat[i][j] = (float)rand();
}
printmat(&mat1);
testtranspose(&mat1,&mat2);
printmat(&mat2);
getchar();
}
I am new to C / C++ (3rd day or so :) ) and I had the same problem. My approach was slightly different in that I thought it would be nice to have a function that would return a transposed matrix. Unfortunately, as I found out, you cannot return a an array nor pass an array to a function in C++ (let alone a double array), but you can pass / return a pointer which works similar to an array. So this is what I did:
int * matrix_transpose(int * A, int A_rows, int A_cols){
int * B;
int B_rows, B_cols;
B_rows = A_cols; B_cols= A_rows;
B = new int [B_rows*B_cols];
for(int i=0;i<B_rows;i++){
for(int j=0;j<B_cols;j++){
B[i*B_cols+j]=A[j*A_cols+i];
}
}
return B;
};
The trick was in dynamic arrays. I used A_rows and B_rows as separate names (you can use only rows and cols) in order to make the problem less intricate and less confusing when reading code.
B = new int [rows*cols] // This is cool in C++.

Resources