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"
Related
My code is showing TLE when there when number of entries in array(n) is 1 *10^5. What should i do? I saw the submission status and in all cases it ran fine except in the last case when n is 100 000, it shows time limit error.
Problem:: https://codeforces.com/contest/474/problem/B
My solution: https://pastebin.com/5RLBirpF
Code:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
int arr[t];
int arrfreq[t];
int sum=1;
for(int i=0;i<t;i++ )
{
cin>>arr[t];
sum+=arr[t];
arrfreq[i]=sum;
}
int m;
cin>>m;
int qsn[m];
int k;
for(int i =0;i<m;i++)
{
cin>>qsn[i];
}
for(int j =0;j<t;j++)
{
if(k<arrfreq[j])
{
cout<<j+1<<"\n";
break;
}
if(k==arrfreq[j])
{
cout<<j+2<<"\n";
break;
}
}
}
You are using one loop for calculating the number of pile. What you can do instead is find the answer in the same loop in which you are taking the input for qsn. Just take the input of qsn and find the pile number in that loop itself. That will reduce your code's time complexity and remove the TLE error.
I saw other solutions on the internet and found out that std::lower_bound function is what has to be used to avoid T.L.E.
But one thing, as mentioned on internet, this function returns a pointer to the lower bound, then , why are we subtracting c from it and then +1 also(in the 2nd last cout line)??(refer to the code.)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,m,j,cnt=0,ans,x,sum=0;
cin>>n;
int a[n],c[n];
for(i=0; i<n; i++){
cin>>a[i];
sum+=a[i];
c[i]=sum;
}
cin>>m;
int b[m];
for(i=0; i<m; i++) cin>>b[i];
for(j=0; j<m; j++)
{
cout<<lower_bound(c,c+n,b[j])-c+1<<endl;
}
return 0;
}
I am trying to use bubble sort method for array with dynamically determined size. Here's the code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter n";
cin>>n;
int arr[n],swap;
cout<<"Enter number"<<endl;
cin>>arr[n];
for(int i=0;i<n-1;i++)
for(int j=0;i<n-i-1;j++)
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
for(int k=0;k<n;k++)
cout<<"arr["<<k<<"]="<<arr[k]<<endl;
return 0;
}
When I define the elements of the array in this way the program works:
const n=5;
int arr[n]={1,2,3,4,5)
But I need to enter the size of the array (n) and its elements from the keyboard. But when I run my code the program crashes after I enter the first number. Is there a way to fix it?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n, swap, temp;
vector<int> arr;
cout<<"Enter n";
cin>>n;
// Loop and accept the n values.
// You may need to take care of the new line.
for(int i = 0; i < n; ++i)
{
cout << "Enter a number : ";
cin >> temp;
arr.push_back(temp);
}
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
for(int k=0;k<n;k++)
cout<<"arr["<<k<<"]="<<arr[k]<<endl;
return 0;
}
Notice how a loop is used to extract n values from user. Also using a std::vector relieves you from writing code for a runtime sized array with new and delete.
Also, you inner loop was checking i<n-i-1 and incrementing j which should be j<n-i-1 instead, otherwise j will increment indefinitely till INT_MAX.
The key word is dynamic allocation. In C, the function is malloc. In Cpp, it can be new and delete. Although a vector can work well, it is just a kind of method by STL. Notice that my code may have safe problem.
#include <iostream>
using namespace std;
int main()
{
int n,temp;
cout<<"Enter n:";
cin>>n;
//dynamic allocation
int *arr=new int[n];
cout<<"Enter number."<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
}
//bubble sort
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
//output the array
for(int i=0;i<n;i++){
cout<<"arr["<<i<<"]="<<arr[i]<<endl;
}
delete [] arr;
return 0;
}
You can't take an integer array like that, you need to run a loop. You can take a string like that. There are lot of errors in the bubble sort logic as well, try the below code snippet. It should work fine. You need to dynamically allocate the array for arr
int n,r,swap,i,*arr;
cout<<"Enter n\n";
cin>>n;
arr = (int *)malloc((n)*sizeof(int));
cout<<"Enter numbers\n"<<n<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)//You're checking for i. you need to check for j
{
if(arr[j+1]<arr[j])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
}
}
//now print your arr
Includes : #include<stdlib.h>
As mentioned in my comment, you CANNOT dynamically declare the size of arrays in C++ (use std::vector if you are willing to achieve that).
You can then do this:
....
cin >> n;
vector<int> arr(n); // reserves space for `n` integers in the memory
....
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.
this is my code, PLease help me ! im using xcode.. i want to generate a sequence for a polynomial and the terms are xor'ed and made a feedback to the first input bit since it is 8 bit it is done 2^8-1 times.Alternate code will also be helpful Thanks in advance
#include "32bit.h"
#include<iostream>
using namespace std;
int main()
{
bool input[8];
int n;
bool out=0;
cout<<"Enter the no of terms ";
cin>>n;
int temp1[n];
int gen=0;
bool store[255];
cout<<"Input power of x in increasing order, Omit x^0";
for(int i=0;i<n;i++)
cin>>temp1[i];
cout<<"Enter key to generate ";
cin>>gen;
for(int m=0;m<255;m++)
{
store[m]=input[gen];
bool temp2[n];
int var=0;
for(int j=0;j<n;j++)
{
var=temp1[j];
temp2[j]=input[var];
}
int c=0;
for(int k=0;k<n;k++)
{
if(temp2[k]%2==1)
c++;
}
if(c%2==1)
out=1;
else
out=0;
for(int l=0;l<8;l++)
input[l+1]=input[l];
input[0]=out;
}
for(int p=0;p<255;p++)
cout<<store[p];
}
There is an out of bounds array access here:
for(int l=0;l<8;l++)
input[l+1]=input[l];
since input is only of size 8 and you are trying to write to input[8] (i.e. the non-existent 9th element) on the last iteration of this loop. I'm guessing it should probably be:
for(int l=0;l<7;l++)
input[l+1]=input[l];
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.