Deleting array of structures, changing structure adress - c++

I create dynamically two arrays of structures (player1 and player1temp). Than i want to make some calculation using data from player1 and save it to player1temp. Then i want to copy data from array player1temp to player1.
Is it a good solution?
struct team
{
int value1;
int value2;
};
int main()
{
srand(time(NULL));
team *player1=new team[5];
for(int i=0; i<5; i++)
{
player1[i].value1=rand()%20;
player1[i].value2=rand()%20;
cout<<"Player1 index: "<<i<<" "<<player1[i].value1<<" "<<player1[i].value2<<"\n";
}
team *player1temp=new team[5];
cout<<"\n\n";
for(int i=0; i<5; i++)
{
player1temp[i].value1=rand()%20;
player1temp[i].value2=rand()%20;
cout<<"Player1temp index: "<<i<<" "<<player1temp[i].value1<<" "<<player1temp[i].value2<<"\n";
}
delete player1;
player1=player1temp;
cout<<"\n\n";
for(int i=0; i<5; i++)
{
cout<<"Player1 index: "<<i<<" "<<player1[i].value1<<" "<<player1[i].value2<<"\n";
}
return 0;
}

Two errors:
delete should be delete[] if used for arrays.
The remaining array should be deleted too before the program ends.
A vector would be easier:
struct team
{
int value1;
int value2;
};
int main()
{
srand(time(NULL));
std::vector<team> player1(5);
for(int i=0; i<5; i++)
{
player1[i].value1=rand()%20;
player1[i].value2=rand()%20;
cout<<"Player1 index: "<<i<<" "<<player1[i].value1<<" "<<player1[i].value2<<"\n";
}
std::vector<team> player1temp = player1;
cout<<"\n\n";
for(int i=0; i<5; i++)
{
player1temp[i].value1=rand()%20;
player1temp[i].value2=rand()%20;
cout<<"Player1temp index: "<<i<<" "<<player1temp[i].value1<<" "<<player1temp[i].value2<<"\n";
}
player1 = std::move(player1temp);
//donĀ“t use player1temp anymore now
cout<<"\n\n";
for(int i=0; i<5; i++)
{
cout<<"Player1 index: "<<i<<" "<<player1[i].value1<<" "<<player1[i].value2<<"\n";
}
return 0;
}

Related

can anyone tell me how val[i*c+j] is working?

#include<iostream>
using namespace std;
int main()
{
int* val;
int r,c;
val = new int[r*c];
cout<<"Enter Rows:";
cin>>r;
cout<<"Enter cols :";
cin>>c;
for(int i= 0 ;i<r ;i++ )
{
for(int j=0 ; j<c; j++)
{
cin>>val[i*c+j];
}
}
for(int i=0; i<r ;i++)
{
for(int j =0 ;j<c;j++)
{
cout<<val[i*c+j]<<"\t";
}
cout<<endl;
}
delete []val;
return 0;
}
i am not getting how val[i*c+j] is working in this. Can anyone explain this to me.
i want to know how 2d array works dynamically and takes input from user and display it

assigning values for two dynamic arrays

hope you doing well.
so i have just started to do an assignment and the first thing i wanted to do was to create the two dynamic array. however, there is something wrong with the array i can't assign values to it. Here is the code:
void Room::memory(int **array){
int x,x2;
int count=0;
cout << "Array size? rows: columns: \n";
cin >> x >> x2;
array = new int*[x];
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
array[i][j]=count;
count++;
}
}
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
cout<< array[i][j]<< " | ";
}
cout << endl;
}
}
i always get the value 0 for my array. whether i use this line or not:
array[i][j]=count;
i tied to compare my code with someone else and it is the same steps but it doesn't work for me.
class Room{
private:
int **array;
public:
void memory(int **array);
};
Why do you do
for(int i=0; i<x;i++){
array[i]= new int[x2];
} twice?
at the same time, please change void memory(int **array); to void memory();
while you crested the 2nd 2D dynamic array,you did not initialize it...And just printed it... In the 2nd 2D array you also need to initialize before printing the all index values....
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
//here should be the initialization
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
cout<< array[i][j]<< " | ";
}
cout << endl;
}

Input element by user into 2D vector c++

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
}

Passing matrix to a function

