I have to make a binary search code in a file, however, it is giving me an error in the class vector_t
that I use to create a vector in which I have to search for a number.
This is the code where I do the binary search in a separate file:
#pragma once
#include<iostream>
#include "stack_v_t.hpp"
#include "vector_t.hpp"
using namespace AED;
int binary_search(vector_t<int>& v, int x){
stack_v_t<int> pila(2 * v.get_sz()); //le paso el tamaño de vector, una pila con el doble para evitar desbordamientos
int auxiliar = 0;
pila.push(x);
while (!pila.empty()) {
int derecha = pila.top(); //el valor que hay en top se iguala a lo que hay en derecha, en pila, necesitas meterlo en pila, lo sobreescribe para hacer otra vuelta
pila.pop();
int izquierda = pila.top(); // se borra el top, pero izquierda y derecha siguen teniendo lo que habia en el top
pila.pop();
if (izquierda > derecha) {return -1; }
else {
int c= (izquierda+derecha)/2;
if (v[c] == x) {
return c;
}
else if (x<v[c]){
derecha = c-1;
pila.push(derecha);
pila.push(izquierda);
}
else if (x>v[c]){
izquierda = c+1;
pila.push(derecha);
pila.push(izquierda);
}
}
}
}
Well, this is the main of this code, which is in another file:
#include<iostream>
#include "bs.hpp"
#include "vector_t.hpp"
using namespace std;
using namespace AED;
#define TAM 10
int main (void) {
vector_t<int> array(TAM); //las constantes se pasan asà cuando son clases vector
int i=0;
int buscado,x;
for (i=0;i<TAM;i++){
cout << "Introduzca el elemento " << i << endl;
cin >> x;
array[i]=x; // los guardo en v[i]
}
cout << "cual es elemento que busca? "<< endl;
cin>> buscado;
if (binary_search(array, buscado))
{
cout << "el elemento "<<buscado<<"ha sido encontrado "<<endl;
}
else{
cout << " el elemento " << buscado << "NO ha sido encontrado "<< endl;
}
return 0;
}
Well, I corrected the errors that came out in bs
and main_bs
, but the compiler points out errors in vector_t
and stack_v_t
.
These are the errors that I do not understand:
In file included from bs.hpp:5:0,
from main_bs.cpp:2:
stack_v_t.hpp:16:3: error: vector_t does not name a type
vector_t<T> v_;
^
stack_v_t.hpp: In constructor AED::stack_v_t<T>::stack_v_t(int):
stack_v_t.hpp:21:3: error: class AED::stack_v_t<T> does not have any field named
v_(max_sz),
^
stack_v_t.hpp: In member function T AED::stack_v_t<T>::top():
stack_v_t.hpp:33:11: error: v_ was not declared in this scope
return v_[top_];
^
In file included from /usr/include/c++/5/cassert:43:0,
from stack_v_t.hpp:7,
from bs.hpp:5,
from main_bs.cpp:2:
stack_v_t.hpp: In member function void AED::stack_v_t<T>::push(T):
stack_v_t.hpp:44:18: error: was not declared in this scope
assert(top_ < v_.get_sz() - 1);
^
In file included from bs.hpp:5:0,
from main_bs.cpp:2:
stack_v_t.hpp: In member function std::ostream& AED::stack_v_t<T>::write(std::ostream&):
stack_v_t.hpp:53:34: error: v_ was not declared in this scope
cout << << setw(2)<< v_.get_v(i) << << endl;
Class stack_v_t
provide it to me, also vector_t
:
I know it's a lot of code, but to be concise here I leave it:
#pragma once
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cassert>
using namespace std;
namespace AED {
template <class T>
class stack_v_t{
private:
vector_t<T> v_;
int top_;
public:
stack_v_t(int max_sz):
v_(max_sz),
top_(-1) {}
~stack_v_t(void) {}
bool empty(void){
return (top_ < 0);
}
T top(void){
assert(!empty());
return v_[top_];
}
void pop(void){
assert(!empty());
top_--;
}
void push(T dato){
assert(top_ < v_.get_sz() - 1);
top_ ++;
v_[top_] = dato;
}
ostream& write(ostream& os){
for(int i = top_; i >= 0; i--)
cout << " │ " << setw(2)<< v_.get_v(i) << " │" << endl;
cout << " └─────┘" << endl;
}
};
}
This is vector_t
#pragma once
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cassert>
using namespace std;
namespace AED {
template <class T>
class vector_t{
private:
T* v_;
int sz_;
public:
vector_t(int sz):
v_(NULL),
sz_(sz) {
crea_vector();
}
~vector_t(void){
destruye_vector();
}
int get_sz(void) const
{
return sz_;
}
T operator[](int pos) const{
assert((pos < sz_) && ((pos >= 0)));
return v_[pos];
}
T& operator[](int pos){
assert((pos < sz_) && ((pos >= 0)));
return v_[pos];
}
ostream& write(ostream& os) const{
os << setw(5) << sz_ << endl;
os << endl;
for(int i = 0; i < sz_; i ++){
v_[i].write(os);
os << " ";
}
return os;
}
private:
void crea_vector(void){
v_ = new T[sz_];
}
void destruye_vector(void)
{
if (v_ != NULL){
delete [] v_;
v_ = NULL;
}
}
};
template <>
ostream& vector_t<int>::write(ostream& os) const{
for(int i = 0; i < sz_; i ++)
os << setw(4) << v_[i] << " ";
return os;
}
}
ostream& operator<< (ostream& os, const AED::vector_t<int>& v)
{
v.write(os);
return os;
}
Thank you for your help, thank you