My program

#include<iostream>
#include<ctime>
#include <math.h>

using namespace std;


bool replace(int * & A , int &n, int i , int * w1 , int n1 , int *w2, int n2){

// to check if A[i ... (i+n1-1)] = w1 [0 ... (n1-1)] write a loop

int k = i;
int l =0;
int found = 1;

while ( k < (i+n1)){

if (A[k] != w1[l]){

found = 0;
break;
}

k = k+1;
l = l+1;
}

if (found == 1){ // w1 matches A at position i
we write a loop to replace w1 wirh w2
A[i ... (i+n2-1)] = w2 [0 ... (n2-1)]

int s = i;
int r = 0;
while (s < (i + n2) ){

A[s] = w2[r];

s = s+1;
r = r+1;
}

return true;

}

if (found == 0){ // w1 does not match A at position i
return false;
}
}

bool findAndReplace (int * & A , int &n, int * w1 , int n1 , int *w2, int n2){
we write a loop to check whenw1 matches A
//so we search for the position i = 0... n-n1
// since if w1 matches the last part of A[] then the index i would (n-1 - (n1-1))

int i = 0;
int found =0;

while (i <= (n-n1)){

if ( replace( A , n, i , w1 , n1 , w2, n2)){

found = 1;
return true;
}
i = i+1;
}
if (found == 0){
return false;
}
}
long value (int *A, int n, int i, int n1 ){
i need to compute the decimal value of the array A[i ... i + n1 -1]
to do so, i need to write a loop

int j = 0;
long value = 0;
long term = 0;

while (j <= (n1 - 1)){

term = A[i+j] * ( (long) pow(2.0 , j) );
value = value + term;
j = j+1;
}

return (value);
}

int main (){

int n;
cout<<"Enter n: "<<endl;
cin>>n;

int *A = new int [n];

srand (n);

for (int i =0; i<n ; i++){
A[i] = rand() %2;
}

for (int o = 0; o <6; o = o+1 ){
cout<<A[o]<<" ";
}
cout<<endl;

// testing the function
int w1[] = {1 ,1 ,1};
int w2 [] = {0 , 0, 0};

replace( A , n, 0 , w1 , 3 , w2, 3);
cout<<"Afterwards(Replace): "<<endl;
for (int o = 0; o <6; o = o+1 ){
cout<<A[o]<<" ";
}
cout<<endl;

findAndReplace ( A , n, w1 , 3 , w2, 3);
cout<<"Afterwards(FindandReplace): "<<endl;
for (int o = 0; o <6; o = o+1 ){
cout<<A[o]<<" ";
}

int A[8] = {1,1,0,0,1,1,1,1};

long q = value (A, 8, 1, 4 );

cout <<q <<endl;


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Person{
private:
        string name;
        int popularity;
        Person* Best;
public:
        Person(const Person &X){name = X.name; popularity = X.popularity; Best = X.Best;}
        Person& operator=( const Person& rhs ) { name = rhs.name; popularity = rhs.popularity; Best = rhs.Best; return *this;}
        Person(){name = ""; popularity = -1; Best = NULL;}
        Person(string x,int y, Person *Z){name = x; popularity = y; Best = Z;}
        Person(string x, int y){name = x;popularity = y; Best= NULL;}
        int get_popularity(){return popularity;}
        string get_name(){return name;}
        Person* get_best(){return Best;}
        void print_friends(){
                if (Best!=NULL)
                        cout<<"The Best friend of "<<name<<" is "<<Best->get_name()<<endl;
                else
                        cout<<name<<" Has no best friends :( "<<endl;}
                
};
Topic archived. No new replies allowed.