Edit: Corrected Code (Thanks for the answers and help!)
#include <iostream>
using namespace std;
int arr1(const int n,int i, int j){
if(j != 0) {
/* if(i == 2*n ){
cout<<"\n";
--j;}*/
if((i <= (n-j) || (i >= (j+n)) && i <2*n)){
cout<<" ";
}
if(i < n && i > (n-j)){
cout<<"\\";
}
if( i > n && i < (n+j)){
cout<<"/";
}
if(i == n){
cout<<"v";
}
if(i == 2*n ){
cout<<"\n";
i = 0;
--j;}
return arr1(n,++i,j);
}
return 0;
}
int main(){
int c;
cin>>c;
arr1(c,1,c);
}
I'm trying to write a program that takes an integer n and recursively prints an arrowhead type design
n=1 -> v
n=2 -> \v/
v
n=3 -> \\v//
\v/
v
etc:
This is my code so far, yet I keep getting a segmentation error. I assume this because of an infinite loop somewhere in the code.
#include <iostream>
using namespace std;
int arr1(const int n, int i, int j)
{
if (j != 0)
{
if (i == 2 * n)
{
cout << "\n";
--j;
}
if (i <= n - j || i >= j + n)
{
cout << "_";
}
if (i < j)
{
cout << "\\";
}
if (i > j)
{
cout << "/";
}
if (i == n)
{
cout << "v";
}
return arr1(n, ++i, j);
}
return 0;
}
int main()
{
int c;
cin >> c;
arr1(c, 1, c);
return 0;
}
The decision of whether to recurse comes down to the value of j -- but you receive j from main, and never modify it afterwards unless i==2*n, passing the exact same value when it calls itself recursively. So yes, that leads to infinite recursion (assuming you originally pass a non-zero value for j, anyway).
You logic in the arr1 function is wrong, for example, if c=3 in the beginning,
the i, j, n values in arr1 will be in each iteration
i = 1 j = 3 n = 3
i = 2 j = 3 n = 3
i = 3 j = 3 n = 3
i = 4 j = 3 n = 3
i = 5 j = 3 n = 3
i = 6 j = 2 n = 3
i = 7 j = 2 n = 3
i = 8 j = 2 n = 3
i = 9 j = 2 n = 3
i = 10 j = 2 n = 3
i = 11 j = 2 n = 3
...
then it goes for ever until stack overflow.
Related
I want to find the list of prime numbers, I try this code but it doesn't show me anything:
#include <iostream>
using namespace std;
int main()
{
int n, i;
std::cout << "Liste des nombres premiers : " << std::endl;
for (n = 1; n < 100; n++) {
for (i = 2; i < n; i++) {
if (n % i == 0)
std::cout << n << " ";
}
}
return 0;
}
I don't like your code at all. Try to do this: create a function that tells you if a number is prime, and then go with a for loop through all numbers from 1 to 100, and check with the function if they are prime.
Here is an easy to remember function (for natural numbers):
bool isPrime(int n)
{
if (n <= 1)
return 0;
if (n == 2)
return 1;
if (n%2 == 0)
return 0;
for (int i=3; i*i <= n; i+=2)
if (n%i == 0)
return 0;
return 1;
}
This function returns 1 if the number is prime, and returns 0 if it isn't.
The idea is this:
If n is 0 or 1, it is not prime.
If n is 2, it is prime.
If n is a multiple of 2, but it isn't 2, it is not prime.
Then, you have to check if n has any divisors until you get to square root of n (i * i <= n)
To correct program errors:
Initialize the value of n as 2.
And print when i is equal to n not when n % i is 0. It should be rather n % i == 0 then break.
Change i < n to i <= n.
Try the first approach (used sqrt()):
#include <iostream>
#include <cmath>
int main(void)
{
for (int i = 2; i < 100; i++) {
if (i == 2 || i == 3) { // if number is 2 or 3, then prints it
std::cout << i << ' ';
}
for (int j = 2; j * j <= i; j++)
{
if (i % j == 0) break;
else if (j + 1 > sqrt(i)) std::cout << i << ' ';
}
}
return 0;
}
Alternative approach:
#include <iostream>
int main(void)
{
for (int n = 2; n < 100; n++)
for (int i = 2; i <= n; i++)
if (i == n) std::cout << i << ' ';
else if (n % i == 0) break;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n, t;
cin >> n;
int i;
for(i = 0; i < n; i++){
cin >> t;
int arr[200];
arr[0] = 1;
int j;
for(j = 1; j < 200; j++) arr[j] = 0;
int l = 1, k;
for(j = 1; j <= t; j++){
int rem = 0, flag = 0;
for(k = 0; k < l; k++){
int temp = (arr[k]*j) ;
arr[k] = (temp + rem) % 10;
rem = (temp+rem) / 10;
if(k == l-1 && rem != 0){
arr[l] = rem;
flag = 1;
}
}
if(flag) l++;
}
while(l--){
cout << arr[l];
}
if(i != n-1){
cout << "\n";
}
}
return 0;
}
Question statement:
You are asked to calculate the factorials of some small positive integers.
Input:
An integer n, 1<=n<=100, denoting the number of testcases, followed by n lines, each containing a single integer t, 1<=t<=100.
Output:
For each integer n given at input, display a line with the value of t!
This is working fine for t < 35 but starts giving error for t >= 35.
Also tell me how can I improve my coding style. I am new to coding.
CASE 1
sample input:
2
1
35
actual output:
1
-40427027-3-786144929666651337523200000000
expected output:
1
10333147966386144929666651337523200000000
CASE 2
sample input:
3
5
6
7
actual output:
120
720
5040
expected output:
120
720
5040
PS Sorry!, Initial question changed as I ignored floating point errors while calculating 17! from scientific calculator. Now, code is not working for values greater than 34
Error was in part that rem can be a 3 digit number so diving by 10 doesn't work. Need to take care for rem > 100
That part of your code looks wrong since it cycles only up to length of your number
so it may grow only by one digit when there is flag:
for(k = 0; k < l; k++){
int temp = (arr[k]*j) ;
arr[k] = (temp + rem) % 10;
rem = (temp+rem) / 10;
if(k == l-1 && rem != 0){
arr[l] = rem;
flag = 1;
}
}
if(flag) l++;
It should be something shorter like:
for(k = 0; k < l; k++) {
rem += arr[k] * j;
arr[k] = rem % 10;
rem /= 10;
if(k == l-1 && rem != 0) ++l;
}
I created an algorithm to solve the following problem statement:
The problem statement is :
We say that two integers x and y have a variation of at least K, if |x − y| ≥ K (the absolute value of their difference is at least K). Given a sequence of N integers a1,a2,...,aN and K, the total variation count is the number of pairs of elements in the sequence with variation at least K, i.e. it is the size of the set of pairs
{(i,j)|1≤i<j≤N and|ai−aj|≥K}
code:
int n, k, count = 0;
cin >> n >> k;
int v[n];
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(v, v + n);
for (int i = 0, j = 1; i < n;) {
if (abs(v[i] - v[j]) >= k) {
count += (n-j);
i++;
}
else
j++;
}
cout << count;
return 0; }
If we have the input:
4 1 // first int is the number of N integers, the second int is the k.
3 1 3 2 // first integers.
So my problem is the following: If I want to improve the algorithm by adding j++ under the i++ in the if block, the output changes from 5 (the correct one) to 4. When i test this improvement on papper and it seems to work, but in code it doesn't, why?
if I trace the execution with your inputs
without the added j++ associated to the i++ :
(abs(v[0] - v[1]) >= k) = 1
count += (n-1)
(abs(v[1] - v[1]) >= k) = 0
(abs(v[1] - v[2]) >= k) = 1
count += (n-2)
(abs(v[2] - v[2]) >= k) = 0
(abs(v[2] - v[3]) >= k) = 0
(abs(v[2] - v[4]) >= k) = 1
count += (n-4)
(abs(v[3] - v[4]) >= k) = 1
count += (n-4)
result 5
if I add j++ in the block containing i++ :
(abs(v[0] - v[1]) >= k) = 1
count += (n-1)
(abs(v[1] - v[2]) >= k) = 1
count += (n-2)
(abs(v[2] - v[3]) >= k) = 0
(abs(v[2] - v[4]) >= k) = 1
count += (n-4)
(abs(v[3] - v[5]) >= k) = 1
count += (n-5)
result 4
This is not the same thing, in that case the result is not the same because of the last count += (n-5) rather than count += (n-4) because of the value of j
[edit add]
The program to test :
#include <iostream>
using namespace std;
int main(int, char **)
{
#if 0
int n, k, count = 0;
cin >> n >> k;
int v[n];
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(v, v + n);
#else
int n = 4, k = 1;
int v[] = { 1, 2, 3, 3};
int count = 0;
#endif
for (int i = 0, j = 1; i < n;) {
cout << "(abs(v[" << i << "] - v[" << j << "]) >= k) = " << (abs(v[i] - v[j]) >= k) << endl;
if (abs(v[i] - v[j]) >= k) {
cout << "\tcount += (n-" << j << ")" << endl;
count += (n-j);
j++; // THIS LINE IS IN COMMENT OR OUT OF COMMENT
i++;
}
else
j++;
}
cout << count;
return 0;
}
[/edit]
So, I have the following problem:
From the file tabl.in a number n will be read (n<=50).
After that a square array with n rows and n columns will be read; all the numbers in the array will be composed by a maximum of 2 digits each.
Shown in the file tabl.out, the modulo between the sum of numbers found on the second diagonal of the array and 10, if the sum is palindrome (true=1, false=0), and the arithmetic mean of elements situated below of the main diagonal.
Will be writing functions for:
reading the array
calculation of the operation sum of secondary diagonal%10
checking if the previous result it is palindrome
calculation of the arithmetic mean below main diagonal
Example:
tabl.in:
4
5 8 2 12
1 0 3 16
1 2 1 11
5 7 2 19
tabl.out:
2 1 3
where
(12+3+2+5)%10 = 22%10 = 2
22 is palindrome = 1
1+2+2+1+7+5 = 18, 18/6=3
My code so far is:
#include <fstream>
using namespace std;
ifstream fin("tabl.in");
ofstream fout("tabl.out");
void readn(int Arr[][51], int n) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
fin >> Arr[i][j];
}
int sumsec(int Arr[][51], int n) {
int s = 0;
float r;
for (int i = 1; i <= n; i++)
s = s + Arr[i][n - i + 1];
r = s % 10;
return r;
}
void pald(int Arr[][51], int n) {
int s = 0, pal = 0;
for (int i = 1; i < n; i++)
s = s + Arr[i][n - i + 1];
while (s != 0) {
pal = pal * 10 + s % 10;
s = s / 10;
}
if (pal == s)
fout << "1 ";
else
fout << "0 ";
}
int ambmd(int Arr[][51], int n) {
int s = 0, k;
float ame;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
s = s + Arr[i][j];
k++;
}
}
ame = s / k;
return ame;
}
int main() {
int Arr[51][51], n;
float r, ame;
fin >> n;
readn(Arr, n);
r = sumsec(Arr, n);
fout << r << " ";
pald(Arr, n);
ame = ambmd(Arr, n);
fout << ame;
}
But I have an issue with the palindrome() function: my output file will have 2 0 3 written to it for the given array from the example, instead of 2 1 3. What am I doing wrong?
Your pald function would work, if you compute s the same way as you do in sumsec and if s would still contain the sum, after you compute pal.
In your case, while (s != 0) {...}, followed by if (pal == s) {...} could be re-written as if (pal == 0), which is clearly not the intended solution. Just save your sum before computing pal, then compare with the saved sum.
Also, change your loop condition for computing s to for (int i = 1; i <= n; i++).
int s = 0, pal = 0, sum = 0;
for (int i = 1; i <= n; i++)
s = s + Arr[i][n - i + 1];
sum = s;
while (s != 0) {
pal = pal * 10 + s % 10;
s = s / 10;
}
if (pal == sum)
fout << "1 ";
else
fout << "0 ";
You should also consider the various comments for code improvements, like not re-computing the sum in the pald function.
I was assigned a "project" where I should create a C++ program to find the largest possible sum of two sub sequences. The user inputs N (array length), K (length of each sub sequence) and N numbers representing the array. The two sub sequences can touch but can't override each other e.g. 1 5 20 20 20 15 10 1 1 1 should output 90 ((5+20+20)+(20+15_10)) and not 115 ((20+20+20)+(20+20+15)).
My code until now is:
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int N, K, MaxN;
cin >> N;
cin >> K;
int Pi[N];
MaxN = N - K + 1;
int Word[MaxN];
int MaxSum;
for(int nn=0; nn<N; nn++) {
cin >> Pi[nn];
}
for(int y=0;y<MaxN;y++) {
Word[y] = 0;
}
for(int j=0; j<MaxN; j++) {
for(int l=0; l<K; l++) {
Word[j] = Word[j] + Pi[j+l];
}
}
sort(Word, Word + MaxN);
MaxSum = Word[MaxN-2] + Word[MaxN-1];
cout << MaxSum;
return 0;
}
Which is correct only in the case where the 2 sub sequences don't interfere with each other e.g. in an array such as 2 4 15 12 10 1 1 20 4 10 which outputs 71.
Thank you all in advance.
This is solution:
precalculate prefixes and suffixes
iterate end of the first subarray
iterate begin of the second subarray, but start from the end of first sub. ar. + 1
we have sum of numbers on interval from 0 to *end* = prefix[end], but we are interested only in interval [end - k, k], so simply subtract prefix[end] - prefix[end - k - 1]
[0 .. end-k-1, end-k .. end]
The same approach for the second subarray: sum2 = suffix[begin] - suffix[begin + i + 1]
then compare with the previous answer
So we just brute-force all possible sub-arrays which not intersect and find the max their sum
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int N,K,MaxN;
cin >> N;
cin >> K;
int Pi[N];
MaxN=N-K+1;
int Word[MaxN];
int MaxSum;
for(int nn=0;nn<N;nn++){
cin >> Pi[nn];
}
int prefix[N];
int sufix[N];
for (int i = 0; i < N; i++) {
prefix[i] = sufix[i] = 0;
}
for (int i = 0; i < N; i++) {
if (i == 0)
prefix[i] = Pi[i];
else
prefix[i] = Pi[i] + prefix[i - 1];
}
for (int i = N - 1; i >= 0; i--) {
if (i == N - 1)
sufix[i] = Pi[i];
else
sufix[i] = Pi[i] + sufix[i + 1];
}
int ans = 0;
for (int i = K - 1; i < MaxN; i++) {
for (int j = i + 1; j < MaxN; j++) {
int x = prefix[i] - (i - K >= 0 ? prefix[i - K] : 0);
int y = sufix[j] - (j + K < N ? sufix[j + K] : 0);
ans = max(ans, x + y);
}
}
cout << ans;
return 0;
}