include
using namespace std;
int main()
{
int i,n,a[20];
cout<<"How Many Numbers Do u want to use as input :- ";
cin>>n;cout< for( i=0;i {
cin>>a[i];
}
cout<<"\nHere your output:- ";
for( i=0;i {
if(a[i]!=42)
cout<<" " < else if(a[i]==42)
break;
}
}
i hv written the prgrm using array 1st u hv to give all the no that u want to use as input & then it gives u the output as mentioned in the qstn, so where am i wrng??
Hi Folks,
For the users that have most recently posted in this topic. Welcome!
Everything that you output will be considered for the answer, so this will cause WA.
cout << "enter the numbers" << endl;
Please use code tags when posting code (see the nice box around the code above?)
I see all of you assuming that there will be a certain number of inputs. djs1234 assumes that it will be less than or equal to 20. This is not true. The problem states that you should continue to take input until it reads 42, it does not say if there will be 5, 100, 10000000 numbers in the input.
while not done
read input
if number = 42 then done
else print number
end while
include
using namespace std;
main()
{
int a[10], n=5, c;
cin >>n;
for(c=0;c {
cin >>a[c];
}
cout <<"the output is :- "<<"\n";
for(c=0;c {
if(a[c]!=42)
{
cout << a[c]<< '\n';
}
else
{
return 0;
}
};
return 0;
}
what the hell is wrong !!!
why it has not been accepted ... it is succesfully running in my compiler
Welcome! Please use code tags when posting code.
When you submit code to the judge, EVERYTHING that you output is considered for the answer. This line of code alone will fail your solution:
cout <<"the output is :- "<<"\n";
Once you have solved that you will then likely get a Runtime Error (SIGSEGV).
Nowhere in the problem statement does it say how many numbers there are in the input. There might be 2, there might be 100, there might be 1000000000. Because this is unknown you cannot store the input in memory. Also, the first number in the input is not the number of inputs to follow (n).
The information above makes this incorrect.
int a[10], n=5, c;
cin >>n;
for(c=0;c<n;c++)
{
cin >>a[c];
}
Please post back if you still have questions.
include
using namespace std;
main()
{
int a[10], n=5, c;
for(c=0;c {
cin >>a[c];
}
for(c=0;c {
if(a[c]!=42)
{
cout << a[c]<< '\n';
}
else
{
return 0;
}
};
return 0;
}
even if i made it to stop after reading 42 ... i'm unable to remove 'n' or the limit of for loop ... it needs an upper limit to be defined. for(c=0; ???;c++)
While Delshire's code does work and gets AC, it has one interesting quirk.
while(a <= 100)
It is given in the problem statement that the numbers "are integers of one or two digits" so this works fine. It would be more acceptable to see:
while(true)
or, since it is given that there is always a 42 in the input statement:
while(a != 42)
This last segment also removes the need for the extra break, giving the loop a single exit point.
#include <iostream>
using namespace std;
int main() {
int a = 0;
while(a != 42) {
cin >> a;
if(a != 42)
cout << a << endl;
}
}
Hello! Thanks for the quick reply, i brought another piece of code, a little more 'formal' i think:
#include <iostream>
using namespace std;
void displayOutput(int myArray[], int qOfNumbers);
int main()
{
int inputValues[100] = {};
for(int i = 0; inputValues[i] < 100; i++)
{
int holder;
cin >> holder;
inputValues[i] = holder;
if (inputValues[i] == 42){
displayOutput(inputValues, i);
break;
}
}
}
void displayOutput(int myArray[], int qOfNumbers)
{
for(int a = 0; a < qOfNumbers; a++)
{
cout << myArray[a] << "\n";
}
}