When I put a number in flag[1][2], I will automatically put that same number in flag[2][0].
Why?
#include <iostream>
using namespace std;
void inicializarFlag(void);
void imrpimeflag(void);
int flag[2][2];
int main(){
int i, j, escolha;
inicializarFlag();
cout<<"digite as posicoes e o valor: "<<endl;
cin>>i;
cin>>j;
cin>>escolha;
flag[i][j] = escolha;
imrpimeflag();
return 0;
}
void inicializarFlag(void){
for (int i=0; i<=2; i++){
for(int j=0; j<=2; j++){
flag[i][j] = 0;
}
}
}
void imrpimeflag(void){
for (int i=0; i<=2; i++){
for(int j=0; j<=2; j++){
cout<<"["<<i<<"]["<<j<<"]: "<<flag[i][j]<<endl;
}
}
}
When you initialize the array int flag[2][2], you are initializing a 2x2 array, not a 3x3 array. Since arrays are zero indexed, flag[2][0] places an int in the zeroth column of the third row of the 2x2 flags array, which is out of bounds. The behavior of placing an element out of the bounds of an array is undefined, and can lead to problems like you describe.
Declare int flag[3][3] and the code should work.
Related
A c ++ problem where I have to determine if the user hit the target. After entering the coordinates of where enemies coulb be, the user then enters more coordinates and I must compare them, print YES if the element[i] of the attacks matches any element of enemies [n]. I know that I'm comparing positions and not elements that's why it's not working but I'm lost.I also tried to solve it by making only one array but it felt better this way.
#include <iostream>
using namespace std;
int main()
{
int n, k, b;
int enemies[];
int attacks[];
cin>>n;
for (int i=0; i<n; i++) {
cin>>b;
enemies[i]=b;
}
cin>>k;
for (int i=0; i<k; i++) {
cin>>b;
atacks[i]=b;
}
for(int i=0; i<k; i++){
if(atacks[i]==enemies[i]){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
return 0;
}
Your code likely doesn't work because this line:
if(atacks[i]==enemies[i])
requires that matching attack and enemy should have the same index in their arrays.
As suggested in the comments, you need to iterate over ALL enemies for EACH attack, which is "O(n*k) solution"
I have a bit of a problem, I am writing a program to ask the user to enter numbers for a Sudoku grid, and then store them in a 2-d array. I know how to print out the array to show the Sudoku grid, But I am having trouble getting the array elements set to the numbers that the user enters, can anyone help?
This is all that I have, which I know is not much but I have only ever done this with 1-d arrays before.
Code:
#include <iostream>
using namespace std;
void fillGrid1(int grid1, int sizeOfArray) {
for(int x = 0; x < sizeOfArray; x++) {
grid1[x][9] = x;
}
}
int main()
{
int grid1[9][9];
fillGrid1(grid1, 9);
for(int row = 0; row < 9; row++) {
for(int column = 0; column < 9; column++) {
cout << grid1[row][column] << " ";
}
cout << endl;
}
}
Here you have two functions, one to interactively fill the hole sudoku by getting the user input. The other for printing the sudoku. With the little information you gave it's what I think you seek:
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
void interactiveSudokuFill(int grid1[9][9]){
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
string theString;
cout<<"Write the value to prace in Sudoku["<<y<<"]["<<x<<"] :"<<endl;
std::getline(cin,theString);
int nr=atoi(theString.c_str());
grid1[y][x]=nr;
}
}
}
void printSudoku(int grid[9][9]){
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
cout<<"["<<grid[y][x]<<"]";
}
cout<<endl;
}
}
int main()
{
int grid1[9][9];
interactiveSudokuFill(grid1);
printSudoku(grid1);
}
There are other more safe/elegant ways of doing this(for example user input should have been checked before delievering it to atoi()), but this way is the simpler I can think of.
Firstly, you're taking in an int where you expect an array:
void fillGrid1(int grid1, int sizeOfArray)
// ^^^^^^^^^
This should be something of the form,
void fillGrid1(int grid1[9][9], int sizeOfArray)
Next is that you should use a nested loop to access the elements of the multidimensional array:
void fillGrid1(int grid1[9][9], int sizeOfArray)
{
for (int i = 0; i < sizeOfArray; ++i)
{
for (int k = 0; k < sizeOfArray; ++k)
{
grid1[i][k] = x; // shouldn't x be the number the user entered?
}
}
}
You should also zero-fill your array:
int grid1[9][9] = {0};
I want to initialize values of 2-D array to 0. But it seems not to work.
Can I initialize values of my **array in constructor to be 0. If yes the How.
My code is.
#include <iostream>
using namespace std;
int main(){
int row, col;
cin>>row;
cin>>col;
int **array=new int*[row];
for (int i=0; i<row; i++){
array[i]=new int[col];
}
for (int i=0; i<row;i++){
for (int j=0; j<col; j++){
array[i][j]={'0'};
cout<<array[i][j]<<" ";
}
cout<<endl;
}
}
Further can someone explain if I have to replace ith elem from the array with some other element, how would I deal with memory allocation.
You probably want array[i][j]=0;. Why were you using the char type '0'?
However, there is an easier way: array[i]=new int[col]();, just add () to value initialize each column.
There also is a better way:
std::vector<std::vector<int>> array(row, std::vector<int>(col));
For your first comment, you would need to create a new array with the new size, copy over all the data, and the delete your old 2-d array.
For your second comment, here is an example:
struct A
{
int **array;
A(int row, int col) : array(new int*[row])
{
for (int i=0; i < row; i++)
{
array[i]=new int[col]();
}
}
};
PS: You can save yourself a lot of work by using std::vector.
this codes works on gcc
#include <iostream>
using namespace std;
int main(){
int row, col;
cin>>row;
cin>>col;
int **array=new int*[row];
for (int i=0; i<row; i++){
array[i]=new int[col];
}
for (int i=0; i<row;i++){
for (int j=0; j<col; j++){
array[i][j]=0;
cout<<array[i][j]<<" ";
}
cout<<endl;
}
}
to replace ith element of the array with some other element we can do something lyk
int *aa = new int[p];
swap(array[i], aa)
but for this to work logically correct u need to make sure that p >= size of the array array[i] is pointing to. In most of our use cases we have them equal.
You can try something like this:-
class A
{
public:
A():my2DArray() {0}
private:
B* my2DArray[max1][max2];
};
So my program makes a random (x,y) arrays, what i want to do is to make x a real number and y imaginary number and add them:
my main program
#include "complx.h"
int main(){
const int n = 2;
Complex a[n];
getData(a,n);
printArray(a,n);
isort(a,n);
printArray(a,n);
complex_sum(a,n);
return 0;
}
it also prints and arranges the arrays, but what I am interested in is how to add the arrays, when for example I would use 4 arrays (x,y)(x,y)(x,y)(x,y).
this is how i get a random numbers
void getData(Complex a[],int n){
int i;
srand(time(0)); //If comment this out, get same sequence with each run.
for(i=0; i<n; i++){
a[i].x = rand()%3; //3 and 10 are just for testing isort
a[i].y = rand()%10;
}
return;
}
and here is how i'm trying to add it:
void complex_sum(Complex a[], int n){
for (int i=0; i<n; i++)
cout<<"("<<a[i].x+a[i].x<<")";
cout<<endl;
return;
I am stuck on how to add (x,y)(x,y)= x+yi
thanks
I am not completely inline with the way you are attempting; but to sum n number of complex numbers stored in an array and print it on screen you can try the following:
void complex_sum(Complex a[], int n){
int real=0,img=0;
for (int i=0; i<n; i++){
real+=a[i].x;
img+=a[i].y;
}
cout<<"("<<real << "+" << img << "i)";
}
I am trying to create a program that adds a polymorphic number that are organized in Rows and columns, so hopefully if you take a look at the arrays I have created you will get an idea of what I am trying to do, but think of it as this way you have 3 arrays A, B, C and I am trying to calculate A+B=C.
But I don't get anything but foolishness, I need help because I know so little about data structures:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int i,j,A[10][10],B[10][10],C[10][10], nf, nc;
cout<<"#Rows: "<<endl;
cin>>nf;
cout<<"#Columns: "<<endl;
cin>>nc;
//For the A part
for(int i=0; i<=nf;i++){
cout<<"Enter the row Number # "<<i;
for(int j= 0; j<=nc;j++){
cout<<"Enter Column Column#"<<j<<endl;;
cin>>A[i][j];
}}
//For the B part
for(int i=0; i<=nf;i++){
cout<<"Enter Row # "<<i<<endl;
for(int j= 0; j<=nc;j++){
cout<<"Enter Column# "<<j<<endl;
cin>>B[i][j];
}}
//Calculation
for(int i=0; i<nf;i++)
for(int j=0;j<nc;j++)
C[i][j]= A[i][j]+ B[i][j];
//output
for(int i=0; i<nf;i++)
for(int j=0;j<nc;j++)
cout<<C[i][j];
system("PAUSE");
return EXIT_SUCCESS;
}
the bounds in you'r input for loops i guess is not what you want it to be or atleast it is not consistent with the calculation loop
for(int j= 0; j<=nc;j++) vs for(int j= 0; j<nc;j++)
You have to initialize your cells to 0. else they will contain junk value and that will be used for addition. You can do it by initialization or using a loop.
int main()
{
int nf, nc, A[10][10]={0}, B[10][10]={0}, C[10][10]={0};
}
You have unused variables i and j. they are not a reason for erroneous output but still avoid it.
you can find a simplified corrected form of your program here (Array Bounds have also been corrected)
for(int i=0; i<nf;i++)
for(int j= 0; j<nc;j++)
these loops are used in both input and output
I hope it will be your response:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i,j,A[10][10],B[10][10],C[10][10], nr, nc;
cout<<"#Rows: "<<endl;
cin>>nr;
cout<<"#Columns: "<<endl;
cin>>nc;
//For the A part
for(int i=0; i<nr;i++){
for(int j= 0; j<nc;j++){
cout<<"Enter the A["<<i<<"]["<<j<<"]"<<endl;
cin>>A[i][j];
}
}
//For the B part
for(int i=0; i<nr;i++){
for(int j= 0; j<nc;j++){
cout<<"Enter the B["<<i<<"]["<<j<<"]"<<endl;
cin>>B[i][j];
}
}
//Calculation
for(int i=0; i<nr;i++)
for(int j=0;j<nc;j++)
C[i][j]= A[i][j]+ B[i][j];
//output
for(int i=0; i<nr;i++)
for(int j=0;j<nc;j++)
cout<<C[i][j];
system("PAUSE");
return EXIT_SUCCESS;
}
Dont complicate things with many cout statements.
cout<<"Enter a["<<i<<"]"<<"["<<j<<"] : ";
cin>>a[i][j];
i = 0 to nf means you are reading nf+1 elements. So there is array out of bound. Be careful with i<nf and i<=nf.