You are on page 1of 4

Homework #0

Name : Abdulaziz Alwasel


ID : 433101425



Q1:

public class Poly {

public double evalPoly(double [] A, int n, double x){

int current = n-1;
double total = 0.0;

for(int i = 0; i<n; i++){

int product = 1;

while(current!=0){
for(int j=0; j<current; j++){

product*=x;

}
}

total += A[current]*product;
current --;
}

return total;
}
}

Q2: 2
Q3:
public void f(int A[],int n){

int len = 2*n;

for(int i = 0; i<len; i++){


for(int j = i; j<len; j++){

if(A[i]-A[j]==1){

A[i]=0;
A[j]=1;

}
}
}

}
Q4:
public int common(int A[], int n, int B[], int m){
int [] isCommon = new int[n];
int filler = 0;
int commonElements = 0;

for(int i = 0 ; i<n ; i++){
int current = A[i];

for(int j = 0 ; j<m ; j++){

if(current==B[j]){
boolean repeat = false;

if(filler!=0){
for(int k = 0 ; k<filler ; k++){
if(current==isCommon[k]){
repeat=true;
break;}
}
}

if(repeat==false){
commonElements++;
isCommon[filler] = current;
filler++;
}
break;
} } }

return commonElements;

}
Q5:
public void rearrange(int A[], int n){

int len = n;

for(int i = 0; i<len; i++){


for(int j = i; j<len; j++){

if(A[i]>=0){
int temp = A[i];
A[i]=A[j];
A[j]=temp;

}
}
}

}
Q6:
public int subString(char str[], int n, char tem[], int m){

int indx = -1;
int isItReally = 0;
int first = 0;

for(int i = 0 ; i<n ; i++){

if (tem[0]==str[i]){
isItReally++;
int k = (i+1);
first = i;

for(int j = 1 ; j<m ; j++){

if(tem[j]==str[k] && k!=n){
k++;
isItReally++;
}

else{

isItReally = 0;
break;
} } }
if(isItReally==m){
indx = first;
return indx;
} }
return indx;
}

You might also like