Исправь ошибки в коде на c++: #include <iostream> #include <vector> using namespace std;…
- User
Исправь ошибки в коде на c++: #include #include using namespace std;
void input_v(vector &v_arr) { cout << "Enter the values: " << endl; for(int i=0;i<v_arr.size();i++) cin>> v_arr[i]; } void output_v(vector &v) { cout << "Output: " << endl; for(int i=0;i<v.size();i++) cout << v[i]<<"\t"; cout << endl; } void insert_elements(vector &v) { int pos; cout << "Enter the position: " << endl; cin >>pos; int n_el; cout << "Enter the number of elements you want to insert: " << endl; cin>>n_el; cout << "Enter the elements: " << endl; for(int it = pos; it<n_el; it++) { float num; cin >> num; v.insert(it, num); } cout << "Vector after inserting: " << endl; output_v(v); } int main() { int N; cout << "Enter the number of values: " << endl; cin>>N; vector v(N); input_v(v); insert_elements(v); return 0; }
- Assistant
#include #include using namespace std;
void input_v(vector &v_arr) { cout << "Enter the values: " << endl; for(int i=0;i<v_arr.size();i++) cin>> v_arr[i]; } void output_v(vector &v) { cout << "Output: " << endl; for(int i=0;i<v.size();i++) cout << v[i]<<"\t"; cout << endl; } void insert_elements(vector &v) { int pos; cout << "Enter the position: " << endl; cin >>pos; int n_el; cout << "Enter the number of elements you want to insert: " << endl; cin>>n_el; cout << "Enter the elements: " << endl; for(int it = pos; it<pos+n_el; it++) { float num; cin >> num; v.insert(v.begin()+it, num); } cout << "Vector after inserting: " << endl; output_v(v); } int main() { int N; cout << "Enter the number of values: " << endl; cin>>N; vector v(N); input_v(v); insert_elements(v); return 0; }
- User
Напиши исправления по пунктам
- Assistant
- Добавить директиву
#include <vector>для использования векторов. - Добавить директиву
using namespace std;для удобства использования стандартных функций и классов. - Изменить объявление вектора
v_arrнаvector<float> &v_arrв функцииinput_v, чтобы вектор передавался по ссылке и можно было изменять его значения. - В цикле
forв функцииinput_v, изменить условие наi<v_arr.size(), чтобы не выйти за границы вектора. - Изменить объявление вектора
vнаvector<float> &vв функцииoutput_v, чтобы вектор передавался по ссылке и можно было изменять его значения. - В цикле
forв функцииoutput_v, изменить условие наi<v.size(), чтобы не выйти за границы вектора. - В функции
insert_elements, изменить объявление переменнойitна , чтобы не возникало проблем при использовании в методе .
- Добавить директиву
- User
Напиши в чём ошибка в коде на c++ с cout и как её исправить: #include #include
using namespace std;
void input_some(map<int, float> &m, int &N) { for (int i = 0; i<N; i++) { int key; float num; cout << "Enter the new key: " << end; cin>>key; cout << "Enter the new value: " << end; cin>>num; m.insert({key,num}); } } void output_m(map<int, float> &m) { cout << "MAP output: " << endl; for(auto j = m.begin(); j != m.end(); j++) cout << j->first << "\t" << j->second << endl; } int main() { map<int, float> m{ {1, 1.5}, {2, 2.777}, {3, 3.666}, {4, 4.99999}, {5, 5.8} }; output_m(m); cout << "Enter the number of new elements: " << endl; int N; cin>>N; input_some(m, N); cout << "After changes: " << endl; output_m(m); return 0; }
- Assistant
Ошибка в коде заключается в использовании неправильного символа конца строки в операторах
cout. Вместоendнужно использоватьendl.Исправленный код:
#include <iostream> #include <map> using namespace std; void input_some(map<int, float> &m, int &N) { for (int i = 0; i<N; i++) { int key; float num; cout << "Enter the new key: " << endl; cin>>key; cout << "Enter the new value: " << endl; cin>>num; m.insert({key,num}); } } void output_m(map<int, float> &m) { cout << "MAP output: " << endl; for(auto j = m.begin(); j != m.end(); j++) cout << j->first << "\t" << j->second << endl; } int main() { map<int, float> m{ {1, 1.5}, {2, 2.777}, {3, 3.666}, {4, 4.99999}, {5, 5.8} }; output_m(m); cout << "Enter the number of new elements: " << endl; int N; cin>>N; input_some(m, N); cout << "After changes: " << endl; output_m(m); return 0; }