Arrays in c programming

Status
Not open for further replies.

john120

Banned
Joined
Aug 13, 2011
Messages
257
Helped
11
Reputation
22
Reaction score
10
Trophy points
1,298
Visit site
Activity points
0
How can I know the index of a any number in an array:

example
#define ARRAY_SIZE 9

const int k[]={1,2,3,4,5,6,7,8,9}

can you plz help me to know (display)the index of 8 (it has to be 8)

Thanks!!
 

the index of 8 is 7 here . because K[0]=1 , k[1] =2 ..... k[8]=9.

you might need to check the libraries of C that search for an item inside an array and tells you at what index it is . but problem if there is more than one !!
 

the index of 8 is 7 here . because K[0]=1 , k[1] =2 ..... k[8]=9.

you might need to check the libraries of C that search for an item inside an array and tells you at what index it is . but problem if there is more than one !!

This can be to make using a very simple loop on the entire array and return the first index that contain the search value
(and return -1 if the value is not in this array)

Code:
#define ARRAY_SIZE 9
const int k[]={1,2,3,4,5,6,7,8,9}

int FindIndex(int *vals, int size, int search)
{
    int i;

    for ( i = 0; i < size ; i++)
        if ( vals[i] == search )
            return i;

    return -1;
}

In your specific example, a call to FindIndex(k, ARRAY_SIZE, 8) should logically return 7
(if the array have more than one occurence of the search value, this fonction always return the lowest index that contain this value and if the value is not in the array, the fonction return -1)

But this is a "true" C example, so I don't think that this can coded as this on a FPGA language ...
=> I implement/test the same thing but using the verilog syntax tomorow (with the print of the result of course)
 
Last edited:


can you plz help me on the codes for this index searching:I am thinking that it is better that I find the index which correspond to a number which has been read by the processor then
that index is multiplied by 0.1 and then added to the starting number which is 8

for example
const double hi_limit[]={1234900,1234100,1234000,1231500,1230090,1228900,122600};
const double lo_limit[]={1235900,1234800,1231900,1231800,1230300,1230100,122800};

the index of those bold number will be 2 then jean=jean+0.1*2 ;as jean has been initialized equals 7.0 the output will be 7.2%


meanwhile what is needed is to find that index 2 (how may I search it inside array);in those two arrays no number appearing twice.

And also the comparison has to be made inside the FindIndex function because I need to see whether the value read is in a given boundary and the calculate jean and then print its final value.
Thanks.
 
Last edited:

Something like this ???
Code:
yannoo@Ubuntoo:~/dev/FPGA$ ./between 1232950
Search the first index when 1232950 is between the array's limits 
1232950 is not between 1235900 and 1234900, so the index cannot to be 0 
1232950 is not between 1234800 and 1234100, so the index cannot to be 1 
1232950 is between 1231900 and 1234000, so the index is 2 
jean = 7 + 0.1 * 2 
The final value is 7.2 

yannoo@Ubuntoo:~/dev/FPGA$ ./between 122700
Search the first index when 122700 is between the array's limits 
122700 is not between 1235900 and 1234900, so the index cannot to be 0 
122700 is not between 1234800 and 1234100, so the index cannot to be 1 
122700 is not between 1231900 and 1234000, so the index cannot to be 2 
122700 is not between 1231800 and 1231500, so the index cannot to be 3 
122700 is not between 1230300 and 1230090, so the index cannot to be 4 
122700 is not between 1230100 and 1228900, so the index cannot to be 5 
122700 is between 122800 and 122600, so the index is 6 
jean = 7 + 0.1 * 6 
The final value is 7.6 

