Need help to store integer values from a loop into an array
up vote
-4
down vote
favorite
I want to list all the factors of a number into an array. So, here's my try at it.
#include<stdio.h>
#define n1 10
int main()
{
int n,i,j,a[n1];
printf("Enter num: ");
scanf("%d",&num);
for(i=1;i<n;i++){
if(n%i==0){
for(j=0;j<n;j++)
I don't know how to proceed from here. I'm confused.
#include<stdio.h>
#define n1 10
int main()
{
int n,i,j,a[n1];
printf("Enter number : ");
scanf("%d",&n);
j = 0;
for(i=1;i<n;i++){
if(n%i==0)
a[j++] = i;
}
printf("The factors of the number are: ");
for(j=0;j;j++)
printf("%dn",a[j]);
}
c
add a comment |
up vote
-4
down vote
favorite
I want to list all the factors of a number into an array. So, here's my try at it.
#include<stdio.h>
#define n1 10
int main()
{
int n,i,j,a[n1];
printf("Enter num: ");
scanf("%d",&num);
for(i=1;i<n;i++){
if(n%i==0){
for(j=0;j<n;j++)
I don't know how to proceed from here. I'm confused.
#include<stdio.h>
#define n1 10
int main()
{
int n,i,j,a[n1];
printf("Enter number : ");
scanf("%d",&n);
j = 0;
for(i=1;i<n;i++){
if(n%i==0)
a[j++] = i;
}
printf("The factors of the number are: ");
for(j=0;j;j++)
printf("%dn",a[j]);
}
c
1
Can 0 be a factor?
– P.W
Nov 7 at 9:13
add a comment |
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
I want to list all the factors of a number into an array. So, here's my try at it.
#include<stdio.h>
#define n1 10
int main()
{
int n,i,j,a[n1];
printf("Enter num: ");
scanf("%d",&num);
for(i=1;i<n;i++){
if(n%i==0){
for(j=0;j<n;j++)
I don't know how to proceed from here. I'm confused.
#include<stdio.h>
#define n1 10
int main()
{
int n,i,j,a[n1];
printf("Enter number : ");
scanf("%d",&n);
j = 0;
for(i=1;i<n;i++){
if(n%i==0)
a[j++] = i;
}
printf("The factors of the number are: ");
for(j=0;j;j++)
printf("%dn",a[j]);
}
c
I want to list all the factors of a number into an array. So, here's my try at it.
#include<stdio.h>
#define n1 10
int main()
{
int n,i,j,a[n1];
printf("Enter num: ");
scanf("%d",&num);
for(i=1;i<n;i++){
if(n%i==0){
for(j=0;j<n;j++)
I don't know how to proceed from here. I'm confused.
#include<stdio.h>
#define n1 10
int main()
{
int n,i,j,a[n1];
printf("Enter number : ");
scanf("%d",&n);
j = 0;
for(i=1;i<n;i++){
if(n%i==0)
a[j++] = i;
}
printf("The factors of the number are: ");
for(j=0;j;j++)
printf("%dn",a[j]);
}
c
c
edited Nov 7 at 9:50
asked Nov 7 at 9:11
Kohila
33
33
1
Can 0 be a factor?
– P.W
Nov 7 at 9:13
add a comment |
1
Can 0 be a factor?
– P.W
Nov 7 at 9:13
1
1
Can 0 be a factor?
– P.W
Nov 7 at 9:13
Can 0 be a factor?
– P.W
Nov 7 at 9:13
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You are quite close, but the inner loop seems pointless. Once you've found a factor, just store it and keep track of how many you've found:
int factors(int n, int *factors)
{
int j = 0;
for (int i = 2; i < n; ++i)
{
if (n % i == 0)
{
factors[j++] = i;
}
}
return j;
}
I ran this on 471113
, and got [193, 2441]
which checks out.
Do note that this is quite limited by the max precision of int
(and should use unsigned long
and be safer for real code).
I didn't know that we can use increment operator as a subscript of an array. Thanks.
– Kohila
Nov 7 at 9:24
@Kohila The index needs to be an integer expression, and you can use any operator in any expression, being an array index is not a special case.
– unwind
Nov 7 at 9:26
Check the second snippet of code @unwind Now, I have a problem. I'm trying to print and show the factors but I cannot come up with the good condition for the second for loop. If I give j<n as condition, then some garbage values are being printed.
– Kohila
Nov 7 at 9:48
@Kohila You should just use a loop from 0 toj
, not includingj
, i.e.for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }
.
– unwind
Nov 7 at 10:11
add a comment |
up vote
0
down vote
I think what you are looking for is:
for(i=0, j=0;i<n;i++)
if(n%i==0)
a[j++] = i;
// print it
for (i=0; i<j; i++)
printf("%d ",a[i]);
Got it. Thanks.
– Kohila
Nov 7 at 9:24
I don't think it's a good idea to include 0 and 1 in the outer loop.
– unwind
Nov 7 at 9:26
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You are quite close, but the inner loop seems pointless. Once you've found a factor, just store it and keep track of how many you've found:
int factors(int n, int *factors)
{
int j = 0;
for (int i = 2; i < n; ++i)
{
if (n % i == 0)
{
factors[j++] = i;
}
}
return j;
}
I ran this on 471113
, and got [193, 2441]
which checks out.
Do note that this is quite limited by the max precision of int
(and should use unsigned long
and be safer for real code).
I didn't know that we can use increment operator as a subscript of an array. Thanks.
– Kohila
Nov 7 at 9:24
@Kohila The index needs to be an integer expression, and you can use any operator in any expression, being an array index is not a special case.
– unwind
Nov 7 at 9:26
Check the second snippet of code @unwind Now, I have a problem. I'm trying to print and show the factors but I cannot come up with the good condition for the second for loop. If I give j<n as condition, then some garbage values are being printed.
– Kohila
Nov 7 at 9:48
@Kohila You should just use a loop from 0 toj
, not includingj
, i.e.for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }
.
– unwind
Nov 7 at 10:11
add a comment |
up vote
0
down vote
accepted
You are quite close, but the inner loop seems pointless. Once you've found a factor, just store it and keep track of how many you've found:
int factors(int n, int *factors)
{
int j = 0;
for (int i = 2; i < n; ++i)
{
if (n % i == 0)
{
factors[j++] = i;
}
}
return j;
}
I ran this on 471113
, and got [193, 2441]
which checks out.
Do note that this is quite limited by the max precision of int
(and should use unsigned long
and be safer for real code).
I didn't know that we can use increment operator as a subscript of an array. Thanks.
– Kohila
Nov 7 at 9:24
@Kohila The index needs to be an integer expression, and you can use any operator in any expression, being an array index is not a special case.
– unwind
Nov 7 at 9:26
Check the second snippet of code @unwind Now, I have a problem. I'm trying to print and show the factors but I cannot come up with the good condition for the second for loop. If I give j<n as condition, then some garbage values are being printed.
– Kohila
Nov 7 at 9:48
@Kohila You should just use a loop from 0 toj
, not includingj
, i.e.for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }
.
– unwind
Nov 7 at 10:11
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You are quite close, but the inner loop seems pointless. Once you've found a factor, just store it and keep track of how many you've found:
int factors(int n, int *factors)
{
int j = 0;
for (int i = 2; i < n; ++i)
{
if (n % i == 0)
{
factors[j++] = i;
}
}
return j;
}
I ran this on 471113
, and got [193, 2441]
which checks out.
Do note that this is quite limited by the max precision of int
(and should use unsigned long
and be safer for real code).
You are quite close, but the inner loop seems pointless. Once you've found a factor, just store it and keep track of how many you've found:
int factors(int n, int *factors)
{
int j = 0;
for (int i = 2; i < n; ++i)
{
if (n % i == 0)
{
factors[j++] = i;
}
}
return j;
}
I ran this on 471113
, and got [193, 2441]
which checks out.
Do note that this is quite limited by the max precision of int
(and should use unsigned long
and be safer for real code).
answered Nov 7 at 9:16
unwind
316k52392526
316k52392526
I didn't know that we can use increment operator as a subscript of an array. Thanks.
– Kohila
Nov 7 at 9:24
@Kohila The index needs to be an integer expression, and you can use any operator in any expression, being an array index is not a special case.
– unwind
Nov 7 at 9:26
Check the second snippet of code @unwind Now, I have a problem. I'm trying to print and show the factors but I cannot come up with the good condition for the second for loop. If I give j<n as condition, then some garbage values are being printed.
– Kohila
Nov 7 at 9:48
@Kohila You should just use a loop from 0 toj
, not includingj
, i.e.for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }
.
– unwind
Nov 7 at 10:11
add a comment |
I didn't know that we can use increment operator as a subscript of an array. Thanks.
– Kohila
Nov 7 at 9:24
@Kohila The index needs to be an integer expression, and you can use any operator in any expression, being an array index is not a special case.
– unwind
Nov 7 at 9:26
Check the second snippet of code @unwind Now, I have a problem. I'm trying to print and show the factors but I cannot come up with the good condition for the second for loop. If I give j<n as condition, then some garbage values are being printed.
– Kohila
Nov 7 at 9:48
@Kohila You should just use a loop from 0 toj
, not includingj
, i.e.for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }
.
– unwind
Nov 7 at 10:11
I didn't know that we can use increment operator as a subscript of an array. Thanks.
– Kohila
Nov 7 at 9:24
I didn't know that we can use increment operator as a subscript of an array. Thanks.
– Kohila
Nov 7 at 9:24
@Kohila The index needs to be an integer expression, and you can use any operator in any expression, being an array index is not a special case.
– unwind
Nov 7 at 9:26
@Kohila The index needs to be an integer expression, and you can use any operator in any expression, being an array index is not a special case.
– unwind
Nov 7 at 9:26
Check the second snippet of code @unwind Now, I have a problem. I'm trying to print and show the factors but I cannot come up with the good condition for the second for loop. If I give j<n as condition, then some garbage values are being printed.
– Kohila
Nov 7 at 9:48
Check the second snippet of code @unwind Now, I have a problem. I'm trying to print and show the factors but I cannot come up with the good condition for the second for loop. If I give j<n as condition, then some garbage values are being printed.
– Kohila
Nov 7 at 9:48
@Kohila You should just use a loop from 0 to
j
, not including j
, i.e. for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }
.– unwind
Nov 7 at 10:11
@Kohila You should just use a loop from 0 to
j
, not including j
, i.e. for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }
.– unwind
Nov 7 at 10:11
add a comment |
up vote
0
down vote
I think what you are looking for is:
for(i=0, j=0;i<n;i++)
if(n%i==0)
a[j++] = i;
// print it
for (i=0; i<j; i++)
printf("%d ",a[i]);
Got it. Thanks.
– Kohila
Nov 7 at 9:24
I don't think it's a good idea to include 0 and 1 in the outer loop.
– unwind
Nov 7 at 9:26
add a comment |
up vote
0
down vote
I think what you are looking for is:
for(i=0, j=0;i<n;i++)
if(n%i==0)
a[j++] = i;
// print it
for (i=0; i<j; i++)
printf("%d ",a[i]);
Got it. Thanks.
– Kohila
Nov 7 at 9:24
I don't think it's a good idea to include 0 and 1 in the outer loop.
– unwind
Nov 7 at 9:26
add a comment |
up vote
0
down vote
up vote
0
down vote
I think what you are looking for is:
for(i=0, j=0;i<n;i++)
if(n%i==0)
a[j++] = i;
// print it
for (i=0; i<j; i++)
printf("%d ",a[i]);
I think what you are looking for is:
for(i=0, j=0;i<n;i++)
if(n%i==0)
a[j++] = i;
// print it
for (i=0; i<j; i++)
printf("%d ",a[i]);
answered Nov 7 at 9:15
Paul Ogilvie
16.5k11134
16.5k11134
Got it. Thanks.
– Kohila
Nov 7 at 9:24
I don't think it's a good idea to include 0 and 1 in the outer loop.
– unwind
Nov 7 at 9:26
add a comment |
Got it. Thanks.
– Kohila
Nov 7 at 9:24
I don't think it's a good idea to include 0 and 1 in the outer loop.
– unwind
Nov 7 at 9:26
Got it. Thanks.
– Kohila
Nov 7 at 9:24
Got it. Thanks.
– Kohila
Nov 7 at 9:24
I don't think it's a good idea to include 0 and 1 in the outer loop.
– unwind
Nov 7 at 9:26
I don't think it's a good idea to include 0 and 1 in the outer loop.
– unwind
Nov 7 at 9:26
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186394%2fneed-help-to-store-integer-values-from-a-loop-into-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can 0 be a factor?
– P.W
Nov 7 at 9:13