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]);
}
}
Related
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
#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3];
int i,j,k;
for(i=0;i<3;i++)
{
ar3[i][j] = 0;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++);
printf("%d\t",ar3[i][j]);
}
getch();
return 0;
}
When I compile the code in Dev C++ it does not give any error but fails to run and the application stops working. Whats wrong with it?
On the line
ar3[i][j] = 0;
j is still garbage so you get undefined behavior, which may lead to different kinds of faults - erroneous results, segfaults, on some rare cases it may even work as expected.
Move that line inside the second loop.
you should initialize arr3[i][j] to 0 as int arr3[i][j]={0};. Also while displaying the multiplication matrix you closed the second loop with j. Don't close that otherwise you won't get desired output. I corrected it. Hope this one solves your problem.
#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3] = {0}; // here goes initialization
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",ar3[i][j]);
}
printf("\n");
}
getch();
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 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
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.
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);