ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
// load array with divisors of c
for (d = 1; d <= c; d++) {
if (c % d == 0) { // correct
printf("%i, %i\n", d, f); // prints correct
/* here is where it hangs and gets stuck. it can't hit the array
for whatever reason. f is initialized above to int f = 0;
without the printf it just sits there until it exits with nothing
in the array. f however does iterate as shown in the printf as well.
c_array[f] = d;
printf("%i\n", c_array[f]);
*/
if (d == c / 2){ // correct
printf("breaks here - %i", f);
break;
} else {
f++;
continue;
}
} else {
continue;
}
}
I cannot get the c_array to load, just hangs then exits with nothing in the array. However the exact pattern works above it in this loop, tested via printf each loop.
Code:
// holding location for divisors of c
int c_array[c];
// pre load array with 0. works, tested with printf added to loop
for (int d = 0; d <= c; d++) {
c_array[d] = 0;
}
The purpose ultimately is a function that calcs the greatest common divisor of 2 numbers.
Thank you in advance. I'm expecting I'm missing something ridiculous but I have been hacking at this for awhile and it's time to call in people who actually know.
For what it's worth the only compiler error as the source file sits now is a warning that variables e and num2 are set but not used.
Last edited by jmgibson1981; 01-26-2023 at 12:17 PM.
The purpose ultimately is a function that calcs the greatest common divisor of 2 numbers.
Code:
//ONE
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i) {
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
printf("\n");
return 0;
}
/**********************************************************/
//TWO
#include <stdio.h>
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
while(n1!=n2) {
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD = %d", n1);
printf("\n");
return 0;
}
//gcc gcd.c -o gcd
Code:
./gcd
Enter two integers: 81 153
G.C.D of 81 and 153 is 9
./gcd
Enter two integers: 8 12
G.C.D of 8 and 12 is 4
./gcd
Enter two positive integers: 153 81
GCD = 9
./gcd
Enter two positive integers: 8 12
GCD = 4
./gcd
Enter two positive integers: 100 20
GCD = 20
Try to avoid overusing break/continue statements in your code if you can. They often lead to logic errors and generally make the code more difficult to follow.
Here's an example:
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_FACTORS 1024
static int factors[MAX_FACTORS];
size_t find_factors( int n )
{
size_t count = 0;
for ( int i = 2 ; i <= n / 2 ; i++ )
if ( count < MAX_FACTORS )
{
if ( n % i == 0 )
factors[count++] = i;
} else {
fprintf(stderr, "factors[] max array size exceeded. Aborted.\n");
exit(EXIT_FAILURE);
}
return count;
}
int main( int argc, char *argv[] )
{
if (argc < 2 )
return 1;
int arg = atoi(argv[1]);
if ( arg < 1 )
return 1;
size_t n = find_factors(arg);
if ( n == 0 )
puts("Prime.");
else
for (size_t i = 0 ; i < n ; i++)
printf("factor: %d\n",factors[i]);
return 0;
}
// pre load array with 0. works, tested with printf added to loop
for (int d = 0; d <= c; d++) {
You have a bug here, the valid indices for c_array are 0 to c-1, so you should write:
Code:
for (int d = 0; d < c; d++) {
After fixing that I get some output which looks reasonable. I worked this out by running your program under gdb, seeing that it crashed at the c_array[f] = d; statement with f being a reasonable looking 0, but c_array had some kind of nonsense value. I restarted and stopped at the initial definition of c_array, where it still had a sensible value, and stepped forward until it longer did. Then it became clear that the pre load loop was doing something wrong.
Quote:
For what it's worth the only compiler error as the source file sits now is a warning that variables e and num2 are set but not used.
e is set but not used (harmless), however, num2 is the other way around (used but not set, bad!):
Code:
lq-factors.c:11:5: warning: 'num2' is used uninitialized in this function [-Wuninitialized]
11 | greatest_c_divisor(num1, num2);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Which means that you are taking greatest_c_divisor(20, ???), which could give you just about anything...
Thank you much. I'm over complicating the hell out of it. That narrows it down. This is exhausting learning this stuff. I appreciate the replies from all as answered and explained.
*EDIT* I look at what I tried. Then i look again at this one.
Code:
for(i=1; i <= n1 && i <= n2; ++i) {
if(n1%i==0 && n2%i==0)
gcd = i;
}
My eyes are big shock at how far I'd gone in the wrong direction.
Last edited by jmgibson1981; 01-26-2023 at 04:49 PM.
I haven't even looked into it. I will be using it from now on. I'm still sorting out basics (clearly), much less debugging. Thus far if it runs and compiles with no errors then I consider it done. Clearly I am incorrect.
I think it has been mentioned before but I get distracted easy and forget things quite often. I'm putting it on a sticky note thing on my desktop.
At first I was making quick progress. This one stumped me good though. I can see how my process was wrong but I think I went this way because I wasn't entirely sure on the formula to do it and or how to put it in code. I'll need to refresh on math as well I'm quickly realizing.
Last edited by jmgibson1981; 01-26-2023 at 08:56 PM.
Thus far if it runs and compiles with no errors then I consider it done.
So couple of things and separately a story, where I'll indicate your can ignore (more than usual )
Since the dawn of programming, your statement is "famous last words", it is very likely that you get code compiled where there are bugs.
From there, there are a few classic ways of debugging, output statements, secondary processing of large data outputs, using a debugger, are some classical ones.
The exact point where you wrote, "At first I was making quick progress. This one stumped me good though.", is a great point to consider altering your diagnostic approach. But it's up to you to choose what works best for you, and also how much to add, and when to add, to your programming toolbox.
I have a blog on it, in my signature, or plenty of refs on the web.
Story:
Our company added very inclusive design and review processes (1980's) and lengthy code review sessions were part of it, followed by on system unit tests.
One developer was having a bad day, started hiting the bench or keyboard, finally exasperated, "I can't BELIEVE this code has SO MANY BUGS! I mean it was REVIEWED!"
Everybody turned and looked at him, then mutually turned to the most convenient peer and made non-verbal faces saying, "Well HE'S gonna last!", smirked and went back to work.
I bet. I'm not up to the debugging section on the course I'm following. It's coming up soon though I can see in the list.
I have no intention of giving up unless I can not only fail, but prove that I can't succeed at it. But it definitely is increasing in challenge for me.
I look back at my past and think of how much time I wasted with gaming, not that it's a bad thing of course. But I could have spent at least some of it learning something useful. Now I'm 41 and it's not as easy to acquire new concepts. Won't be giving up soon though for sure.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.