You are on page 1of 5

Homework 1

Cse31

Problem 1
a)
Binary Unsigned Signed 1’s 2’s Biased
Complement Complement
1100 1010 202 -54 110101 110110

0011 1001 57 +57 11000110 11000111

0110 1010 106 +106 10010101 10010110

1001 0000 144 -112 1101111 1110000

b)
Property Unsigned Signed 1’s Comp 2’s Comp Biased

Can represent positive T T T T T


numbers
Can represent negative F T T T T
numbers

Has more than one F T T F F


representation for 0
Use the same addition T F F F F
process as unsigned

c) -32768 is the most negative 16-bit 2’s complement integer.


d) +32767 is the most positive 16-bit signed integer.
Problem 2
void swapArray(int *a1, int *a2, int size){
int *temp = (int*) malloc(size*sizeof(int));
for(int i=0; i<size; i++){
*(temp+i)=*(a2+i);
*(a2+i)= *(a1+i);
}
for(int i=0; i<size; i++){
*(a1+i)= *(temp+i);
}
free(temp);
}

Problem 3
a) char* changeCase(char* str){
char* p;
char* result;
result = (char*) malloc((strlen(str)+1)*sizeof(char));

strcpy(result, str);

for(p=result; *p!='\0'; p++){


if(*p>='A' && *p<='Z'){//if the character is uppercase.
*p +=32;// then change it to lower case.
}
}
return result;
}
b) void changeCase_by_ref(char ** n){

*n = changeCase(*n);

void changeCase_name(char* name[], int i){

changeCase_by_ref( &(name[i]) );

Problem 4

a) #define MAX_NAME_LEN 128

typedef struct {
char name[MAX_NAME_LEN];
unsigned long sid;
} Student;

/* return the name of student s */


const char* getName(const Student* s) {
return s->name;
}

/* set the name of student s */


void setName(Student* s, const char* name) {

strcpy(s->name, name);
}
/* return the SID of student s */
unsigned long getStudentID(const Student* s) {

/* fill me in */
return s->sid;
}

/*set the SID of student s */


void setStudentID(Student* s, unsigned long sid) {

/* fill me in */
s->studentID= studentID;
}

b)
// Here in the above code we can't return a pointer to a locally declared variable Student. It will
return a null value as the variable is going to be a garbage value.

// SO Instead, you need to allocate a student first can solve the logical error.

// You should do like this:

void makeDefault(Student* pS) {

setName( pS, "John");


setStudentID( pS, 12345678);

}
//And then let the calling application allocate the student.

You might also like