I was trying to solve the problem here- https://www.codechef.com/APRIL19B/problems/FENCE and initialized the array to 0 but when I try to access the value at arr[0][4] with n=4 and m=4, it prints a garbage value.
I tried to initialize using vector thinking I'm making some mistake in the initialization of array, it works for the sample testcase but still gives segmentation fault.
Here is my code -
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
long long n,m,k,res=0;
cin>>n>>m>>k;
//vector<vector<long long>> arr(n+2,vector<long long>(m+2,0));
long long arr[n+2][m+2]={0};
long long vec[k][k];
for(unsigned int it=0;it<k;it++){
int t1,t2;
cin>>t1>>t2;
arr[t1][t2]=1;
vec[it][0]=t1;
vec[it][1]=t2;
}
cout<<"values:"<<arr[1][4]<<endl;
for(unsigned int itr =0;itr<k;itr++){
int j = vec[itr][0];
int i = vec[itr][1];
//cout<<i<<" "<<j<<endl;
res+=4-(arr[i-1][j]+arr[i+1][j]+arr[i][j-1]+arr[i][j+1]);
}
cout<<res<<endl;
}
return 0;
}
Edit:
The sample input is:
Example Input
2
4 4 9
1 4
2 1
2 2
2 3
3 1
3 3
4 1
4 2
4 3
4 4 1
1 1
Example Output
20
4
The constraints:
1≤T≤10
1≤N,M≤10^9
1≤K≤10^5
1≤r≤N
1≤c≤M
the cells containing plants are pairwise distinct
I expect the output to be- 20 for the first testcase but get garbage value.
When declaring an array in C++, its size must be a constant expression - that is, the size must be known at compile time. Your compiler ought to be complaining about these lines because m, n, and k are uninitialized (more correctly, initialized to indeterminate values) at compile time:
long long arr[n+2][m+2]={0};
long long vec[k][k];
I think that array is getting out of bounds in arr[i-1][j] or arr[i+1][j] or arr[i][j+1] or arr[i][j-1] that why the error is coming.
Related
I am a novice to CP and while I was doing this problem of finding the second maximum among three numbers I wrote this code which however works for int but doesn't work for long even though they are the same data types.
Input Format:
The first line contains the number of triples, N.
The next N lines which follow each have three space separated integers.
Input Used:
3
1 2 3
10 15 5
100 999 500
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >>n ;
while(n--){
long a,b,c,d;//if i change long to int output is correct
scanf("%i%i%i",&a,&b,&c);
d=min(max(a,b),max(a,c));
printf("%i\n",d);// outputs 1 \n 10 \n 100 \n(\n means new line)
}
return 0;
}
Perhaps because you're using the wrong specifier. %i is for ints, but %li is for longs.
For this problem I need to to do multiple queries so I used a for loop for it
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>ans; //vector to store the ans
string s,e; //real dna and virus dna
cin>>s;
int q,start,match=0; // no. of queries, starting value of the query, var to store matching occurence
unsigned int step=0; //var to keep count of virus dna iteration
cin>>q;
for(int i=0;i<q;i++){//loop to iterate q times
cin>>start;
int l,r,x,c;
if(start==2){ // for 2nd type query
cin>>l>>r>>e;
for(int i=l-1;i<=r-1;i++){
if(s[i]==e[step]){
match+=1;
}
step+=1;
if(step== e.length()){
step=0; //starting again from start of virus
}
}
}
ans.push_back(match);
match=0; //reintializing value for next query
if(start==1){ //for 1st type query
cin>>x>>c;
s[x-1]=c; //replacing char at x-1 with c
}
}
for(int j=0;j<ans.size();j++){ //loop for ans output
cout<<ans[j]<<endl;
}
return 0;
}
but it terminates before it should for ex: for this input,
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
It would stop at 5th line and print 8 ,2 ,0, 0 whereas it should be 8, 2, 4. If I do individual queries without loop things work fine but any kind of loop doesn't work. Pls help. Also any suggestion for solving this kind of problems more efficiently will be very helpful to me.
The bigger problem in your code is that the variable c is defined as int
int l,r,x,c;
when receive chars and not integers (T, in your example) so should be defined as char
int l,r,x;
char c;
If c is int, when you send 4 T to
cin>>x>>c;
x receive 4, c doesn't receive T (T isn't a valid int) so start the next iteration of the external cycle, is called
cin>>start;
when T remain in the buffer; start is integer so there is the same problem and the program end.
And, as pointed by soon, ans.push_back() should be inside the first if. Now a value (zero) is added also with rows starting with 1.
This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 7 years ago.
I just started learning c++ and came to Arrays. So what I want to do is perform some function on the array that was passed in function. So for this I tried small example
#include <iostream>
using namespace std;
void printArray(int data[]){
cout<<"len is :"<< sizeof(data)<<endl;
for(int i = 0 ;i<sizeof(data); i++){
cout<<data[i]<<" ";
}
cout<<endl;
}
int main() {
int data[] = {1,2,3,4,5,6};
printArray(data);
cout<<"in main :"<<sizeof(data)<<endl;
for(int i = 0 ;i<sizeof(data); i++){
cout<<data[i]<<" ";
}
return 0;
}
And Following is the output I obtained from the code
len is :8
1 2 3 4 5 6 738192384 -1126994503
in main :24
1 2 3 4 5 6 738192384 -1126994503 0 0 -1061892411 32677 0 0 -1665163256 32766 0 1 4196851 0 0 0 463403231 1946389667
I am not understanding where the process goes wrong. or is some thing in my code that make these weird changes to happen. and also the sizeof() is giving two values for the same array.Correct me where I am wrong anywhere.
I am using Eclipse software with c++ plugin added.
Thanks in advance for help!!
sizeof doesn't work if you pass an array to the function in this fashion. Try passing the size with the array or pass the begin and end pointers of the array.
If you don't mind templates, you can pass the array by reference:
template<unsigned length>
void printArray(int (&data)[length]) {
//length is the length of data
}
Please explain me how the code i am providing gives the output as :
1 2 3 4 5 6 7 8 9 10 11
#include<stdlib.h>
#include<iostream.h>
int main()
{
randomize();
int Num, Rndnum;
cin >> Num;
Rndnum = random(Num) + 7;
for (int N =1; N<=Rndnum; N++)
cout << N <<"";
}
Please explain me this code snippet
Well you are taking an input Num from the user and passing that to a random() function. You are then taking the returned value from that function and adding 7 to it and assigning it to Rndnum. Finally you are looping through from 1 to the Rndnum and printing of each of those numbers (1, 2, ...., Rndnum).
In the case of printing out 1 - 11 you must have gotten a return value of 4 from random(Num).
since I cannot see neither the function randomize() nor random(), I cannot tell you what they do but in this case the function call random(Num) gives back a 4, so Rndum adds up to 11.
Lastly, the for-loop repeates 11 (1 to inclusively 11) times and each time the output is the counter N itself.
So depending on what random does to your variable Num the number of iterations of your loop change.
Hope that helps!
P.S. If you want to look into random numbers in c++, take a look here
C++ rand
#include<iostream.h>
#include<fstream.h>
ifstream f("date.in");
using namespace std;
int i;
int P(int a[100],int k,int max)
{
max=a[1];
for(i=2;i<=k;i++)
if(a[i]>max)
max=a[i];
return max;
}
int main()
{
int x,a[100],n;
f>>n;
for(i=1;i<=n;i++)
f>>a[i];
for(i=2;i<=n;i++)
a[i]=P(a,i,x);
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
}
My "date.in" file consists of the following :
12
4 6 3 7 8 1 6 2 7 9 10 8
As the title states, the program should modify the array from within the file such that each number has the maximum value found in the array up to, and including, the position of that respective number. I've gone through it a hundred times but cannot figure out what's wrong with my code.
When compiled, I get the following:
4 6 3 7 8 8 6 8 7 9 10 10
Any assistance would be appreciated.
int i;
Globals are usually a bad idea. Because this loop:
for(i=2;i<=n;i++)
a[i]=P(a,i,x);
and this loop:
for(i=2;i<=k;i++)
if(a[i]>max)
max=a[i];
are running "at the same time", and thus i in the first one is NOT counting from 2 to n properly, it's only actually getting the first index and then the even indexes. (Check your results, the even indexes are 100% correct: x 6 x 7 x 8 x 8 x 9 x 10). If you use counters local to each loop: for(int i=2; ... then this problem wouldn't be happening.
Also your entire design is slow. Not sure why you did it that way, because it can be done easily in a single pass: http://ideone.com/LmD0HX.
And use <iostream> not <iostream.h>. They're actually different files.