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
Here is my code. I think the logic is right, though it is really complicated and messy and inefficient. But when I compile it, it comes out with funny answers
Can anyone help me to find where the bug is ?
#include<stdio.h>
#include<conio.h>
int main(void)
{
int counter = 0;
int i, j;
int c;
int str[100];
int con;
for (i = 1, c = 0; i <= 100; c++, i++)
{
j = 1;
con = 0;
while (i%j == 0)
{
if (j>i)
{
break;
}
printf("i=%d j=%d\n", i, j);
j++;
con++;
printf("The number of con is %d\n", con);
}
if (con <= 2)
{
str[c] = i;
counter++;
}
}
putchar('\n');
printf("The value of counter is %d\n", counter);
for (i = 0; i<counter; i++)
{
printf("%d\n", str[i]);
}
_getch();
return 0;
}
Instead of
str[c] = i;
you want to have
str[counter] = i;
and drop the c variable altogether.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
Im trying to create a program that does various math operations, and i wanted to start with calculating prime numbers within a given range. However, when i try to execute the code, it just returns exit status -1. What is wrong with the program and how do i fix it?
#include <iostream>
#include <vector>
using namespace std;
void getPrimes(int min, int max) {
int range = max - min;
std::vector< int > possible_values;
for (int q = 0; q < range; q++) {
possible_values.push_back(min + q);
}
for (int i = 0; i < range; i++) {
int num_of_factors = 0;
int num = possible_values.at(i);
for (int c = 0; c < num; c++) {
if (num % c == 0) {
num_of_factors++;
}
}
if (num_of_factors == 0) {
std::cout << num << endl;
}
}
}
int main() {
int min, max;
std::cout << "min: ";
std::cin >> min;
std::cout << "max: ";
std::cin >> max;
getPrimes(min, max);
}
this loop should start from 2 because :
c%0 is undefined behavior
every number %1 is 0 so you can see why num_of_factors is never 0
for (int c = 2; c < num; c++) {
if (num % c == 0) {
num_of_factors++;
}
}
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]);
}
}
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
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;
}
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);