LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2023, 12:14 PM   #1
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,156

Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
Trouble loading array - C


Code:
    // 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;
    }
Current source file - https://gitlab.com/jmgibson1981/mycp...0-%2066/main.c

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.
 
Old 01-26-2023, 01:08 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,157
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
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
 
1 members found this post helpful.
Old 01-26-2023, 03:31 PM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,922

Rep: Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040
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;
}
 
1 members found this post helpful.
Old 01-26-2023, 04:22 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
    // 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...
 
1 members found this post helpful.
Old 01-26-2023, 04:48 PM   #5
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,156

Original Poster
Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
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.
 
Old 01-26-2023, 04:50 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
This seems like something you can debug, in the debugger. Have you considered gdb?
 
Old 01-26-2023, 08:53 PM   #7
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,156

Original Poster
Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
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.
 
Old 01-27-2023, 02:32 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by jmgibson1981 View Post
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.

Last edited by rtmistler; 01-27-2023 at 02:33 PM.
 
Old 01-27-2023, 02:54 PM   #9
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,156

Original Poster
Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
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.
 
Old 01-27-2023, 04:28 PM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by jmgibson1981 View Post
Now I'm 41 and it's not as easy to acquire new concepts. Won't be giving up soon though for sure.
57 not an academic, working for an employer. I feel I learn new stuff everyday, and do invite that.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
Finding Module Dependencies...(Still loading...still loading..still loading..HANG!!!) Aeudian Linux - General 3 08-11-2003 03:31 PM
Finding Module Dependencies.....(still loading....Still loading....still loading) Aeudian Linux - Newbie 1 07-28-2003 02:27 PM
trouble ahead, trouble behind....trouble with mplayer Goonie Linux - Software 3 07-02-2003 02:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration