Hint: Missed newline ? while trying to solve the below quiz [closed] - c++

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 :)

Related

trying to plus each element of the loop over the next element [closed]

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;
}

this is showing wrong output when i am giving this more then one test cases [closed]

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
this is codechef January challenge's first problemcodechef.
When i am giving more than one test case ,it is giving wrong output. and when i am giving one test cases it is going right.
my code:-
#include<iostream>
using namespace std;
int long long N;
int main()
{
int i,j, N,t,d,k,sum=0;
cin>>t;
for(j=1;j<=t;j++)
{
cin>>N>>k>>d;
for(i=0;i<N;i++)
{
int num;
cin >> num;
sum += num;
}
for(i=0;i<N;i++)
{
sum= int(sum/k);
if(sum>d)
{
cout<<d<<endl;
}
else
{
cout<<sum<<endl;
}
}
}
return 0;
}
Your output is in the wrong place, it should be inside the test case loop not after it. You also need to print a newline after every result
Your code is like this
for(j=1;j<=t;j++)
{
...
}
if(sum>d)
{
cout<<d;
}
else
cout<<sum;
but it should be like this
for(j=1;j<=t;j++)
{
...
if(sum>d)
cout<<d<<'\n';
else
cout<<sum<<'\n';
}
Mistakes like this are much easier to spot if you get into the habit of indenting your code consistently.
Another error is that you don't reset sum to zero for each test case. Your loop should also look like this
for(j=1;j<=t;j++)
{
sum=0;
...
}
Some sloppy mistakes. To be a programmer you really have to pay attention to the details.
For free, here's another tip. You don't need to use an array for this problem. To add up numbers you don't need to put them all in an array first and then add them up later. You can just add them up as you input them.
int sum = 0;
for (int i = 0; i < N; i++)
{
int num;
cin >> num;
sum += num;
}

How to count odd or even numbers between particular elements? [closed]

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.

How can this code be improved to avoid timeout with large numbers of data entries? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My solution to a HackerRank problem works but times out when very large amounts of data are used, such as the following: https://hr-testcases-us-east-1.s3.amazonaws.com/9403/input11.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1565703339&Signature=JcuoWT7wKxpU3GWudO4wLNWK6Dg%3D&response-content-type=text%2Fplain
I'm quite aware that the code is far from ideal, and will probably make experienced software developers cringe... so I'm hoping it can be improved.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int N;
cin >> N;
vector<int> v;
vector<string> s;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
v.push_back(a);
}
int Q;
cin >> Q;
for (int i = 0; i < Q; i++) {
int a;
int b = 0;
cin >> a;
for (int j = 0; j < v.size(); j++) {
if(v[j]==a) {
s.push_back("Yes " + to_string(j+1));
b++;
j=v.size();
}
}
if (b==0) {
vector<int>::iterator low;
low = std::lower_bound(v.begin(), v.end(), a);
int d = low-v.begin();
d++;
s.push_back("No " + to_string(d));
}
}
for (int i = 0; i < s.size(); i++) {
cout << s[i] <<"\n";
}
return 0;
}
The problem statement is:
Ideally, I'd rather not have a completely new solution, but rather get some help making this one better.
First of all, it is always useful to analyze the performance of a software by profiling it with the adeguate tool, see here.
With just taking a look at is, there are a few points which you could optimize:
the s list is useless. You push back data on it and then print in order. Just print directly. You would save memory and the last for loop.
inside your second loop you first perform a linear search and then, if nothing is found, you use std::lower_bound which has logarithmic complexity. You could reduce the time by just looping once and looking for the element or the smallest one at the same time, reducing the amount of looping you need. You can also take advantage of the fact that the N integers are sorted to stop the search earlier.

Logic to return continuous numbers lets say array with elements 1,9,2,3,4,9,6,7,8 then it should return 2,3,4,6,7,8? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My code is below but there is some error can anyone guide me to write logic to return continuous numbers, for example, if array[] = {1,3,5,2,3,4,7,4,5,6} then function should return 2,3,4,4,5,6 keep time complexity in mind?
#include <stdio.h>
#define max 10
int coll[max];
void call_sort(int* p) {
int i = 0, first, sec;
while (*p) {
first = *p;
p++;
sec = *p - 1;
if (first == sec) {
coll[i] = *p;
i++;
}
int j;
for (j = 0; j < max; j++) {
printf("%d ", coll[j]);
//coll=coll+1;
}
}
}
int main(void) {
printf("ya\n");
int buff[max], i;
for (i = 0; i < max; i++)
scanf("%d", buff[i]);
call_sort(&buff);
}
There are many problems with your code
Wrong parameter to scanf(), you need to pass the address of the variable to modify it inside scanf() like this
scanf("%d", &buff[i]);
and you should also check that scanf() did read the value correctly by checking it's return value.
Wrong parameter to call_sort(), this will not cause any problem in this case, but it's wrong, and this combined with the scanf() issue, means that you did not enable compiler warnings, you should.
The correct way to pass buff is simply
call_sort(buff);
Wrong while (*p) which assumes that 0 is the last element of the array.
You should probably pass the size as a parameter and write a for loop, since you are dealing with numbers and 0 is a number, if it were a text string then it would be ok to exclude 0 from the normal values and use it as a sentinel value which is done in the standard library functions for strings.