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]);
}









share|improve this question




















  • 1




    Can 0 be a factor?
    – P.W
    Nov 7 at 9:13















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]);
}









share|improve this question




















  • 1




    Can 0 be a factor?
    – P.W
    Nov 7 at 9:13













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]);
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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).






share|improve this answer





















  • 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 to j, not including j, i.e. for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }.
    – unwind
    Nov 7 at 10:11


















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]);





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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).






share|improve this answer





















  • 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 to j, not including j, i.e. for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }.
    – unwind
    Nov 7 at 10:11















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).






share|improve this answer





















  • 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 to j, not including j, i.e. for (int k = 0; k < n; ++k) { printf("%dn", a[k]); }.
    – unwind
    Nov 7 at 10:11













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).






share|improve this answer












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).







share|improve this answer












share|improve this answer



share|improve this answer










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 to j, not including j, 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










  • @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 to j, not including j, 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












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]);





share|improve this answer





















  • 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















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]);





share|improve this answer





















  • 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













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]);





share|improve this answer












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]);






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()