C++ Even Numbers Lab submission Query - c++

Hi there? Here is my code, what could be the errors because it is correct when I run it?Write a program that reads a positive integer n, and prints the first
n even numbers.
For example, one execution would look like this:
Please enter a positive integer: 3
2
4
6
File Name
evennumbers.cpp
Score
There are three tests each worth 2 points
Autograder Results
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Please enter a positive integer:";
cin>>n;
cout<<"These are all positive integers in between 1 to "<<n<<" "<<endl;
for(int i=1;i<=n;i++)
if(i%2==0)
cout<<i<<" "<<endl;
return 0;
}
Evaluating Test 1 Output (0.0/2.0)
Test Failed: 'these are all positive integers in between 1 to 3 2' != '246'
these are all positive integers in between 1 to 3 2
246
Evaluating Test 2 Output (0.0/2.0)
Test Failed: 'these are all positive integers in between 1 to 2 2' != '24'
these are all positive integers in between 1 to 2 2
24
Evaluating Test 3 Output (0.0/2.0)
Test Failed: 'these are all positive integers in between 1 to 4 2 4' != '2468'
these are all positive integers in between 1 to 4 2 4
2468

There are two problems with your code.
Your program is not doing what was asked. It is currently printing every even number between 1 and n; when the problem asks you to print all the first n even numbers. So if n is 3, you should print all the first three even numbers, that is, 2 4 6.
Considering your program is being run through an automatic checker, you should print only what is asked. So don't print these are all positives (...). Print only the even numbers you should print, given the input number. For instance, if the input is 3, print only 2 4 6. Don't print these are all positive integers between 1 to 3 2 4 6.

Related

How to count the number of permutations?

We are given array-'a' and array-'b' consisting of positive integers.
How can I count all the permutation of array 'a' which are strictly lexicographically smaller than array-'b'?
Arrays can contain as many as 10^5 integers(positive)
Example:
1 2 3 is lexicographically smaller than 3 1 2
1 2 3 is lexicographically smaller than 1 4 5.
I would like the solution to be in C++.
Input : 3
1 2 3
2 1 3
Output : 2
Only permutations 1,2,3 and 1,3,2 are lexicographically smaller than 2 1 3
Let's just tackle the algorithm. Once you get that figured out, the implementation should be pretty straightforward. Does this look like it does what you're looking for?
Pseudo code:
function get_perms(a,b)
#count the number of digits in a that are <b[0]
count = sum(a<b[0])
Nperms = (len(a)-1)! #modify this formula as needed
N = count*Nperms
if sum(a==b[0]) > 0
remove 1 b[0] from a
# repeat the process with the substring assuming a[0]==b[0]
N += sum(a==b[0])*get_perms(a,b[1:end])
return N
main()
get_perms(a,b)
Edit: I did a little searching. I believe that this is what you are looking for.

printing 1D array in 6 columns

So I encountered this problem of not knowing how to display my random numbers from an array. The display should have 6 columns and I don’t know how about the rows. It is up to the user to enter amount of numbers in the array. Then, rand() will generate the numbers and display them in 6 columns like that (IF USER ENTERED 26 NUMBERS)
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1
I know how to generate the numbers and all that, my problem is only in displaying 1d array in that format. (the output has to compatible with other numbers entered as well not only 26) Any help or pointing in the right direction would be much appreciated.
Thanks,
uszy 123345.
Since you worry about just the output, you can insert a newline character every 6 numbers printed and just stop when you get at the end of the array.
Very often in c++ programming you can make use of extra variables to accomplish something every X number of iterations in a loop. In your case it looks like you want to insert a newline after every 6 numbers. Here is an example of how you would do that in code:
//I am assuming an array called arr with 26 elements already exists
for(int x = 1, i = 0; i < 26; ++i)
{
std::cout << arr[i] << ' ';
x++;
if(x == 6)
{
std::cout << std::endl;
x = 0;
}
}

