I have an array that is made up doubles which I need to round down and convert to integers so I can use them as indices in an output array. I have just started C programming and am not sure how this works. So far the best I have been able to come up with is:
int create_hist( double input_array[], int count, int output_array[17] ) {
for ( int i = 0; i < count; i++ ) {
input_array[i] = int floor(input_array[i]);
output_array[input_array[i]]++;
However I am getting the following errors which I am having trouble deciphering:
array.c:11:20: error: expected expression before ‘int’
input_array[i] = int floor(input_array[i]);
^
array.c:12:7: error: array subscript is not an integer
hist[input_array[i]]++;
^
array.c:14:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
If someone could let me know where I have gone wrong it would be greatly appreciated.
Unless you actually want to modify input_array, you would be best off saving the rounded off double in an intermediate variable to then access your integer array. And no need to use floor() casting the double to int will do that.
int create_hist(double input_array[], int count, int output_array[17]) {
for (int i = 0; i < count; i++) {
int index = (int)input_array[i];
if ((index > 16) || (index < 0)) {
return -1;
}
output_array[index]++;
}
return 0;
}
Of course, you should really pass in the size of output_array as a variable as well, instead of hard-coding it.
So let get cracking:
First error is due to the fact that you are kind of declaring a function.
input_array[i] = int floor(input_array[i]);
notice int in front of floor, that is not necessary. It should be
input_array[i] = floor(input_array[i]);
Second error is due to the fact that you are accessing array element using double in
output_array[input_array[i]]++;
either you should do it some other way or do following:
output_array[(int) input_array[i]]++;
and the third error is unbalanced parenthesizes.
I am relatively new to programming and I have come across an issue whilst trying to write a function that utilizes loops. The function should work as such:
The array Data consists of a 51X2 set of doubles, which are all used in the program through the initial for loop, the a variable and the b variable. The X and Y variables are both set to the minimum values of the array.
Using the a and X variables as an example, the X value is compared to a and incremented until it surpasses a. The number of loop iterations are tracked by the c variable. This c variable is then used in the Graph array. The same procedure occurs for the b and Y variables. This process is repeated for every value of S to analyse all data points from the Data array.
The issue I'm having is that the c and d variables don't change relative to the changes inside the loops. The variables will not change from their initialized value. I am looking to find a solution that allows for the c and d variables to change in relation to the number of iterations of the for loop.
The relevant function code can be seen below:
void Data_Plot(double Data[51][2], char Graph[44][56])
{
int N = 50;
int S,q,r;
int c = 0;
int d = 0;
double a = Data[S][0];
double b = Data[S][1];
double X = Data[0][0];
double Y = Data[0][1];
for (S=0;S<N;S++)
{
for(X;X<a;X+=0.1428571429)
{
c++;
}
for(Y;Y<b;Y+=2)
{
d++;
}
Graph[c][d] = '*';
}
I am aware that my code is very unoptimized and messy, but I can fix those issues up with future projects after I have finished this one.
Edit: I would like to note that I have attempted this with c and d being set to other values, as well as being left as NULL. The same result occurred regardless of the variable initialization.
Since a and b depend on S, which you change during the for loop, you need to move those variables inside the loop.
void Data_Plot(double Data[51][2], char Graph[44][56])
{
int N = 50;
int S,q,r;
int c = 0;
int d = 0;
double X = Data[0][0];
double Y = Data[0][1];
for (S=0;S<N;S++)
{
double a = Data[S][0];
for(X;X<a;X+=0.1428571429)
{
c++;
}
double b = Data[S][1];
for(Y;Y<b;Y+=2)
{
d++;
}
Graph[c][d] = '*';
}
I am trying to sort array of structures and then display its sorted values. When I use some variable in the loop, it displays a garbage value of first structure while rest of all values are as they are But when I use variable value, it displays all values correct.
My code is:
void classificationClass::sortMyStruct(location *temp)
{
const int loopLimit = _tempVec.size()*_tempVec[0].size();
double *_temp = (double*)malloc(sizeof(double));
int *_tempX = (int*)malloc(sizeof(int)), *_tempY = (int*)malloc(sizeof(int));
for (int a = 0; a < loopLimit; a++)
{
for (int b = 0; b< loopLimit; b++)
{
if (temp[b].value > temp[b+1].value)
{
*_temp = temp[b].value;
*_tempX = temp[b].xLoc;
*_tempY = temp[b].yLoc;
temp[b].value = temp[b+1].value;
temp[b].xLoc = temp[b+1].xLoc;
temp[b].yLoc = temp[b+1].yLoc;
temp[b+1].value = *_temp;
temp[b+1].xLoc = *_tempX;
temp[b+1].yLoc = *_tempY;
}
}
}
free(_temp);
free(_tempX);
free(_tempY);
}
Now if I use "17" instead of "loopLimit", I get correct values.
The output using a variable is like this
The output using hard coded value is like this
If anybody can please guide me in a right direction to this problem's solution, I'll be very thankful. I'm stuck on it for like 2 or 3 days now.
Thanks
When you are on the last iteration of the loop this code will be invalid: temp[b+1] since b+1 is out of the bounds of your array.
I've got a c study which it must print all the numbers in an array then how many times they repeated.
int lottery(int a,int b,int c,int d,int e,int f,int i,int count)
{
printf("Enter the loop count:");
scanf("%d",&d);
a=time(NULL);
srand(a);
int genel[100][100];
int hepsi[50]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49};
count=0;
for(e=0;e<=d-1;e++)
{
for(b=0;b<=5;b++)
{
genel[e][b]=(rand()%49+1);
while(i>=0 && i<=49)
{
if(genel[e][b]==hepsi[i])
{
count=count+1;
}
else{
count=count;
}
}
printf("%d->%d\t",genel[e][b],count);
}
}
}
This doesnt work obviously. the output must be something like that
1-->0 2-->3 3-->15 etc
TY for your help, cheers :)
It is important that you understand what you are doing, naming is therefore very important. Nesting loops is okay if you know what you are doing. An easier to understand approach would be:
void lottery() {
int i, j //forloop counters
int randArray[100][100]; //array for random values
srand(Time(NULL)); //set random seed based on system time
//set random values
for(i = 0; i < 100; i++) {
for(j = 0; j < 100; j++) {
randArray[i][j] = rand()%49 + 1; //sets random ranging from 1 to 49 (49 incl)
}
}
//here you can start the counting procedure, which I won't spoil but ill give some hints below
}
There are a few options, first the easy lazy approach:
use a loop over all the values, 'int number' from 1 up to 49, inside that forloop use two forloops to search through the whole array, incrementing int x everytime you encounter the value 'number'. After youve searched through the whole array, you can use printf("%d -> %d", number, x); to print the value, set x to zero and count another number.
Another approach is as u tried,
create an array with for each number a location where you can increment a counter. Loop through the whole array now using two for-loops, increment the arraylocation corresponding to the value which youve found at randArray[i][j]. Afterwards print the array with counts using another forloop.
I suggest you try to clean up your code and approach, try again and come back with problems you encounter. Good luck!
sorry if this wasn't helpful to you, I tried to spoil not too much because according to my own experience programming should be learned by making mistakes.
We are programming a ST269 microcontroller which has two IR distance sensors. To calibrate these sensors we made one table for each sensor with the distance we measured and the corresponding value we get from the ADC.
Now we want to use one function to approximate the values in between. So we defined two two-dimensional arrays (one for each sensor) as global variables. In our function we then want to copy the one array we want to work with to a working array and approximate our values.
So here's the code:
...
unsigned int ir_werte_re[][] = {
{8,553},
...
{83,133}
};
unsigned int ir_werte_li[][] = {
{8,566},
...
{83,147}
};
...
unsigned int geradenaproximation(unsigned int messwert, unsigned int seite)
{
unsigned int working_array[16][16];
unsigned int i = 0;
if (seite == 0) {
for (i = 0; i < sizeof(working_array); i++) {
working_array[i][0] = ir_werte_li[i][0];
i++;
}
}
else {
for (i = 0; i < sizeof(working_array); i++) {
working_array[i][0] = ir_werte_re[i][0];
i++;
}
}
i = 0;
unsigned int y1 = 0;
unsigned int x1 = 0;
...
}
This code is in a file called sensor.c. We didn't write anything about our global arrays in the sensor.h should we? The sensor.h of course is included in our main.c and there the function is called.
We also tried to copy the arrays via
memcpy(working_array, ir_werte_li, sizeof(working_array));
And in every way we do this we get a
syntax error near unsigned
in the line where we're declaring
unsigned int y1 = 0;
and I'm pretty sure that there is no syntax error in this line : )
The last time I spend coding in C is a few years away so I'm not sure if the way we try to do this is good. Perhaps we can solve this by using a pointer instead of really copying the array or something. So please help me out I'll appreciate your bits on this.
In C (pre-C99), all variable definitions must appear at the top of the current block scope.