C++ unclear output [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to find all the divisors of a number (n) and to add to the array those divisors that are at the 1st power (that appear only once), but I get in output just zeros, what's wrong with my code?
#include<iostream>
using namespace std;
int k,A[100000],n,p,d=2,pozitia=0;
int main()
{
cin>>n;
while(n>1)
{
p=0;
while(n%d==0)
{
p=p+1;
n=n/d;
}
if (p==1) { A[pozitia]=d; pozitia++; }
d=d+1;
}
for (int i=0;i<=pozitia;i++) cout<<A[pozitia]<<" ";
return 0;
}

You print always the same value:
for (int i=0;i<=pozitia;i++)
cout<<A[pozitia]<<" ";
It should be
for (int i=0;i<pozitia;i++)
cout<<A[i]<<" ";
Also pay attention that it should be i<pozitia and not i<=pozitia because you increment pozitia each time you insert a new value so at the end pozitia will point to a not initialized value in A.

I couldn't follow your logic for computing the divisors. It seems to be much simpler than you make it out to be.
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
Here's a program that uses that logic.
#include<iostream>
using namespace std;
void printDivisors(int A[], int pozitia)
{
for (int i=0;i<pozitia;i++) cout<<A[i]<<" ";
}
void fun(int n)
{
int A[100000];
int d = 2;
int pozitia=0;
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
printDivisors(A, pozitia);
}
int main()
{
int n;
cin>>n;
fun(n);
return 0;
}
The output for input of 100:
2 4 5 10 20 25 50

Related

How does change in Passing parameters changes the output of the Code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When I pass only Sum as Parameter in My coin Exchange Problem to find no of ways to find the amount Sum..
Example
if the coins are {2,3,5} and the desired sum is 9 i get correct output as 8.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum) {
int S = 0;
if(sum == 0) return 1;
if(sum < 0) return 0;
for(int i=1;i<=n;i++) {
S += fun(sum - a[i]);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum);
return 0;
}
But when I also give Current Index as a parameter it gives me output as 3 which is wrong.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum, int idx) {
int S = 0;
if(sum == 0) return 1;
if(idx > n) return 0;
if(sum < 0) return 0;
for(int i=idx;i<=n;i++) {
S += fun(sum - a[i], i);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum,1);
return 0;
}
But WHY??
Is my parameter passing in this case is wrong?
You don't have the same result between your 2 solutions because in the first, at each "function call", you will iterate from 1 to N and in your second solution, you will iterate from Index to N. So the number of loop will be different.

newbie ask about decimal to binary in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
this program suppose to convert decimal to binary but somehow i screw it up
can some one point out the error for me?
thanks a lot
#include<conio.h>
#include<stdio.h>
int main(){
int a;
int b[20];
int q = 0;
printf("decimal : ");scanf("%d",&a);
while(a>0)) {
b[q]=a%2;
a=a/2;
q++;
}while(a>0);
printf("binary : ");
for (int i = q-1; i>=0;i--){
printf("%d",b[q]);
}
}
Corrected code is:
#include<conio.h>
#include<stdio.h>
int main(){
int a;
int b[20];
int q = 0;
printf("decimal : ");scanf("%d",&a);
while(a>0) {
b[q]=a%2;
a=a/2;
q++;
}
printf("binary : ");
for (int i = q-1; i>=0;i--){
printf("%d",b[i]);
}
}
You were printing b[q] instead of b[i]
There are some problems with your code:
you added an extra ")" of the first while;
the second 'while' is useless (the code is being repeated due to the first one)
you are not printing the elements you want (you should use var 'i'), what you are really printing is the value after the last 0/1 (because you are using 'q')
code should look like this:
#include <conio.h>
#include <stdio.h>
int main() {
int a;
int b[20];
int q = 0;
printf("decimal: ");
scanf("%d", &a);
while (a > 0) {
b[q] = a % 2;
a = a / 2;
q++;
}
printf("binary: ");
for (int i = q - 1; i >= 0; i--) {
printf("%d", b[i]);
}
}

Skipped condition and I don't know why [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 6 years ago.
Improve this question
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.

C++ program calculates n times n for the numbers 1 to 9 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have to write a program that calculates n times n for the numbers between 1 and 9 using a function.
The output should be like 1, 4, 27, 256...
I can feel I'm very close to finishing it but I just can't figure out what the problem is, here is the code I wrote:
#include <iostream>
using namespace std;
int result, number, n;
void function1()
{
result = number;
for (int x = 1; x < number; x++)
{
result = number*result;
}
}
int main()
{
for (n = 1; n < 10; n++)
{
function1();
cout << result << endl;
system("pause");
return 0;
}
}
Try this :-
#include <iostream>
#include <math>
using namespace std;
int result, number, n;
void function1(int number)
{
int result;
result = pow(number,number);
cout<<result;
}
int main()
{
for (n = 1; n < 10; n++)
{
function1(n);
system("pause");
}
return 0;
}

division of array on some criterion [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
in this code i try to divide the a[5] into two arrays on the criterion of stored data in the array .......
and store the index of the array a[5] to other arrays to show these index contain different data elements
but i doesn't work for me
#include <iostream>
using namespace std;
void printarray(int b[], int count)
{
int i;
for(i=0;i<count;i++)
cout<<b[i]<<endl;
}
void main()
{
int a[5]={1,0,0,1,1};
int array[5][5]=
{
{0,5,0,4,0},
{0,0,6,0,7},
{0,0,0,0,8},
{0,0,0,0,10},
{0,0,0,0,0}
};
int count=0;
int counti=0;
int C1=0;
int i;
for(i=0;i<5;i++)
{
if(C1==a[i])
{
count++;
}
else
{
counti++;
}
}
int *b= new int[count];
int *c= new int[counti];
for(i=0;i<5;i++)
{
if(C1==a[i])
{
b[i]=i;
}
else
{
c[i]=i;
}
}
printarray(b,count);
}
the code display the grabage values... plz help me
its show the following result
-842151450
1
The first i was 1 , so b will contain {1, 2}. where ( b[1] = 1, b[2] = 2 )
when you loop through b to print all elements you start from index 0 although b started from index 1.
you can fix index using (j, k instead of i).
int *b = new int[count];
int *c = new int[counti];
int j, k;
j = k = 0;
for (i = 0; i<5; i++)
{
if (C1 == a[i])
{
b[j++] = i;
}
else
{
c[k++] = i;
}
}
printarray(b, count);