Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hi everyone!!! I was writing contest and actually this is not difficult question but I stuck on it. Please help, here my code that passed only three tests and failed on fourth test. Answer must not use function and pointers. Thanks in advance!
#include <iostream>
using namespace std;
int main()
{
long long int n;
cin>>n;
long long int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long long int max=a[0],min=[0],ind1=0,ind2=0;
for(int i=0;i<n;i++){
if(a[i]>=max) {
max=a[i];
ind2=i;
}
if(a[i]<=min) {
min=a[i];
ind1=i;
}
}
int sum=0;
if(ind1<ind2)
for(int i=ind1+1;i<ind2;i++){
if(a[i]%2==0) sum++;
}
else if(ind1>ind2)
for(int i=ind2+1;i<ind1;i++){
if(a[i]%2==0) sum++;
}
cout<<sum;
}
It looks like you're trying to count the number of even elements between the min and max element in the array?
It may have failed the last test because you set initial min/max to garbage. In the case where a[0] is initialised to zero, and all the elements in the array are negative, the reported max will be incorrect, for example. You need to set max to the minimum integer and vice versa.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
i'm trying to plus each elemnt of the loop result over the next elemnt
Like if i have a loop it gives me the numbers between 1~5 so i need 1+2+3+4+5
I tried below code but it didn't work
#include <iostream>
using namespace std;
int main() {
for (int i =1; i <=50 ;i++){
cout<<i<<" ";
cout<<(i +=i)<<endl;
}
return 0;
}
The variable 'i' is sort of a "control" variable that maintains the count. You are adding 'i' to itself and not actually printing it. If you need to print characters like a '+' you need to use quotation marks, unless they are variables.
The code you probably want to use is -
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i=1; i<n; i++){
cout<<i<<"+";
}
cout<<n<<endl;
return 0;
}
Or if you wanted the sum of all the elements, the statement 'i+=1' actually adds 'i' to itself, this is not good as 'i' is your iterator, or a control variable of some sort that maintains your count. You should not operate over it. You will need another variable to hold the sum as you loop through.
You probably want this in this situation -
#include <iostream>
using namespace std;
int main() {
int sum=0;
for(int i=1; i<=5; i++){
sum+=i;
}
cout<<sum<<endl;
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
int sumTo(int value)
{
int total(0);
for (int count=1; count <= value; ++count)
total += count;
return total;
}
Where do I add cout<<total; to print the results? I have tried to add the print function several locations without success -- either getting nothing or a large number (maybe a number previously stored in the location). Have debugger with the result showing total as 15 when value is equal to 5.
In addition, I have added a void function to print, but still no correct results.
Need help with the proper placement of the print function to yield the correct answer?
This should work :
#include <iostream>
int sumTo(int value)
{
int total = 0;
for (int count=1; count <= value; ++count)
total += count;
return total;
}
int main(void)
{
std::cout << sumTo(12);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Problem
This is similar to the previous problem, however there will be multiple integers in the input. You have to write a computer program to read each integer and print Even if the integer is divisible by 2, else print Odd. To help further, the number of integers (T) to read will be the first input to the computer program.
Input Format:
First line of input contains count of integers: T. T>=1
After that, each line contains the integer N.
Sample Input:
2
4
5
Sample Output:
Even
Odd
Note: There should be a newline after each output. Otherwise you might end up printing EvenOdd here, which will result in wrong answer.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0) cout<<"Even"<<endl;
else cout<<"Odd"<<endl;
}
return 0;
}
You don't need neither array nor vectors for this:
int main() {
int n;
cin >> n;
while(n--) {
int number;
cin >> number;
// do something with number
}
return 0;
}
Note that this structure is the most common to solve this kind of problems
your problem will be solved if you change
for(int i =0; i <sizeof(a)/sizeof(int); i++)
to
for(int i =1; i <sizeof(a)/sizeof(int); i++)
But, how'd you know the number of elemnts for the array beforehand?
To correct this, you don't declare the array statically. Take the first input, and allocate memory for that many ints.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0)
cout<<"Even"<<endl;
else
cout<<"Odd"<<endl;
}
return 0;
}
this code which u posted is working perfectly bro :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I just start to learn programming and here is a problem about fibonacci sequence:
It is asked to make a function with parameter k(means k-order of fibonacci) and parameter n(means the n th member of sequence) and use that function to get the m th member of a k-order sequence.
f0 = 0, f1 =0, ....fk-2 =0, fk-1=1;
when n=k, k+1,...
fn= fn-1+fn-2+....+fn-k; n=k,k+1,....
(letters and numbers on right-side of f are subscripts which mean the n th member, n-1 th member..)
following is my code:
int Fibonacci(int k, int n){
int result=0;
if (n==k-1) return 1;
if (n<k-1) return 0;
if (n>=k){
for(int i=n-1; i>i-k-1;--i){
result+=Fibonacci(k,i);
cout<<result<<endl;
}
return result;
}
}
new version
int result=0;
int Fibonacci(int k, int n){
if (n==k-1) return 1;
if (n<k-1) return 0;
if (n>=k){
for(int i=n-1; i>n-k-1;--i){
result+=Fibonacci(k,i);
cout<<result<<endl;
}
return result;
}
}
my question is why this code cannot come out correct answer? there should be some problems in the loop but I cannot find them. Will someone help me?
for(int i=n-1; i>i-k-1;--i)
i will pretty much always be greater than i-k-1
Maybe you mean for(int i=n-1; i>n-k-1;--i), but you should check.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have been solving this problem http://www.codechef.com/DEC13/problems/MARBLEGF/ and i am not getting why am getting runtime error again and again,can anyone please help me with this?
Thanks in advance..!!
Heres the code.
#include<iostream>
using namespace std;
int main()
{
long long int n;
long int q;
int i,a,b,sum_temp=0,flag=0;
char act[10];
cin>>n;
cin>>q;
int array[n],temp[n],temp2[n];
long int sum;
for(i=0;i<n;i++){
cin>>temp[i];
temp2[i]=0;
array[i]=0;
}
while(q>0){
for(i=0;i<3;i++){
cin>>act[i];
}
act[3]='\0';
a=act[1]-'0';
b=act[2]-'0';
if(act[0]=='S'){
if(array[b]==0){
for(i=0;i<=b;i++){
if(i>0){
array[i]=array[i-1]+temp[i];
}else{
array[i]=temp[i];
}
}
}
sum_temp=0;
for(i=a;i<=b && flag==1;i++){
sum_temp=sum_temp+temp2[i];
}
if(a>0){
sum=(array[b]-array[a-1])+sum_temp;
}
else{
sum=array[b]+sum_temp;
}
cout<<sum<<endl;
}
else if(act[0]=='G'){
temp2[a]=b;
flag=1;
}
else if(act[0]=='T'){
temp2[a]=-b;
flag=1;
}
q--;
}
return 0;
}
EDIT:
As per the link you provided, the range of N is 2 ≤ N ≤ 1000000.
Hence as suggested by #Retired Ninja, there is a possibility that you may be getting stack overflow.
Solution: Use a vector.
Among other things, this is not valid C++:
cin>>n;
cin>>q;
int array[n],temp[n],temp2[n];
Here n should be a compile time constant. Better is to use
std::vector<int> array( n );
One reason as to why you are getting run-time error is you maybe accessing out of bounds array:
b=act[2]-'0';
if(act[0]=='S'){
if(array[b]==0){
How do you know here that b is less then array size?
I suggest you run it through a debugger.