Comparison of two integer saved in array

Status
Not open for further replies.

ecaits

Member level 4
Joined
Jan 16, 2014
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
579
Dear All Friends,

I want to compare two integer array and want to know that which is greater. I have tried logic to compare MSB of each to LSB but I am not getting exact result.

For Example.

int data[5] = '12334" and int sample[5] = "11274" are two integer array and I want to compare that which array is greater.

Plz suggest me how can I complete this?
 

if the arrays are the same length and don't contain negative values the following should work?
Code:
    int i, data[5] = {1,2,3,3,4}, sample[5] = {1,1,2,7,4};
    for(i=0;i<5;i++)
    if(data[i]>sample[i]) { printf("data > sample \n"); break; }
    else
       if(data[i]<sample[i]) { printf("sample > data \n"); break; }
    printf("equal\n");
 

"11274" is a string literal not an integer. The initialization statements are filling part of the integer arrays with characters.
- Is this what you want?
- In which sense do you want to compare the strings?
 

Actually, I want to compare the setvalue and process value. If process value is higher than setvalue then I want to make one of the pin high logic. I have stored setvalue in EEPROM and process value is stored in one array. I read the setvalue from EEPROM and stored it again in another array. So I want to compare these two array and want to observe that which is higher.
 

what type is this data ? e.g. an array of integer vales
Code:
sample[5] = {1,1,2,7,4};

or as your original code
Code:
int data1[5] = "12334" ;
where, as FvM states, you have initialised an integer with an array of characters

this definition gives an error with GNU C
Code:
C:\temp\zz.c|5|error: array of inappropriate type initialized from string constant|
 

I would expect process value and setpoints represented by simple signed or unsigned integers, neither arrays nor character strings. In case that the setpoint is given as a decimal string, it would be converted to an integer number, e.g by atoi() before comparing it with the process value.

this definition gives an error with GNU C
Yes, that's helpful to indicate the possible user error. Other compilers are simply filling up the array with characters.
 

if the data are simple integers you can compare them so
Code:
    int i, data = 12345, sample = 11274;
    for(i=0;i<5;i++)
    if(data>sample) { printf("data > sample \n"); break; }
    else
       if(data<sample) { printf("sample > data \n"); break; }
    printf("equal\n");
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…