yannoo@Ubuntoo:~/dev/FPGA$ ./between 0
Search the first index when 0 is between the array's limits 
0 is not between 1235900 and 1234900, so the index cannot to be 0 
0 is not between 1234800 and 1234100, so the index cannot to be 1 
0 is not between 1231900 and 1234000, so the index cannot to be 2 
0 is not between 1231800 and 1231500, so the index cannot to be 3 
0 is not between 1230300 and 1230090, so the index cannot to be 4 
0 is not between 1230100 and 1228900, so the index cannot to be 5 
0 is not between 122800 and 122600, so the index cannot to be 6 
Any interval between the array's limits can contain the value 0 :( 
The final value is 7.0

This was generated by this :
Code:
#include <stdio.h>
#include <stdlib.h>

const double hi_limit[]={1234900,1234100,1234000,1231500,1230090,1228900,122600};
const double lo_limit[]={1235900,1234800,1231900,1231800,1230300,1230100,122800};

// the number of values into hi_limit[] and low_limit[] array 
# define NUM_LIMITS 7

// the jean's starting number (???) and the index multiplier
#define STARTING_NUMBER 7.0f
#define MULTIPLIER 0.1f

double jean = STARTING_NUMBER;

int FindIndex(const double *low, const double *hi, int size, double search)
{
    int i;

    printf("Search the first index when %.0f is between the array's limits \n", search);

    for ( i = 0; i < size ; i++)
    {
        if ( ((low[i] <= search) && (hi[i] >= search)) || ((low[i] >= search) && (hi[i] <= search)) ) 
	{
	    printf("%.0f is between %.0f and %.0f, so the index is %d \n", search, lo_limit[i], hi_limit[i], i); 
            return i;
	}
	else
	{
	   printf("%.0f is not between %.0f and %.0f, so the index cannot to be %d \n", search, lo_limit[i], hi_limit[i], i);
	} 
	    
    }

    printf("Any interval between the array's limits can contain the value %.0f :( \n", search);
    return -1;
}

int main(int argc, char **argv)
{
	double tosearch = (1234000 + 1231900) / 2;  
	int firstidx;

	if ( argc > 1 )
	{
		tosearch = atof(argv[1]);
	}

	firstidx = FindIndex(lo_limit, hi_limit, NUM_LIMITS, tosearch);

	if( firstidx != -1 )
	{
		printf("jean = %.0f + %.1f * %d \n", STARTING_NUMBER, MULTIPLIER, firstidx); 
		jean = STARTING_NUMBER +  (MULTIPLIER * (float)(firstidx));
	}

	printf("The final value is %.1f \n", jean);
}
(I think that it's not reasonable to begin to write the FPGA's verilog version of this before to have something that work good on a true C program ...)
 


I declared the function FindIndex and coded,but when I call it it says:Attempt to create a pointer to a constant
 

I declared the function FindIndex and coded,but when I call it it says:Attempt to create a pointer to a constant

Have you test without the const keyword ?

I'm very new on the FPGA programming domain and have only installed iverilog and vvp on my linux box for the instant.

I can too now recept the "hello, world" message from the FPGA board to my linux box with the RS232 to USB cable and Kermit that run on my computer
(only to configure Kermit, the RS232 to USB connection is automatically recognized as /dev/ttyUSB0 on my linux box)

I test since somes hours to install others tools like Quartus on my Ubuntoo 13.04 but the download speed is really too slow, so I have stop it

=> what are the best tools for to handle the compilation and transfer of .vl files from my linux box to the Xilinx FPGA Development Spartan-3E XC3S500E-PQG208 Board 4.3" TFT LCD 4 Nios ?
(I have only test some .vl files using iveriflog and vvp for the instant => now that this work I want to begin to see other thing than a static colored checkerboard on the on-board screen of the FPGA screen :smile: )
 

The given codes does not give me the index of a number in the array,help to solve the issue plz.
 

The given codes does not give me the index of a number in the array,help to solve the issue plz.

