Sei sulla pagina 1di 4

Agam Mittal

ECE 1125

ECE 1125 Homework #2


Problem 1
int** make2DArray(int rows)
{
int **x, i;
malloc(x, rows *sizeof(*x));
for (i=0; i<rows;i++) {
malloc(x[i]), len[i] * sizeof( **x ));
}
return x;
}
Problem 2
typedef struct{
float length;
float width;
} rectangle;
typedef struct{
int base;
int height;
} triangle;
typedef struct{
float radius;
float pointX;
float pointY;
} circle;

Agam Mittal
ECE 1125

Problem 3
void pmult(int startA, int finishA, int startB, int finishB, int* startD, int* finishD);
float coefficient;
*startD = avail;
while((startA <= finishA) && (startB<=finish)){
for (startA<=finishA, startA++){
for (startB <= finshB, startB++){
[startD].exp= terms[StartA].exp + terms[startB].expon
[startD].coeff=terms[StartA].coeff * terms[startB].coeff
startD++;
}
}
}
}
Complexity: O(m*n)
Problem 4
void stackEmpty()
{
if (top==-1){
return;
}
}
Problem 5
empty(stack s);
create(queue q);
while (end != 5) {
insert(q)
}
if (size[q]!=0) {
delete[q];
push[s]
}
if (size[s]!=0) {
pop[s]
insert[q];
}
}

Agam Mittal
ECE 1125

Problem 6

Problem 7
(a) One triple represents each sparse matrix; also, there are w bits per word, and
each word corresponds to a single triple. Every term that is not zero is stored
as 3 triples, and to store the # of rows, cols, and # of nonzero terms, there are
3 more triples. So, one needs 3x+3 triples for x non-zero numbers. The total
bit storage is w(3x+3)
typedefstruc{
int row;
int col;
int val;
}term;
term matrixA[][MAX] matrixB[][MAX];

void Madd() {
for(i=0;i<rows; i++){
for(j=0;j<cols;j++){
if ((matrixA[i][j].row == 1) && (matrixB[i][j].col==1)){

Agam Mittal
ECE 1125

matrixD[i][j] val = matrixA[i][j].value + matrixB[i][j].value;


}
}
}
}
8. X = A $ B / C / D + E / F $ (G + H)
Fully parenthesized : (A$(B/(C/D)) + E/(F$(G+H))
Converted postfix: A B C D / / $ E F G H + $ / +

Potrebbero piacerti anche