Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Trying to understand Arrays in C on an Atmega16

Status
Not open for further replies.

ruriz

Newbie level 1
Newbie level 1
Joined
Feb 4, 2015
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
8
Hello, So I have this simple code that I am trying to understand in able to build an UTP lan tester
I would appreciate if someone can help me with this
so here is my code

assume we have an array of three types of cables and an array with a pointer to pass variables
Code:
int findType(int *v){ // the cable types
		int straight[8] = {1, 2, 3, 4, 5, 6, 7, 8};
		int cross[8] = {3, 6, 1, 4, 5, 2, 7, 8};
		int roll[8] = {8, 7, 6, 5, 4, 3, 2, 1};
		int i, ok = 0;
		
		for(i = 0; i < 8; i++){
			if(v[i] != straight[i]){
				ok = 1;
				break;
			}
		}


		if(ok == 0)
			return 0;
		
		ok = 0;
	for(i = 0; i < 8; i++){
			if(v[i] != cross[i]){
				ok = 1;
				break;
			}
		}

		if(ok == 0)
			return 1; // type if cable 
		
		ok = 0;
	int type;
	int v[8] = {0, 0, 0, 0, 0, 0, 0, 0};
//etc etc

my question is what does 'ok' do in this code , and why do I have to set it back to zero and use an if loop to return the type?
 

I'm sure that it is good idea to use this code. Looks like someone very newbie wrote it.
OK is a boolean variable in this case. It sets when correct cable choosen.
 

something like this makes more sense

Code:
int findType(int *v){ // the cable types
		int straight[8] = {1, 2, 3, 4, 5, 6, 7, 8};
		int cross[8] = {3, 6, 1, 4, 5, 2, 7, 8};
		int roll[8] = {8, 7, 6, 5, 4, 3, 2, 1};
		if ( checkEachType(v, straight) )  return 1;
               	if ( checkEachType(v, cross) )     return 2;
               	if ( checkEachType(v, roll) )        return 3;
		return 0;
}
int checkEachType(int *unknown, int *standard){
		int i;
		for(i = 0; i < 8; i++){
			if(unknown[i] != standard[i]) 	return 0;
		}
   		return 1;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top