Can you please post your array please ?
(I don't understand why this can to work with somes datas and not with others ...)
[note that this code return **the first** index when the search value is contained between the high and low limits arrays ...]
 
Last edited:

Can you please post your array please ?
(I don't understand why this can to work with somes datas and not with others ...)
[note that this code return **the first** index when the search value is contained between the high and low limits arrays ...]
Hello See all my codes below:

#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
//#define ARRAY_SIZE 7
#define ARRAY_SIZE 9


int l,i,largest,size,search;
float value,value1;
int j[]={1,2,3,4,5,6,7,8,9};
int FindIndex(int *vals, int size, int search)
{
int i;

for ( i = 0; i < size ; i++)
if ( vals == search )
return i;

return -1;
}

void main(void)
{
set_tris_c(0xff);
set_tris_b(0x00);
lcd_init();
delay_us(200);

while(1)
{
set_timer1(0);
setup_timer_1(t1_external | T1_DIV_BY_1);
delay_ms(1000);
setup_timer_1(T1_DISABLED);
value=get_timer1();
value1=value;
delay_ms(20);
FindIndex(j,ARRAY_SIZE,value1);
printf(lcd_putc,"\f%d",search);
delay_ms(1000);

}
}
The Pic reads the value on T1CKI and then the system search that number in the array above and then it prints its index.
 

the following is the code for my array but it is not printing the exact values on the lcd:
#define ARRAY_SIZE 9
int k[]={1145,1245,1345,1454,1578,1665,1745,1548,1459};
int j[]={2780,2641,2462,2013,2234,2655,2786,2577,2128};

int FindIndex(int *vals, int size, int search)
{
int i;
float l;
for ( i =0; i <size ; i++)
while((value1<= j)&&(k<=value1))
{

l=i;
lcd_gotoxy(11,1);
printf(lcd_putc,"\f%2.1f",l);
printf(lcd_putc,"\%%");
delay_ms(1500);
}
}

for reading the number of value1;the following are the codes:
set_timer1(0);
setup_timer_1(t1_external | T1_DIV_BY_8);
delay_ms(250);
setup_timer_1(T1_DISABLED);
value=get_timer1();


help me to see way the array is not printing the real value index.
 

Why this ?
Code:
for ( i =0; i <size ; i++)
while((value1<= j[i])&&(k[i]<=value1))

You don't want to make this ?
Code:
for ( i =0; i <size ; i++)
if ( ((value1<= j[i]) &&( k[i] <=value1)) || ((value1<= k[i]) &&( j[i] <=value1))  )
(cf. use if instead while + test if value1 is **between** k and j with no assumption than k is always <= j )


How you compile/transfert/test the code on your FPGA, it's a SPARTAN plateform ?
(I don't know already how to compile/transfert/test something from my Linux box to a FPGA plateform, all I have is for to devel on the Windows platerform, not a Linux platform :sad
[I have a Xilinx FPGA Development Spartan-3E XC3S500E-PQG208 Board 4.3" TFT LCD 4 Nios, I have find iveriflog and vvp for to test the code on my Linux box but I don't know how to handle the transfert/execute the code into the FPGA side]
 
Last edited:



I did but the problem remains the same,I am doubting on how I called the function but I am not able to write the conveniable syntax for function calling.

Help on it.
 

Ok, so you want everything into the main() for to begin, right ?

if yes, this is something like this :
Code:
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
//#define ARRAY_SIZE 7
#define ARRAY_SIZE 9

int i, found;
int value;

int j[]={1,2,3,4,5,6,7,8,9};

void main(void)
{

   set_tris_c(0xff);
   set_tris_b(0x00);
   lcd_init();
   delay_us(200);

   while(1)
   {
      set_timer1(0);
      setup_timer_1(t1_external | T1_DIV_BY_1);
      delay_ms(1000);
      setup_timer_1(T1_DISABLED);
      value=get_timer1();
      delay_ms(20);

      for ( i = 0, found = -1 ; i < ARRAY_SIZE ; i++)
        if ( j[i] == value )
        {
           printf(lcd_putc,"j[%d] is %d", i, value);
           found = 1;
        }

      if ( found == -1 )
         printf(lcd_put, "cannot found the %d value :(", value);   

      delay_ms(1000);

   } // end while(1)

} // end main()

PS : please, respond me about what platform you use for to transfert the code from your computer to the FGPA board
(I haven't already find how to make this from my Linux box to a SPARTAN FPGA board :sad: )
 
Last edited:



I am not using FPGA ;me I am running simple PIC programmer with ccs c compiler,for the FPGA I use some time the one from Korea I let you know by tommorow when I come in my other labs.Have you test those codes you give me I am trying but not running.I want to print the index of the number found after conditions.

Thanks
 

I cannot test it because this ..

Code:
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"

and this :
Code:
set_timer1(0);
      setup_timer_1(t1_external | T1_DIV_BY_1);
      delay_ms(1000);
      setup_timer_1(T1_DISABLED);
      value=get_timer1();

But I can easily test on my Linux box a C working code that use **only** standards headers

- - - Updated - - -

This code work on my Linux box :
Code:
// your specifics dev plateform
// #include <16F877A.h>
// #DEVICE ADC=10
// #fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
// #use delay(clock=20000000)
// #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#include "lcd.c"

// specific printf / delay headers used on my Linux box
#include <stdio.h>
#include <unistd.h>

#define delay_ms(t) usleep(t*1000L)

//#define ARRAY_SIZE 7
#define ARRAY_SIZE 9

int i, found;
int value, n = 0;

int j[]={1,2,3,4,5,6,7,8,9};

void main(void)
{

   // set_tris_c(0xff);
   // set_tris_b(0x00);
   // lcd_init();

   delay_ms(200);	

   while(1)
   {

      printf("\nStep %d \n", n++);
      // set_timer1(0);
      // setup_timer_1(t1_external | T1_DIV_BY_1);
     delay_ms(1000);

      // setup_timer_1(T1_DISABLED);
      // value=get_timer1();
      value = rand() % 20;
      printf("search the value %d \n", value);

      delay_ms(20);

      for ( i = 0, found = -1 ; i < ARRAY_SIZE ; i++)
        if ( j[i] == value )
        {
           printf("j[%d] is %d \n", i, value);
           found = 1;
        }

      if ( found == -1 )
         printf("cannot found the %d value :( \n", value);   

      delay_ms(100);

   } // end while(1)

} // end main()
(the value normally returned by get_timer1() is here emulated by rand %20)

When this run, this say this :
Code:
Step 0 
search the value 3 
j[2] is 3 

Step 1 
search the value 6 
j[5] is 6 

Step 2 
search the value 17 
cannot found the 17 value :( 

Step 3 
search the value 15 
cannot found the 15 value :( 

Step 4
search the value 13 
cannot found the 13 value :( 

Step 5 
search the value 15 
cannot found the 15 value :( 

Step 6
search the value 6 
j[5] is 6 

...

PS : I have begin to download the Xilink SDK 2013 but my download manager say 7 hours :shock:
=> why 2.2 Go of download when what I really want is only a compilator/linker/transfer chain tools for to can devel FPGA's executables from a linux platform to a FPGA platform ???
(if this is for to have an 2.1 Go IDE this is clearly a lack of time for me, I usually use gedit for editing and my prefered compilator is gcc ... => only one or two howtos and minimals tools seem me very better for to begin on FPGA dev from a linux box that a very big IDE that take very very ... very too long time to download )
[I have already install iverilog and vvp on my linux box and this work fine, so I only need something like a "transfer2FPGA" tool, not a 2.2 Go SDK "de la mort qui tue mais dont 99% ne me serviront strictement à rien" ... ]
 
Last edited:

I have see this at https://iroi.seu.edu.cn/books/asics/book2/ch11/CH11.02.htm :
There are no multidimensional arrays in Verilog, but we may declare a memory data type as an array of registers

You have to use something like this :
Code:
module declarations_5;
reg [31:0] VideoRam [7:0]; // An 8-word by 32-bit wide memory.
initial begin 
VideoRam[1] = 'bxz; // We must specify an index for a memory.
VideoRam[2] = 1; 
VideoRam[7] = VideoRam[VideoRam[2]]; // Need 2 clock cycles for this.
VideoRam[8] = 1; // Careful! the compiler won't complain about this!
// Verify what we entered:
$display("VideoRam[0] is %b",VideoRam[0]);
$display("VideoRam[1] is %b",VideoRam[1]);
$display("VideoRam[2] is %b",VideoRam[2]);
$display("VideoRam[7] is %b",VideoRam[7]);
end 
endmodule 
VideoRam[0] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
VideoRam[1] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz
VideoRam[2] is 00000000000000000000000000000001
VideoRam[7] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz

- - - Updated - - -

I have a verilog source that work well when emulated on my linux box :

Code:
module between;
 
  reg clock;
  integer idx;
  integer search = 8;
  reg [31:0] limits [8:0];
  

initial 
  begin 

    clock = 0; idx = 0;
    limits[0] = 1;
    limits[1] = 2;
    limits[2] = 3;
    limits[3] = 4;
    limits[4] = 5;
    limits[5] = 6;
    limits[6] = 7;
    limits[7] = 8;
    limits[8] = 9;
    #1000 $finish;
  end 

always  #10 clock = ~ clock;

always 
  begin @ (negedge clock);

    if (idx == 9)
      	idx = 0;
    else      
	idx = idx + 1;

    if ( limits[idx] == search) 
    	$display("time = ",$time," limits[", idx, "]=", search);
  end 
endmodule
=> this search the value 8 (cf. the line "integer search = 8") into the array limits[] declared at the line "reg [31:0] limits [8:0];"

This give this at output when emulated on my Linux box
Code:
yannoo@Ubuntoo:~/dev/FPGA$ make
iverilog -o between_fpga between.vl
vvp between_fpga
time =                  140 limits[          7]=          8
time =                  320 limits[          7]=          8
time =                  500 limits[          7]=          8
time =                  680 limits[          7]=          8
time =                  860 limits[          7]=          8
(I don't know how to handle only one occurence, so I have employ "#1000 $finish;" for to be sure that this test stop a day)
[note than the array begin at 0, not at 1]

- - - Updated - - -

I have found how to finish as soon as possible :

Code:
   if ( limits[idx] == search)
    begin
    	$display("time = ",$time," limits[", idx, "]=", search);
        $finish;
    end

=> now, I have only to found how to handle the nesting of multiples modules between them for to have the equivalent of fonctions

- - - Updated - - -

This version exit at the first index that contain the searched value or display a message such as the value isn't found at the end

Code:
module between;
 
  reg clock;
  integer idx;
  integer search = 11;
  reg [31:0] limits [8:0];
  

initial 
  begin 

    clock = 0; idx = 0;
    limits[0] = 1;
    limits[1] = 2;
    limits[2] = 3;
    limits[3] = 4;
    limits[4] = 5;
    limits[5] = 6;
    limits[6] = 7;
    limits[7] = 8;
    limits[8] = 9;
    #1000 $finish;

  end 

always  #10 clock = ~ clock;

always 
  begin @ (negedge clock);

    if ( limits[idx] == search)
    begin
    	$display("time = ",$time," limits[", idx, "]=", search);
        $finish;
    end
    else

    if (idx == 9)
    begin
      	$display("Cannot found the value ", search, " into the array");
        $finish;
    end
    else      
	idx = idx + 1;
  end 
endmodule

With search = 8
Code:
time =                  160 limits[          7]=          8

And if I modify the value of search with 11 :
Code:
integer search = 11;

Code:
Cannot found the value          11 into the array

But in first view, you want a true C source (not a verilog source), so my last post with the linux source seem to more adapted for you
 
Last edited:


with the codes given previously;can you plz help me to add a push button so I read the value of i and value after pushing a push button.

Thanks
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…