I'a trying to implement an algorithm, i want to input element by the user into 2D vector so that I have an element like this:
reference 1:
1 2 3
3 2 1
1 2 3
so I want to know how to push_back the element into 2D vector
my problem here:
std::vector<vector<int>> d;
//std::vector<int> d;
cout<<"Enter the N number of ship and port:"<<endl;
cin>>in;
cout<<"\Enter preference etc..:\n";
for(i=0; i<in; i++){
cout<<"ship"<<i+1<<":"<<' ';
for(j=0; j<in; j++){
cin>>temp;
d.push_back(temp);// I don't know how to push_back here!!
}
}
Here is the solution
std::vector<vector<int>> d;
//std::vector<int> d;
cout<<"Enter the N number of ship and port:"<<endl;
cin>>in;
cout<<"\Enter preference etc..:\n";
for(i=0; i<in; i++){
cout<<"ship"<<i+1<<":"<<' ';
for(j=0; j<in; j++){
cin>>temp;
d[i].push_back(temp);
}
}
C++ is a strong type language, d is a vector of vector:
for(i=0; i<in; i++){
cout<<"ship"<<i+1<<":"<<' ';
vector<int> row;
for(j=0; j<in; j++){
cin>>temp;
row.push_back(temp);// I don't know how to push_back here!!
}
d.push_back(row);
}
This should work:
vector<vector<int> > d;
int val;
for(int i = 0; i < in; i++){
vector<int> temp;
for(int j = 0; j < in; j++){
cin >> val;
temp.push_back(val);
}
d.push_back(temp);
temp.clear();
}
In general, we can add elements in a 2D matrix of vectors according to a similar approach as mentioned below :
#include<bits/stdc++.h>
using namespace std;
int main() {
int row,col;
cin>>row>>col;
vector<vector<int>>matrix;
for(int i=0;i<row;i++){
vector<int>temp;
for(int j=0;j<col;j++){
int val;
cin>>val;
temp.push_back(val);
}
matrix.push_back(temp);
}
return 0;
}
d[x].push_back(y);
This should work for you.
There are two methods to perform this task:
vector<vector<int> > v;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
v[i].push_back(data);
}}
vector<vector<int> > v;
for(int i=0;i<n;i++){
vector<int> x;
for(int j=0;j<m;j++) x[j].push_back(data);
v.push_back(x);
}
/* Below takes user input for n x n array */
vector<vector <int>> arr(n);
for (int i = 0; i < n; i++) {
arr[i].resize(n);
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
This is what you can do
int in;
std::cout << "Enter the N number of ship and port:" << std::endl;
std::cin >> in;
// declare a vector d of size 'in' containing vectors of size 0;
std::vector<std::vector<int>> d(in, std::vector<int>());
std::cout << "\Enter preference etc..:\n";
for(i=0; i<in; i++){
std::cout << "ship" << i+1 << ":" << ' ';
for(j=0; j<in; j++){
int temp;
std::cin >> temp;
d[i].push_back(temp); // now you can push_back here!!
}
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cout << "Please enter the no. of edges: ";
cin >> N;
vector<vector<int>> outer;
for(int i = 0; i < N; i++)
{
vector<int> temp;
for(int j = 0; j < 1; j++)
{
int u, v;
cin >> u;
cin >> v;
temp.push_back(u);
temp.push_back(v);
};
outer.push_back(temp);
temp.clear();
};
for(int i = 0; i<outer.size(); i++)
{
for(int j = 0; j < outer[i].size(); j++)
{
cout<<" "<<outer[i][j];
};
cout<<endl;
};
};
//this is a solution and it litrally works give it a try
//v(n) is must
//thats why your sol was giving problem
//we must specify rows
int main()
{
int value;
vector<vector<int>> v(n);
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
cin>>value;
v[i].push_back(value);
}
}
return 0;
}
vector<vector<int>> matrix; //declaring 2D vactor as matrix
int val;
cin>>val; //reading size of matrix in val
for(int i=0;i<val;i++){
vector<int> temp; //make temp vector for storing val for every iteration
for(int j=0;j<val;j++){
int x;
cin>>x;
temp.emplace_back(x);
}
matrix.emplace_back(temp); // push back to matrix, temp vector's all value for every iteration
temp.clear(); // clear temp vector for every iteration
}
Related
This is a C++ code and I feel its correct, yet it doesn't work.
I will explain a bit for understanding
t - number of tries.
n - size of array.
k - number of rotation.
Input: 1 2 3 4 5 .
for k=2, Output: 4 5 1 2 3.
Please advise on the same.
#include<iostream>
using namespace std;
int main() {
int t,n,k;
cin >> t;
int s = 0;
int a[1000];
int b[1000];
for (int i = 0; i < t; i++) {
cin >> n; //TAKING IN THE NUMBER OF ELEMENTS.
cin >> k; // TAKING IN AMOUNT OF ROTATION.
for (int j = 0; j < n; j++) {
cin >> a[j]; //READING ARRAY.
cout << " ";
}
for (int y = 0; y < n; y++) {
b[y + k] = a[y]; // REARRANGING ARRAY.
if (y + k >= n) {
b[s] = a[y];
s++;
}
cout << b[y] << " "; // SHOWING ARRAY.
}
}
return 0;
}
The Problem with your code is in line,
cout<<b[y]<<" "; // SHOWING ARRAY.
You must take it out of that for loop,since you are trying to print something that you have not given value.For example,first y=0 you set b[y+2] but you print b[y] which is not yet set.
Another problem which you will see for t>1 is initialisation of s has been done out of main i.e it is 0 for only first run.
the final code will be
#include
using namespace std;
int main()
{
int t;
cin>>t;
int n;
int k;
int a[1000];
int b[1000];
for(int i=0;i<t;i++)
{
int s=0; //Changed
cin>>n;
cin>>k;
for(int j=0;j<n;j++)
{
cin>>a[j];
// cout<<" "; //Not needed
}
for(int y=0;y<n;y++)
{
b[y+k]=a[y];
if(y+k>=n)
{
b[s]=a[y];
s++;
}
//cout<<b[y]<<" "; // changed this
}
for(int y=0;y<n;y++) //added this
cout<<b[y]<<" ";
}
return 0;
}
I need to solve a problem below in which I need to print final result as a float,
but this code gives the output as an int value.
#include <iostream>
using namespace std;
int main()
{
int T;
cin>>T;
for(int i=0; i<T; i++)
{
int N;
cin>>N;
int A[N],B[N];
float K=0;
for(int j=0; j<N; j++)
cin>>A[i];
for(int j=0; j<N; j++)
cin>>B[i];
for(int j=0; j<N; j++)
K=(B[i]/A[i]) + K ;
cout<<K<<endl;
}
return 0;
}
Input:
1
2
4 2
10 6
Output: 6
Desired output: 5.500000
With some C++14 trickery you can simplify your computation to:
float K = std::inner_product(begin(B), end(B), begin(A), 0.0f, std::plus<float>{}, std::divides<float>{});
Demo
Also note that dynamic arrays are a GCC extension, so the following code is not portable:
int N;
cin>>N;
int A[N],B[N];
We can use a simple vector instead (I did it for the code snippet above):
vector<int> A, B;
int tmp;
for(int j=0; j<N; j++){
cin >> tmp;
A.push_back(tmp);
}
for(int j=0; j<N; j++){
cin >> tmp;
B.push_back(tmp);
}
Explanation:
std::inner_product is primarily meant for... well, computing the inner product (sum of pairwise multiplications) on two collections. However, it parameterizes both the "sum" aspect and the "product" aspect, so we can sub out the "product" for a "division". The C++14 std::divides and std::plus function objects make it easier for us to specify the operations we want (else we could write a lambda for C++11).
You need type cast one of the values of the calculation to be a (float) for the result to be a float value:
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
for(int i = 0; i < T; i++)
{
int N;
cin >> N;
int A[N], B[N];
float K = 0;
for(int j = 0; j < N; j++)
cin >> A[j];
for(int j = 0; j < N; j++)
cin >> B[j];
for(int j = 0; j < N; j++)
K += (float)B[j] / A[j];
cout << K << endl;
}
return 0;
}
I have ruby version of this program & trying to do the same in C++
The input must be:
2 # Number of pairs
562 -881 # First pair
310 -385 # Second pair
And output:
-319
-75
It's working fine with one array of 2 numbers and breaks if pairs > 2. What's wrong in my for loops?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int iter;
cin >> iter;
int arr[2];
for (int i=0; i<iter; i++) {
for (int n=0; n<2; n++) {
// Enter numbers
cin >> arr[n];
}
}
for (int num=0; num<2; num++) {
sum+=arr[num];
}
for (int i=0; i<iter; i++) {
// Get the sum of numbers
cout << sum << endl;
}
return 0;
}
Thanks for any help!
for (int i=0; i<iter; i++) {
for (int n=0; n<2; n++) {
// Enter numbers
cin >> arr[n];
}
}
In first iteration values are entered in arr and again in second iteration previous values are overwritten (similar in next iterations if any ). This is the problem .
Solution -
#include <iostream>
using namespace std;
int main() {
int iter;
cin >> iter;
int arr[2];
int sum[iter]; // declare sum as array with iter number of elements
for(int i=0;i<iter;i++){
sum[i]=0; // initialize elements of sum to 0
}
for (int i=0; i<iter; i++) {
for (int n=0; n<2; n++) {
// Enter numbers
cin >> arr[n]; // take iput
sum[i]+=arr[n]; // add numbers and store in sum
}
}
for (int i=0; i<iter; i++) {
// Get the sum of numbers
cout << sum[i] << endl; // print values in sum after taing all inputs
}
return 0;
}
You're overwriting the contents of arr on each iteration of the loop. Try something like this (live demo here):
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int iter;
cin >> iter;
int arr[2];
for (int i=0; i<iter; i++) {
for (int n=0; n<2; n++) {
// Enter numbers
cin >> arr[n];
}
for (int num=0; num<2; num++) {
sum+=arr[num];
}
cout << sum << endl;
}
return 0;
}
There is something wrong in the if condition but it's the 7th day I have no clue. It prints the taken integer like this
mike .. mike1 .. mike12 .. mike123
But actually I need it to be like this
mike .. mike1 .. mike2 .. mike3
Can anyone help? this is my code :
#include <bits/stdc++.h>
using namespace std;
string str(int o){
stringstream ss;
ss<<o;
return ss.str();}
int main(){
int n;
cin>>n;
int z[n];
for(int p=0;p<n;p++)
{
z[p]=-50;
}
string x[n];
int k;
for(int i=0;i<n;i++){
k=1;
cin>>x[i];
for(int j=0;j<i;j++){
if(x[j]== x[i]){
x[i] = x[j] + str(k) ;
k++;
z[i]=0;
}
}
}
for(int q=0;q<n;q++){
if(z[q]==0)
cout<<x[q]<<endl;
else
cout<<"OK"<<endl;
}
return 0;
}
Remove the second for loop and get k outside the loop like this:
k=1;
for(int i=0;i<n;i++){
cin>>x[i];
x[i] = x[i] + str(k) ;
k++;
z[i]=0;
}
You can simplify all your code with C++11
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k = 1;
cin >> n;
vector<bool> z(n, false);
vector<string> x(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
x[i] += to_string(k);
++k;
z[i] = true;
}
for (int i = 0; i < n; ++i)
cout << (z[i] ? x[i] : "OK") << endl;
return 0;
}
I want to create a program that prompts the user for the size of the vector, then applies that size to the vector (filled with random numbers). This is what I have so far, but I am at a loss. There was a similar question on this site but it was closed due to being too ambiguous. How can I create x number of data.push_back instances based on a number entered by the user?
int main()
{
srand(time(NULL));
data.push_back(rand()%10+1);
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<<i<<" "<<data[i]<<endl;
}
return 0;
}
You could do the following if you have to use push_back
main()
{
int n;
cin >> n;
vector<int> data;
srand(time(NULL));
for (int i = 0 ; i < n; i++)
{
data.push_back(rand()%10+1);
}
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<<i<<" "<<data[i]<<endl;
}
return 0;
}
You may also pre-allocate the whole vector, which is more efficient.
main()
{
int n;
cin >> n;
vector<int> data(n);
srand(time(NULL));
for (int i = 0 ; i < n; i++)
{
data[i] = rand()%10 + 1;
}
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<<i<<" "<<data[i]<<endl;
}
return 0;
}
Here is one way:
Get a number from the user using std::cin and store it in an int or preferably std::vector<int>::size_type. Let's call this number n.
Loop n times, each time calling rand(), and push_back the result into your std::vector<int>
Note that std::vector<int> is a dynamic container and it's size can change at runtime. In your case, you do not have to specify it's size at compile time.
You could go for:`
srand(time(NULL));
std::vector<int> data;
int x;
std::cin>>x;
data.resize(x);
std::generate(data.begin(), data.end(), [](){return rand()%10+1;});
for(auto& num : data)
std::cout<<num<<std::endl;`
or, if you want to display the index as well:
for (int i=0; i<data.size(); i++)
cout<<"Vector: "<<i<<" "<<data[i]<<endl;
making sure you include the appropriate headers (<cstdlib>, <vector> and <algorithm>)