I am getting an error when calling the 'printMat' function. My requirement is to create a matrix after accepting the number of rows and columns, and then take input into the matrix, then call the printMat by sending the matrix and print the elements. The error is as follows:
error: parameter 'a' includes pointer to array of unknown bo
und 'int []'
#include<iostream>
using namespace std;
int row,col;
void printMat(int* a[])
{
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cout<<a[i][j]<<" ";
}
}
}
int main()
{
cin>>row;
cin>>col;
int mat[row][col];
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cin>>mat[i][j];
}
}
printMat(mat);
return 0;
}
int* a[]
is an array of pointer, but you are passing a pointer to an array:
int (*a)[]
The reason it doesn't work is that arrays are just a nightmare. In C++, we use vectors instead.
#include<iostream>
#include<vector>
using namespace std;
void printMat(vector<vector<int>> mat)
{
for(vector<int> one_row : mat)
{
for(int one_cell_in_this_row : one_row)
{
cout << one_cell_in_this_row << ' ';
}
cout << endl;
}
}
int main()
{
int row,col;
cin>>row;
cin>>col;
vector< vector<int> > mat( row , vector<int>(col,0) );
// ^ ^
// initialize the vector ~~~~~/ |
// with 'row' items, each |
// of which is a vector |
// of 'col' integers. ~~~~~~~~~~~~~~~~~~~~~~~~~/
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
int current_entry;
cin>>current_entry;
mat.at(i).at(j) = current_entry;
}
}
printMat(mat);
return 0;
}
You can solve using pointer arithmetic. See following code.
#include<iostream>
using namespace std;
int row,col;
void printMat(int *a)
{
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cout<< *((a+i*col) + j)<<" ";
}
}
}
int main()
{
cin>>row;
cin>>col;
int mat[row][col];
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cin>>mat[i][j];
}
}
printMat((int *)mat);
return 0;
}
Other possible solutions are explained in this link

two-dimensional array not displaying proper output

i want the user to input values in a 2 dimensional array. he can also choose the size of each dimension
int main()
{
int x;
int y;
int *p;
cout<<"How many items do you want to allocate in dimension x?"<<endl;
cin>>x;
cout<<"How many items do you want to allocate in dimension y?"<<endl;
cin>>y;
p = new int[x,y];
for(int i=0; i<x; i++) //This loops on the rows.
{
for(int j=0; j<y; j++) //This loops on the columns
{
int value;
cout<<"Enter value: "<<endl;
cin>>value;
p[i,j] = value;
}
}
drill1_4 obj;
obj.CopyArray(p,x,y);
}
and then, ill output the two dimensional array via
class drill1_4
{
public:
void CopyArray(int*,int,int);
private:
int *p;
};
void drill1_4::CopyArray(int* a,int x,int y)
{
p = a;
for(int i=0; i<x; i++) //This loops on the rows.
{
for(int j=0; j<y; j++) //This loops on the columns
{
cout << p[i,j] << " ";
}
cout << endl;
}
getch();
}
logic seems fine, but lets say, if the user enters the numbers, the array should look like this
1 2
3 4
but instead, it looks like this:
3 3
4 4
The array is not displaying it correctly.
I don't know if you figured out the answer to your problem. The comments above tell you where you went wrong. Here is a possible answer.
#include <cstdio>
#include <iostream>
using namespace std;
class drill1_4
{
public:
void CopyArray(int**,int,int);
private:
int **p;
};
void drill1_4::CopyArray(int** a,int x,int y)
{
p = a;
for(int j=0; j<y; ++j) //This loops on the rows.
{
for(int i=0; i<x; ++i) //This loops on the columns
{
cout << p[i][j] << " ";
}
cout << endl;
}
cin.get();
}
int main()
{
int x;
int y;
int **p;
cout<<"How many items do you want to allocate in dimension x?"<<endl;
cin>>x;
cout<<"How many items do you want to allocate in dimension y?"<<endl;
cin>>y;
p = new int*[x];
for (size_t i = 0; i < x; ++i)
p[i] = new int[y];
for(int j=0; j<y; ++j) //This loops on the rows.
{
for(int i=0; i<x; ++i) //This loops on the columns
{
int value;
cout<<"Enter value: "<<endl;
cin>>value;
p[i][j] = value;
}
}
drill1_4 obj;
obj.CopyArray(p,x,y);
}
I am not sure I understood what you meant by x-dimmension. If you meant x running horizontally than I think that i and j for-loops shout be reversed as x will represent columns.