Demo entry 3550342

Problem 1

   

Submitted by anonymous on Jan 22, 2016 at 06:36
Language: C. Code size: 2.6 kB.

// Problem 1
//	a)
#define MAX_KEY_PRESS 20

unsigned char curr_key, key_hist[MAX_KEY_PRESS], press=0

for (int i = press; i < MAX_KEY_PRESS; ++i)
{
	curr_key = getKey(); //returns character from keypad
	key_hist[i] = curr_key;

	if (curr_key == '*')
	{
		leds_off();
		buzzerOn();
	}
	else if (curr_key == '#')
	{
		leds_off();
		buzzer_off();
	}
	else 
	{
		leds_on(curr_key-0x30);
	}
}


/*	b)
* 		i) Goes through the loop only once since that kk starts as 1 (or true), but at the end of the first loop is set to 0 (or false) so it would not repeat the loop.
*       At the START of the FIRST loop:
*      	   kk = 1
*          mm = 4096
*          inVal = undefined
*
*	    At the END of the FIRST loop:
*		   kk = 0
*		   mm = 2048
*		   inVal = 4097
*
* 		ii) Goes through the loop 255 times since count starts at 1 and will keep increasing until it overflows when adding 1 to 255. 
* 		In the last three loops count is 253, 254, and 255 respectively.
*/


// c)
#define EXAMS 3 //defines a variable exams for the number of exams
#define LABS 5 //defines a variable labs for the number of labs
#define HWS 6 //defines a variable hws for the number of hws

char grades[14]; //makes an array of grades with datatype char
char exam_avg, lab_avg, hw_avg, final_grade='*'; //intializes many different char's 
// Both these chars are appropriatley sized, because a char is the smallest datatype and there will be no overflow issues with a max value of 150

exam_avg=((int)(grades[0]+grades[1]+grades[2]))/EXAMS; //sets exam average to be the quiotent of the sum of a few grades and the number of exams

/*
* A cast is when you temporarily change the datatype of a variable in order to work with other variables dataypes. 
* So, for this example it is trying th cast the sum of the different grade 1, 2, and 3 to an int 
* in order to divide by and inteven though each grade is specified as a char.
* However, this isn't really correct, because the quiotent produced is going to be an int even though the variable being set is a char.
*/


// d)
int calculateArea (side1, side2, hieght, shapeCode)
{
	int area

	if (shapeCode == R || shapeCode == P)
	{
		area = side1 * hieght;
	}
	if (shapeCode == T)
	{
		area = (side1 * hieght) / 2
	}
	if (shapeCode == Z)
	{
		area = ((side1 + side2) *hieght) / 2
	}
	return area;
}

void main()
{
	int sideLength1 = 5, sideLength2, height = 10
	char shapeType = T // R = rectangle, P = parallelogram, T = Triangle, Z = Trapezoid 

	calculateArea(sideLength1, sideLength2, hieght, shapeType);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).