C Programmers, please help with small program

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
int main (void) {
int i, j,SUM=0;
for (i=1;i<10000, i++) {
for (j=1;j<i, j++) {
if(i % j) == 0 {
SUM+= j
}
if sum == i then {
printf(" %d", SUM);
}
sum = 0;
}
return 0;
};
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
The purpose of that loop is to check each j value to modulus with i, but any more then that I am not sure what it idoes..
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: Aznguy1872
The purpose of that loop is to check each j value to modulus with i, but any more then that I am not sure what it idoes..

according to the code, there is nothing inside the brackets, so nothing gets done in that for loop. Once the loop is done, j is the value i-1, then you are checking if (i % (i-1) ==0)
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
Originally posted by: JetBlack69
Originally posted by: Aznguy1872
The purpose of that loop is to check each j value to modulus with i, but any more then that I am not sure what it idoes..

according to the code, there is nothing inside the brackets, so nothing gets done in that for loop. Once the loop is done, j is the value i-1, then you are checking if (i % (i-1) ==0)


which means I would need an if statement in the brackets for the second for loop?
 

tmarcum99

Junior Member
Nov 27, 2006
1
0
0
i would take out the sum variable and sit on ur thumb a bit. then i would fix your syntax cuz there are errors all over the damn thing......re evaulate what you want to do here.... i finished writing this prog in under 35 seconds....lets go now...
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
shell:~> gcc test.c
test.c: In function `main':
test.c:3: error: syntax error before ')' token
test.c:4: error: syntax error before ')' token
test.c: At top level:
test.c:8: error: syntax error before string constant
test.c:8: warning: conflicting types for built-in function `printf'
test.c:8: warning: data definition has no type or storage class
test.c:9: warning: data definition has no type or storage class
test.c:10: error: syntax error before '}' token
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
because I am at work Im using this ssh thing to connect to a compiler. I normally use bloodshed Dev ++, when i get home later tonight I'll be able to do more there...
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Do you know what those mean? You have the same error in line 3 and 4, they are both for loops. You should have a ; instead of a ,
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
Im sorry if I am new at this c programming, this is my first time experience with programming and I am not good with it. But I am trying my best. I am a few chapters behind because of projects and with exams coming up dont make it much easier to get caught up.
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
#include <stdio.h>

int main (void) {
int i, j,SUM=0;
for (i=1;i<10000; i++) {
for (j=1;j<i; j++) {
if(i % j) == 0 {
SUM+= j
}
if (sum == i then) {
printf("%d", SUM);
}
sum = 0;
}
return 0;
};
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: beggerking
#include <stdio.h>

int main (void) {
int i, j,SUM=0;
for (i=1;i<10000; i++) {
for (j=1;j<i; j++) {
if(i % j) == 0 {
SUM+= j
}
if (sum == i then) {
printf("%d", SUM);
}
sum = 0;
}
return 0;
};


then is not a keyword.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
I compiled it and this is the error I get:

test.c: In function `main':
test.c:7: error: syntax error before '==' token

so in line 7.. hrm
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
int main (void) {
int i, j,SUM=0;
for (i=1;i<10000; i++) {
for (j=1;j<i; j++) {
if(i % j) == 0 {
SUM+= j ;
}
if (sum == i then) {
printf("%d", SUM);
}
sum = 0;
}
return 0;
};


need to close the sum+=j

the first sum = 0 is to initialize the sum variable and to set it to 0
then the remaining sum = 0 s resets the sum after every iteration.
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: Aznguy1872
how come in ur code we declaring that SUM = 0 twice?

He is declaring SUM 0 a second time for the same reason he declared it 0 the first time.

If he didn't declare SUM 0 the second time, it would contain the results of the previous iteration.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
I still get an error with that code, I changed the sum to SUM and got rid of the 'then' and i get

test.c: In function `main':
test.c:6: error: syntax error before '==' token