TATU Samarqand filiali
TTKT fakultet
103-guruh talabasi
Murtazoyev Dilshodning
Dasturlash fanidan
7-hafta mustaqil topshirig`i
javoblari.
1. double turidagi qiymatlar bilan ishlovchi vektro qanday e’lon qilinadi?
double turidagi qiymat vektorga qanday qo’shiladi? double turidagi qiymat
vektordan qanday o’chiriladi?
Javobi:
Double turidagi vector e’lon qilish sintaksisi quydagicha bo’ladi:
vectorvector_nomi;
Misol uchun:
vectorv;
Double turidagi qiymatni vektorga qo’shish sintaksisi:
vector_nomi.push_back(qiymat);
Misol uchun:
v.push_back(1.2);
Vektordan double turidagi qiymatni o’chirish sintaksisi:
vector_nomi.pop_back();
Misol uchun:
v.pop_back();
2. Quyidagi kodda (a) va (b) holatdan qaysi biri to’g’ri? Noto’g’ri holat
Mavjud bo’lsa, sababini tushuntirib bering.
Javobi:
a-chizmada xatolik bor bizlar vector elon qildik lekin uning uchun nechta joy ajratganimizni ko’rsatmadik v[0]=4 desak ya’ni birinchi elementiga e’lon qilsak u birinchi elementini tanimaydi shuning uchun xatolik beradi.
b-chizmada xatolik bermaydi chunki bizlar vector e’lon qilganimizda uning xotiradan nechta joy ajratganimizni ko’rsatdik.
Shu joygacha hamma elementlarni 0 qilib instalizatsiya qiladi vayaratadi.
Shu joygacha elementlarga murojat qilsak xatolik bermaydi.
3. Quyidagi massivni vektorda tasvirlang:
4. Quyidagi ikki o’lchovli massivni vektorda tasvirlang:
JAVOB:
#include
#include
#include
#include
typedef long longll;
typedefint li;
using namespace std;
vectorv(4);
vector> v1(4,v);
main()
{
v1[0][0]=1;
v1[0][1]=2;
v1[0][2]=3;
v1[0][3]=4;
v1[1][0]=5;
v1[1][1]=6;
v1[1][2]=7;
v1[1][3]=8;
v1[2][0]=9;
v1[2][1]=10;
v1[2][2]=11;
v1[2][3]=12;
v1[3][0]=13;
v1[3][1]=14;
v1[3][2]=15;
v1[3][3]=16;
}
5. (vector sinfini implementatsiyasi) C++ dagi vector sinfini o’zingiz mustaqil ravishda implementatsiya qiling. Standart vector sinfida ko’plab funksiyalar mavjud. Siz 12.2 chizmada UML diagrammada berilgan funksiyalarni implementatsiya qiling.
JAVOB:
#include
#include
#include
#include
using namespace std;
template
class Vector
{
public:
typedef T * iterator;
Vector();
Vector(unsigned int size);
Vector(unsigned int size, const T & initial);
Vector(const Vector& v);
~Vector();
unsigned intcapacity() const;
unsigned intsize() const;
bool empty() const;
iterator begin();
iterator end();
T &front();
T &back();
void push_back(const T & value);
void pop_back();
void reserve(unsigned int capacity);
void resize(unsigned int size);
T &operator[](unsigned int index);
Vector& operator=(const Vector&);
private:
unsigned intmy_size;
unsigned intmy_capacity;
T * buffer;
};
// Your code goes here ...
template
Vector::Vector()
{
my_capacity = 0;
my_size = 0;
buffer = 0;
}
template
Vector::Vector(const Vector& v)
{
my_size = v.my_size;
my_capacity = v.my_capacity;
buffer = new T[my_size];
for (inti = 0; i
buffer[i] = v.buffer[i];
}
template
Vector::Vector(unsigned int size)
{
my_capacity = size;
my_size = size;
buffer = new T[size];
}
template
Vector::Vector(unsigned int size, const T & initial)
{
my_size-size;
my_capacity = size;
buffer = new T [size];
for (inti = 0; i< size; i++)
buffer[i] = initial;
T();
}
template
Vector& Vector::operator = (const Vector& v)
{
delete[ ] buffer;
my_size = v.my_size;
my_capacity = v.my_capacity;
buffer = new T [my_size];
for (inti = 0; i
buffer[i] = v.buffer[i];
return *this;
}
template
typename Vector::iterator Vector::begin()
{
return buffer;
}
template
typename Vector::iterator Vector::end()
{
return buffer + size();
}
template
T& Vector::Vector::front()
{
return buffer[0];
}
template
T& Vector::Vector::back()
{
return buffer[size - 1];
}
template
void Vector::push_back(const T & v)
{
if (my_size>= my_capacity)
reserve(my_capacity +5);
buffer [my_size++] = v;
}
template
void Vector::pop_back()
{
my_size--;
}
template
void Vector::reserve(unsigned int capacity)
{
if(buffer == 0)
{
my_size = 0;
my_capacity = 0;
}
T * buffer = new T [capacity];
assert(buffer);
copy (buffer, buffer + my_size, buffer);
my_capacity = capacity;
delete[] buffer;
buffer = buffer;
}
template
unsigned int Vector::size()const//
{
return my_size;
}
template
void Vector::resize(unsigned int size)
{
reserve(size);
size = size;
}
template
T& Vector::operator[](unsigned int index)
{
return buffer[index];
}
template
unsigned int Vector::capacity()const
{
return my_capacity;
}
template
Vector::~Vector()
{
delete[]buffer;
}
intmain()
{
Vector v;
v.reserve(2);
assert(v.capacity() == 2);
Vector v1(2);
assert(v1.capacity() == 2);
assert(v1.size() == 2);
assert(v1[0] == "");
assert(v1[1] == "");
v1[0] = "hi";
assert(v1[0] == "hi");
Vector v2(2, 7);
assert(v2[1] == 7);
Vector v10(v2);
assert(v10[1] == 7);
Vector v3(2, "hello");
assert(v3.size() == 2);
assert(v3.capacity() == 2);
assert(v3[0] == "hello");
assert(v3[1] == "hello");
v3.resize(1);
assert(v3.size() == 1);
assert(v3[0] == "hello");
Vector v4 = v3;
assert(v4.size() == 1);
assert(v4[0] == v3[0]);
v3[0] = "test";
assert(v4[0] != v3[0]);
assert(v4[0] == "hello");
v3.pop_back();
assert(v3.size() == 0);
Vector v5(7, 9);
Vector::iterator it = v5.begin();
while (it != v5.end())
{
assert(*it == 9);
++it;
}
Vector v6;
v6.push_back(100);
assert(v6.size() == 1);
assert(v6[0] == 100);
v6.push_back(101);
assert(v6.size() == 2);
assert(v6[0] == 100);
v6.push_back(101);
cout<< "SUCCESS\n";
}
6. (stek sinfini vektordan foydalangan holda qayta implementatsiya qiling) 12.4 listingda GenericStack sinfi massivdan foydalanilgan holda implementatsiya qilingan. Uni vektordan foydalangan holda implementatsiya qiling.
JAVOB:
template
class Stack {
public:
Stack() = default;
Stack(std::initializer_listi) :n{} {
for (auto&x :i) {
elem.push_back(x);
++n;
}
}
Stack(const Stack&) = default;
Stack(Stack&&) = default;
Stack& operator=(const Stack&) = default;
Stack& operator=(Stack&&) = default;
~Stack() = default;
intsize() const {
return n;
}
bool isEmpty() const {
return (n == 0);
}
void push(T);
T pop();
T&peek();
private:
std::vectorelem;
int n;
};
template
void Stack::push(T t) {
elem.push_back(t);
++n;
}
template
T Stack::pop() {
if (isEmpty()) {
throw std::out_of_range("underflow");
}
T t = *elem.erase(elem.end()-1);
--n;
return t;
}
template
T& Stack::peek() {
if (isEmpty()) {
throw std::out_of_range("underflow");
}
returnelem[n-1];
}
Javobi:
template
class Stack {
public:
Stack() = default;
Stack(std::initializer_listi) :n{} {
for (auto&x :i) {
elem.push_back(x);
++n;
}
}
Stack(const Stack&) = default;
Stack(Stack&&) = default;
Stack& operator=(const Stack&) = default;
Stack& operator=(Stack&&) = default;
~Stack() = default;
intsize() const {
return n;
}
bool isEmpty() const {
return (n == 0);
}
void push(T);
T pop();
T&peek();
private:
std::vectorelem;
int n;
};
template
void Stack::push(T t) {
elem.push_back(t);
++n;
}
template
T Stack::pop() {
if (isEmpty()) {
throw std::out_of_range("underflow");
}
T t = *elem.erase(elem.end()-1);
--n;
return t;
}
template
T& Stack::peek() {
if (isEmpty()) {
throw std::out_of_range("underflow");
}
returnelem[n-1];
}
Do'stlaringiz bilan baham: |