(Задание 00 курса Assembly Language)
A warmup assignment

Write a C program that dynamically allocates an array of two bytes. The memory should not be initialized at first. The output should be:

1) The number of ‘ones’ in the memory allocated and the number of ‘zeros’ in the memory allocated.
2) The Hamming distance between the two bytes (the number of corresponding bits in the bytes that are are different.)
3) The bit pattern in each byte. (forwards and backwards.)
4) The value of each byte taken as an unsigned integer.
5) The hex value of the first byte as a bit pattern. (e.g. if the byte is 01111110 then the hex value is 7E)

The program should then prompt the user for two specific characters which are used to initialize the two bytes and the above output should be repeated.

The results should be submitted as simple C source code in a SINGLE TEXT FILE (i.e. all functions should be in the same file as main) so that the T.A.’s can EASILY cut and paste into a compiler of their choosing and run them. If the code does not run it gets a zero so keep it simple. However if you feel that it should have run you will be able to show the T.A. why at their office hours.

Grading
The assignment is out of 10. Each type of output is worth 2 points. You will get 2 if the output is correct and 0 if its not. If the output is not clearly annotated you will lose up to 3 points off the total. The assignment has the same weight as the other assignments. The TA’s will not grade the code.

Solution

#include
#include

int countZero(int b[2][8]);
int countHam(int b[2][8]);
void printPatterns(int bits[2][8]);
void countValues(int bits[2][8]);
void countHex(int bits[2][8]);

int main(void){
	int i, j;
	char c1, c2;   

	int bits[2][8];

	unsigned char bitmask = 0x80;
	for(i = 0; i<8; i++){
		if(c1& bitmask) {bits[0][i] = 1;}
			else bits[0][i] = 0;
		bitmask = bitmask>>1;
	}
	bitmask = 0x80;
	for(i = 0; i<8; i++){
		if(c2& bitmask) {bits[1][i] = 1;}
			else bits[1][i] = 0;
		bitmask = bitmask>>1;
	}

	countZero(bits);
	countHam(bits);
	printPatterns(bits);
	countValues(bits);
	countHex(bits);

	printf("\n\n\nEnter two symbols: ");
	scanf("%c%c", &c1, &c2);

	bitmask = 0x80;
	for(i = 0; i<8; i++){
		if(c1& bitmask) {bits[0][i] = 1;}
			else bits[0][i] = 0;
		bitmask = bitmask>>1;
	}

	bitmask = 0x80;
	for(i = 0; i<8; i++){
			if(c2& bitmask) {bits[1][i] = 1;}
			else bits[1][i] = 0;
		bitmask = bitmask>>1;
	}

	countZero(bits);
	countHam(bits);
	printPatterns(bits);
	countValues(bits);
	countHex(bits);
}

int countZero(int b[2][8]){
	int i, j, k = 0;
	for(i = 0; i < 2; i++){
        for(j = 0; j < 8; j++)
            if(b[i][j] == 0) k++;}
	printf("%d\n%d\n", k, 16-k);
	return k;
}

int countHam(int b[2][8]){
	int i, j, k = 0;
	for(i = 0; i < 8; i++)
		if(b[0][i] != b[1][i]) k++;
	printf("%d\n", k);
	return k;
}

void printPatterns(int bits[2][8]){
	int i, j;
	for(i = 0; i < 2; i++){
        for(j = 0; j < 8; j++)
            printf("%d", bits[i][j]);
            printf("\n");
        }

    for(i = 0; i < 2; i++){
		for( j = 7; j >= 0; j--)
			printf("%d", bits[i][j]);
            printf("\n");
        }
}

void countValues(int bits[2][8]){
	int j;
	unsigned int val[2];
	val[1]=0;
	val[0]=0;
    for(j = 0; j < 8; j++){
		val[0] = val[0] + bits[0][j]*pow(2, 7-j);
	}
	for(j = 0; j < 8; j++){
		val[1] = val[1] + bits[1][j]*pow(2, 7-j);
    }
    printf("%d\n", val[0]);
	printf("%d\n", val[1]);
}

void countHex(int bits[2][8]){
	int i;
	unsigned int hex[2];
    hex[0] = 0; hex[1] = 0;

	hex[0] = bits[0][0]*8 + bits[0][1]*4 + bits[0][2]*2 + bits[0][3];
	hex[1] = bits[0][4]*8 + bits[0][5]*4 + bits[0][6]*2 + bits[0][7];
    char hex1[2];
    for(i = 0; i < 2; i++){
		if(hex[i] == 10) {hex1[i] = 'A';}
			if(hex[i] == 11) {hex1[i] = 'B';}
				if(hex[i] == 12) {hex1[i] = 'C';}
					if(hex[i] == 13) {hex1[i] = 'D';}
						if(hex[i] == 14) {hex1[i] = 'E';}
							if(hex[i] == 15) {hex1[i] = 'F';}
								else{
									if(hex[i] == 0) hex1[i] = '0';
									if(hex[i] == 1) hex1[i] = '1';
									if(hex[i] == 2) hex1[i] = '2';
									if(hex[i] == 3) hex1[i] = '3';
									if(hex[i] == 4) hex1[i] = '4';
									if(hex[i] == 5) hex1[i] = '5';
									if(hex[i] == 6) hex1[i] = '6';
									if(hex[i] == 7) hex1[i] = '7';
									if(hex[i] == 8) hex1[i] = '8';
									if(hex[i] == 9) hex1[i] = '9';
									}
						}
	printf("%c%c\n", hex1[0], hex1[1]);
}

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

2 Responses to Задание 0: Hamming Distance, C/C++

Avatar

» Описание курса CSS

Июнь 7th, 2009 at 0:48

[...] Задание 0: Hamming Distance, C/C++ [...]

Avatar

» Описание курса Computer Science Student

Сентябрь 28th, 2009 at 2:04

[...] Задание 0: Hamming Distance, C/C++ [...]

Comment Form


Warning: Parameter 1 to id_generic_callback() expected to be a reference, value given in /home/users/f/freetonik/domains/css.freetonik.com/wp-content/plugins/intensedebate/intensedebate.php on line 911

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 8625678 bytes) in /home/users/f/freetonik/domains/css.freetonik.com/wp-includes/functions.php on line 959