Working with unsigned integers -- printf conflict?

Functions working properly, but printf output incorrect.

Page 1 of 1

3 Replies - 755 Views - Last Post: 01 September 2010 - 05:39 PM Rate Topic: ***-- 2 Votes

#1 pushasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-September 10

Working with unsigned integers -- printf conflict?

Posted 01 September 2010 - 03:27 PM

I'm doing a simple prime factorization program with two different functions: one that counts how many prime factors a number has, and another that counts how many distinct prime factors a number has. I am sure that these functions are working correctly, because when I only test one function at a time (using a 'printf' debug statement), the output is correct. However, if I try to use more than one function in a certain section of code, it appears as if some sort of overflow has occurred. I'm not to familiar with C, so my speculation might be completely off.

#include <assert.h>
#include <math.h>     
#include <stdio.h>
#include <string.h>   /* Included for strcmp. */



unsigned int
check_prime(unsigned int number)
{

	unsigned int divisor;
	unsigned int boolprime;

	boolprime = 1;  /* Initialize boolean 'prime' as being true */
	
	for (divisor = number - 1; divisor > 1; divisor--)
	{
		if (number % divisor == 0)
		{
			boolprime = 0;
			divisor = 1;    /* to exit loop when not prime */
		}
	}
	
	return (boolprime);

}







unsigned int
count_factors(unsigned int n)
{
	
	unsigned int factor; 
	unsigned int result;
	unsigned int count;

	result = n;

	assert(n > 1);
	
	for (factor = 2; result > 1; factor++)
	{
		while (result % factor == 0 && check_prime(factor))
		{
			result = result / factor;
			count++;
		}
	}
	printf
	return (count);
       
}





unsigned int
count_distinct_factors(unsigned int n)
{
	unsigned int factor;
	unsigned int result;
	unsigned int count;
	
	result = n;


	assert(n > 1);
	
	for (factor = 2; result > 1; factor++)
	{
		while (result % factor == 0 && check_prime(factor))
		{
			result = result / factor;
		}
		
		count++;
	}

	return (count);
}







void
test_factors(void)
{
	unsigned int n = 12;
	
		
	printf("\n%u has ", n);
	printf("%u factors, ", count_factors(n));
	printf("%u of them distinct.\n",count_distinct_factors(n));
 
}





/* 
 * Requires:
 *   Nothing.
 *
 * Effects:
 *   If the "-t" option is specified on the command line, then test code is
 *   executed.  Otherwise, requests a number from standard input and prints
 *   the number of prime factors that number has and the number of those
 *   factors that are distinct.  Upon completion, the program always returns
 *   0.
 *
 *   If the number that is input is not between 2 and the largest unsigned
 *   integer, the output of the program is undefined, but it will not crash.
 */




int
main(int argc, char **argv)
{
	unsigned int n;

	/* If "-t" is specified, run test procedure and exit program. */
	if (argc > 1 && strcmp(argv[1], "-t") == 0) {
		test_factors();
		return (0);
	}

	/* Get input. */
	printf("Enter number:\n");
	scanf("%u", &n);
	
	
	/* Print results. */
	printf("%u has %u prime factors, %u of them distinct.\n",
	    n, count_factors(n), count_distinct_factors(n));

	/* No errors. */
	return (0);
}





When I comment out all but one of the lines in the "test_factors" function, the output is correct. However, when I try to print all of them (see: 'main' function), the output is way off. For example, an input of 12 for the 'main' function would output the following:
" 12 has 11084 prime factors, 11081 of them distinct."

I'm assuming this has something to do with the fact that I am working exclusively with unsigned integers and that there is some sort of overflow/offset that I am unaware of. Any suggestions would be greatly appreciated.

-- Sarah --

Is This A Good Question/Topic? 0
  • +

Replies To: Working with unsigned integers -- printf conflict?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Working with unsigned integers -- printf conflict?

Posted 01 September 2010 - 04:10 PM

I question your error reporting. Can you show us an example of the output from running your program? Copy paste it exactly.
Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Working with unsigned integers -- printf conflict?

Posted 01 September 2010 - 04:21 PM

when you declare the variable "count" what is its initial value?
unsigned int count;



unknown right? so what is the value of this statment
count++;



this can be expanded to count = count + 1; but count is unknown so the value is count = unknown + 1? does that make any sense.

Initialized variables work better.
Was This Post Helpful? 1
  • +
  • -

#4 pushasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-September 10

Re: Working with unsigned integers -- printf conflict?

Posted 01 September 2010 - 05:39 PM

View PostImaSexy, on 01 September 2010 - 03:21 PM, said:

when you declare the variable "count" what is its initial value?
unsigned int count;



unknown right? so what is the value of this statment
count++;



this can be expanded to count = count + 1; but count is unknown so the value is count = unknown + 1? does that make any sense.

Initialized variables work better.




Thank you so much! The best program glitches are the simple ones.
I was making the issue more complicated than it needed to be.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1