What mistake have I made in this code(C++)?

I was solving a problem on codechef:
https://www.codechef.com/NITWMA01/problems/QPALIN.It required to input m number of input cases after getting the value of m from the user. I always used to run a loop of:
while(m--)
{//input test cases}, but in this problem I don't know why the loop is running less than m times when I have to get input cases m times. I tried running the code with the sample input(with m having 6) but main() returned 0 after getting just 4 inputs(and printint respective outputs wherever necessary).
My code is as follows:
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main()
{
int n,m,op,x,l,r,i,j,xorpair=0;
char k,s[100000];
scanf("%d",&n);
scanf("%s",s);
scanf("%d",&m);
for(j=0;j<m;j++)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&x);
scanf("%c",&k);
s[x-1]=k;
}
else
{
xorpair=0;
scanf("%d%d",&l,&r);
for(i=l-1;i<r;i++)
{
xorpair^=s[i]-'0';
}
if(xorpair==0)
{
printf("YES\n");
}
else printf("NO\n");
}
}
return 0;
}
PS: I have replaced cin with scanf. Also I believe I was not able to properly convey what problem actually I am facing. So here's the test case that explains it more clearly:
(What it should be like)
Sample input:
7
abbacca
6
2 1 4
1 1 z
2 1 4
1 4 z
2 1 4
2 5 7
Sample output:
YES
NO
YES
YES
Following is the problem that I am facing:
When I run the program for the above input, this is what appears on the output screen
7
abbacca
6
2 1 4
YES
1 1 z
2 1 4
NO
1 4 z
Process returned 0 (0x0)
I am not able to enter inputs 6 times and after 4th input it returns 0.
scanf("%c" does not do what you seem to expect. It reads just the ' ' before the char you want. Then the next tries at reading op and x fail leaving op still equal to 1, so you do two of operation 1 for every once that you intended to.

Counting ways of breaking up a string of digits into numbers under 26

Given a string of digits, I wish to find the number of ways of breaking up the string into individual numbers so that each number is under 26.
For example, "8888888" can only be broken up as "8 8 8 8 8 8 8". Whereas "1234567" can be broken up as "1 2 3 4 5 6 7", "12 3 4 5 6 7" and "1 23 4 5 6 7".
I'd like both a recurrence relation for the solution, and some code that uses dynamic programming.
This is what I've got so far. It only covers the base cases which are a empty string should return 1 a string of one digit should return 1 and a string of all numbers larger than 2 should return 1.
int countPerms(vector<int> number, int currentPermCount)
{
vector< vector<int> > permsOfNumber;
vector<int> working;
int totalPerms=0, size=number.size();
bool areAllOverTwo=true, forLoop = true;
if (number.size() <=1)
{
//TODO: print out permetations
return 1;
}
for (int i = 0; i < number.size()-1; i++) //minus one here because we dont care what the last digit is if all of them before it are over 2 then there is only one way to decode them
{
if (number.at(i) <= 2)
{
areAllOverTwo = false;
}
}
if (areAllOverTwo) //if all the nubmers are over 2 then there is only one possable combination 3456676546 has only one combination.
{
permsOfNumber.push_back(number);
//TODO: write function to print out the permetions
return 1;
}
do
{
//TODO find all the peremtions here
} while (forLoop);
return totalPerms;
}
Assuming you either don't have zeros, or you disallow numbers with leading zeros), the recurrence relations are:
N(1aS) = N(S) + N(aS)
N(2aS) = N(S) + N(aS) if a < 6.
N(a) = 1
N(aS) = N(S) otherwise
Here, a refers to a single digit, and S to a number. The first line of the recurrence relation says that if your string starts with a 1, then you can either have it on its own, or join it with the next digit. The second line says that if you start with a 2 you can either have it on its own, or join it with the next digit assuming that gives a number less than 26. The third line is the termination condition: when you're down to 1 digit, the result is 1. The final line says if you haven't been able to match one of the previous rules, then the first digit can't be joined to the second, so it must stand on its own.
The recurrence relations can be implemented fairly directly as an iterative dynamic programming solution. Here's code in Python, but it's easy to translate into other languages.
def N(S):
a1, a2 = 1, 1
for i in xrange(len(S) - 2, -1, -1):
if S[i] == '1' or S[i] == '2' and S[i+1] < '6':
a1, a2 = a1 + a2, a1
else:
a1, a2 = a1, a1
return a1
print N('88888888')
print N('12345678')
Output:
1
3
An interesting observation is that N('1' * n) is the n+1'st fibonacci number:
for i in xrange(1, 20):
print i, N('1' * i)
Output:
1 1
2 2
3 3
4 5
5 8
6 13
7 21
8 34
9 55
If I understand correctly, there are only 25 possibilities. My first crack at this would be to initialize an array of 25 ints all to zero and when I find a number less than 25, set that index to 1. Then I would count up all the 1's in the array when I was finished looking at the string.
What do you mean by recurrence? If you're looking for a recursive function, you would need to find a good way to break the string of numbers down recursively. I'm not sure that's the best approach here. I would just go through digit by digit and as you said if the digit is 2 or less, then store it and test appending the next digit... i.e. 10*digit + next. I hope that helped! Good luck.
Another way to think about it is that, after the initial single digit possibility, for every sequence of contiguous possible pairs of digits (e.g., 111 or 12223) of length n we multiply the result by:
1 + sum, i=1 to floor (n/2), of (n-i) choose i
For example, with a sequence of 11111, we can have
i=1, 1 1 1 11 => 5 - 1 = 4 choose 1 (possibilities with one pair)
i=2, 1 11 11 => 5 - 2 = 3 choose 2 (possibilities with two pairs)
This seems directly related to Wikipedia's description of Fibonacci numbers' "Use in Mathematics," for example, in counting "the number of compositions of 1s and 2s that sum to a given total n" (http://en.wikipedia.org/wiki/Fibonacci_number).
Using the combinatorial method (or other fast Fibonacci's) could be suitable for strings with very long sequences.

Formula Sequence

I need help finding the formula of the sequence for the next problem.
What I think and have for now is Sn=n(10^n-1)/9 but it just works in some cases...
Here is the description of the problem:
Description
Sn is based upon the sequence positive integers numbers. The value n can be found n times, so the first 25 terms of this sequence are as follows:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7...
For this problem, you have to write a program that calculates the i-th term in the sequence. That is, determine Sn(i).
Input specification
Input may contain several test cases (but no more than 10^5). Each test case is given in a line of its own, and contains an integer i (1 <= i <= 2 * 10^9). Input ends with a test case in which i is 0, and this case must not be processed.
Output specification
For each test case in the input, you must print the value of Sn(i) in a single line.
Sample input
1
25
100
0
Sample output
1
7
14
Thanks solopilot! I made the code but the online judge show me Time Limit Exceeded, what could be my error?
#include <iostream> #include <math.h> using namespace std; int main() {int i;
int NTS;
cin>>i;
while (i>=1){
NTS=ceil((sqrt(8*i+1)-1)/2);
cout<<" "<<NTS<<endl;
cin>>i;
}
return 0;}
F(n) = ceiling((sqrt(8*n+1)-1)/2)
Say F(n) = a.
Then n ~= a * (a+1) / 2.
Rearranging: a^2 + a - 2n ~= 0.
Solving: a = F(n) = (-1 + sqrt(1+8n)) / 2.
Ignore the negative answer.
The pattern looks like a pyramid.
Level : 1 3 6 10 15 21 28...
No : 1 2 3 4 5 6 7...
Level = n(n+1)/2 => elements
3 = 3*4/2 => 6
6 = 6*7/2 => 21