Reputation: 563
I have a Memory chip that has data read by an embedded system serially. The memory device has 10 locations with each location having the following information: customerID, NumOfParts, MFGName. Here is my version of C code I wrote for this.
IN C FILE "PartName.h"
//"PartName.h"
#ifndef PartName_H
#define PartName_H
typedef UINT8 MFGName_Struct_t [10];
typedef struct
{
UINT8 customerID;
UINT8 NumOfParts;
MFGName_Struct_t MFGName;
}IDENT_Struct_t;
typedef struct MFG_Struct_t
{
UINT8 <some other variable>;
UINT8 <some other variable>;
IDENT_Struct_t Ident1;
IDENT_Struct_t Ident2;
IDENT_Struct_t Ident3;
IDENT_Struct_t Ident4;
IDENT_Struct_t Ident5;
IDENT_Struct_t Ident6;
IDENT_Struct_t Ident7;
IDENT_Struct_t Ident8;
IDENT_Struct_t Ident9;
IDENT_Struct_t Ident10;
} MFG_Struct_t;
#endif
IN C FILE "HighNum.c"
//"HighNum.c"
#include "PartName.h"
static MFG_Struct_t MFG;
UINT8 HighNum(void)
{
//UINT8 i;
UINT8 highestNum = 0;
//for (i = 0;(i < 10);i++)
if (MFG.Ident1.NumOfParts > MFG.Ident2.NumOfParts)
{
highestNum = MFG.Ident1.NumOfParts;
}
if (MFG.Ident2.NumOfParts > MFG.Ident3.NumOfParts)
{
highestNum = MFG.Ident2.NumOfParts;
}
if (MFG.Ident3.NumOfParts > MFG.Ident4.NumOfParts)
{
highestNum = MFG.Ident3.NumOfParts;
}
if (MFG.Ident4.NumOfParts > MFG.Ident5.NumOfParts)
{
highestNum = MFG.Ident4.NumOfParts;
}
<and so on>
return highestNum;
}
How can this be done more efficient in C? using for statements or While statements? I thought about concatenating the number portion and cycling through the variables...
Upvotes: 0
Views: 67
Reputation: 1821
You could redefine the structure to put the 10 "Ident" values in an array, then use a for-loop to test your values:
typedef struct MFG_Struct_t
{
UINT8 <some other variable>;
UINT8 <some other variable>;
IDENT_Struct_t Ident[10];
} MFG_Struct_t;
:
:
UINT8 HighNum(void) {
UINT8 i, highestNum = 0;
for (i = 0; i < 9; i++) {
if (MFG.Ident[i].NumOfParts > MFG.Ident[i+1].NumOfParts) {
highestNum = MFG.Ident[i].NumOfParts;
}
}
return highestNum;
}
or, better yet... change the HighNum function to be more efficient, as follows:
UINT8 HighNum(void) {
UINT8 i, highestNum = MFG.Ident[0].NumOfParts;
for (i = 1; i < 10; i++) {
if (MFG.Ident[i].NumOfParts > highestNum) {
highestNum = MFG.Ident[i].NumOfParts;
}
}
return highestNum;
}
Upvotes: 1
Reputation: 223
I hope the following code can help you:
typedef struct MFG_Struct_t
{
UINT8 <some other variable>;
UINT8 <some other variable>;
IDENT_Struct_t Idents[10];
} MFG_Struct_t;
typedef enum _Idents
{
Ident1 = 0,
Ident2,
Ident3,
Ident4,
Ident5,
Ident6,
Ident7,
Ident8,
Ident9,
Ident10,
} IdentType;
if (MFG.Idents[Ident1].NumOfParts > MFG.Idents[Ident2].NumOfParts)
{
highestNum = MFG.Idents[Ident1].NumOfParts;
}
// or
for (index = Ident1; index < Ident10; index ++)
{
if (MFG.Idents[index].NumOfParts > MFG.Idents[index +1].NumOfParts)
{
//......
}
}
Upvotes: 1
Reputation: 7441
If you modify your structure to this:
#define IDENT_SIZE 10
typedef struct MFG_Struct_t
{
UINT8 <some other variable>;
UINT8 <some other variable>;
IDENT_Struct_t Ident[IDENT_SIZE];
} MFG_Struct_t;
Then you can do this:
size_t i = 0;
for (i = 0; i < IDENT_SIZE; i++) {
if (MFG.Ident[i].NumOfParts > highestNum) {
highestNum = MFG.Ident[i].NumOfParts;
}
}
Upvotes: 1