Prime factorization programm doesn't work for certain inputs - c

I'm a beginner. I am suppossed to write a simple primal factorization programm and what i came up with has a weird behavior.
Inputs are suppossede to be in the range of 64 bits integer(long long).
It works just fine until i input values of certain length i.e. 13. (picture attached), in which case it just shut's down without an error, which i assume indicated that the programm sees the number as 0, because it should give an error otherwise.
Now, i think problem may be in pointers or in scanf function, so i'd much appreciate if somebody points me in the way of where my mistake is.
I programm in VS on Windows 10, using standard command prompt as terminal.
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#include<stdint.h>
int ReadInput(int64_t *n, int *ret);
void Primal_Factorization(int64_t n);
enum {INPUT_ERROR = 100, SUCCESS = 0};
int main()
{
int ret = SUCCESS;
int64_t n = 0;
while (ReadInput(&n, &ret) > 0)
{
Primal_Factorization(n);
}
if (n < 0)
{
fprintf(stderr, "Error: Chybny vstup!\n");
ret = INPUT_ERROR;
}
return ret;
}
int ReadInput(int64_t *n, int *ret){
if(scanf("%lld", n) != 1){
*n = 0;
fprintf(stderr, "Error: Chybny vstup!\n");
*ret = INPUT_ERROR;
}
return *n;
}
void Primal_Factorization(int64_t n){
int64_t n_sqrt = sqrt(n);
int count;
int64_t n_origin = n;
bool first_iteration = true;
printf("Prvociselny rozklad cisla %lld je:\n", n);
for (int i = 2; i <= n_sqrt; i++){
count = 0;
if(n % i == 0){
if(first_iteration == false) printf(" x ");
while (n % i == 0){
n = n / i;
count++;
}
if(count != 1) printf("%d^%d", i, count);
else printf("%d", i);
first_iteration = false;
} else continue;
}
if(n_origin == n) printf("%lld\n", n);
else if(n != 1) printf(" x %lld\n", n);
else printf("\n");
}

In this function:
int ReadInput(int64_t *n, int *ret){
if(scanf("%lld", n) != 1){
*n = 0;
fprintf(stderr, "Error: Chybny vstup!\n");
*ret = INPUT_ERROR;
}
return *n;
}
The 64-bit integer is returned as an int which is then used in the main() function to check if the value is positive. The range of int is system-dependent, but if the int is 32 bit (very commonly the case), then for example 10^18 will after truncation to 32-bits result in the value -1486618624, i.e. a negative value. Thus the program terminates because the returned value from ReadInput() is negative, but it does not print the error because n (the untruncated 64-bit integer) is positive.
A minimal modification would be to have ReadInput() return a int64_t instead of int:
int64_t ReadInput(int64_t *n, int *ret)

Related

Is this c program null safe?

Is this is program to print prime numbers between two numbers
a and b (null safe or not)?
if not , How to add null safe function to make it (NULL SAFE)
I have tried checking if argv[1] or argv[2] are equall to 0
but it is not how it should work
any Ideas for this?
//check if a or b or both are negative numbers
int check_negative(int *x, int *y){
if(*x<0 || *y<0)
{
printf("Error : One or Both arguments are Negative\n");
return 1;
}
}
//check if n is a prime number
int isprime(int n)
{
int i, status = 1;
for(i=2; i <= n/2; ++i)
{
if (n%i == 0)
{
status = 0;
break;
}
}
return status;
}
int main(int argc, char* argv[] )
{
if(argc==1)
{
printf("Error : No arguments were passed\n");
return 0 ;
}
int a=atoi(argv[1]),b=atoi(argv[2]),n,status;
if(check_negative(&a,&b)==1)
{
return 0;
}
printf("Prime numbers between %d and %d are: \n", a, b);
for(n=a+1; n<b; ++n)
{
status = isprime(n);
if(status == 1)
printf("%d ",n);
}
return 0;
}
Is this c program null safe?
No.
The below fails when argc < 3 (e.g. 2,1,0) as it calls atoi() with NULL or unknown. #Ian Abbott
int a=atoi(argv[1]),b=atoi(argv[2]),n,status;
Instead change prior test from only testing against 1 to the below. Note that argc == 0 is possible on select platforms and with certain function calls.
// if(argc==1)
if(argc < 3)
As a stand-alone function, check_negative() does not check if a null pointer argument is passed before de-referencing. Add checks
int check_negative(int *x, int *y){
if (x == NULL || y == NULL) return 0; /// or handle in some way
Aside:
isprime(Any_int_less_than_2) errantly returns 1.
for(i=2; i <= n/2; ++i) is very slow for large n. Suggest speedier for(i=2; i <= n/i; ++i). Sample

I have to check a number is palindrome or not by recursion

int main()
{
int i,n;
printf("Enter the number");
scanf("%d",&n);
i=pali(n);
if(n==i)
printf("Number is pall");
else
printf("Not Pall");
}
int pali(int n)
{
int r;
static sum=0;
if(n!=0)
{
r=n%10;
sum=sum*10+r;
pali(n/10);
}
return sum;
}
I used a static variable to add up the sum. Is there any way where no static variable will be used?
Yes, the typical ("functional") approach is to carry the state in the form of a function argument. This often makes it necessary/nice to have a second function that does the actual recursion, which you can start by calling with the proper initial values for the state:
int do_pali(int sum, int n)
{
if(n != 0)
{
const int r = n % 10;
return do_pali(10 * sum + r, n / 10);
}
return sum;
}
the public function then just becomes:
int pali(int n)
{
return do_pali(0, n);
}
In languages with inner functions this can be more neatly expressed (GCC supports this as an extension).
Sure, you can do it this way :
#include <stdio.h>
int pali(int n)
{
int sum = 0;
int keeper = 0;
for (int i = n; i > 0; i /= 10) {
if (keeper != 0) {
sum *= 10;
sum += (keeper - i * 10);
}
keeper = i;
}
sum *= 10;
sum += keeper;
return sum;
}
int main(int argc, char** argv)
{
int i, n;
printf("Enter the number : ");
scanf("%d",&n);
i = pali(n);
if(n == i)
printf("Number is palindrome");
else
printf("Not Palindrome");
}
Using recursion is even easier :
#include <stdio.h>
int pali(int n, int sum)
{
sum += n - ((n / 10) * 10);
n /= 10;
if (n > 0)
pali(n, sum * 10);
else
return sum;
}
int main(int argc, char** argv)
{
int i, n;
printf("Enter the number : ");
scanf("%d",&n);
i = pali(n, 0);
if(n == i)
printf("Number is palindrome");
else
printf("Not Palindrome");
}
And a recursive version with only one parameter :
#include <stdio.h>
int pali(int n)
{
int fUnit, lUnit;
fUnit = n;
int mul = 1;
while (fUnit > 10) {
fUnit /= 10;
mul *= 10;
}
lUnit = n - ((n / 10) * 10);
n -= (fUnit * mul);
n /= 10;
if (mul == 1) return 1;
else if (fUnit == lUnit) return pali(n);
else return 0;
}
int main(int argc, char** argv)
{
int n;
printf("Enter the number : ");
scanf("%d",&n);
if(pali(n) == 1)
printf("Number is palindrome");
else
printf("Not Palindrome");
}
Since your function returns sum you could replace this line:
pali(n/10);
with
sum=pali(n/10);
You'd also have to move it up a line too.
Here is an optimized version that
Doesn't use local static.
Only includes stdio.h
Uses recursion.
#include <stdio.h>
static void perform_useless_recursion (int n)
{
if(n--)
{
perform_useless_recursion(n);
}
}
_Bool is_pali (int n)
{
perform_useless_recursion(1);
int sum = 0;
for(int i=n; i!=0; i/=10)
{
sum = sum*10 + i%10;
}
return n == sum;
}
int main (void)
{
int n=5005;
if(is_pali(n))
printf("Number is pall");
else
printf("Not Pall");
return 0;
}
The code could be improved even further by removing the perform_useless_recursion() function.
The advantage of this code is that the actual calculation is performed by a fast loop, instead of slow, dangerous recursion. In the real world outside artificial school assignments, there is no reason to write inefficient and dangerous code when you could write efficient and safe code. As a bonus, removing recursion also gives far more readable code.
If you benchmark this code you'll notice that it will be faster than all other versions posted and consumes less memory.
You can make a function that check only the first and the last digits of the number and pass the rest of the number onward.
To explain better, think about the following cases:
pali(1220) would check for the first (1) and last (0) digits. Since 1 != 0 pali would return false.
pali(17891) would check first (1) and last (1). Since they're equal then the function would recursively return pali(789) (which itself would return false since 7 != 9).
pali(878) would check that 8=8 and recursively return pali(7)
pali(3) would check that first (3) and last (3) numbers are equal and return 0.
The challenge here is to develop an algorithm that:
Check if first and last numbers are the same (even if it's only one digit!)
Strip the number from first and last digits and call itself on the remainder
Then all you need to do is apply recursion. Here's a sample implementation:
int pali(int number)
{
int smallDigit, bigDigit;
/* When recursion ends suceffuly*/
if (number == 0)
return 1;
/* Check for first and last digit of a number */
smallDigit = number % 10;
bigDigit = number;
while(bigDigit/10!=0)
{
bigDigit = bigDigit/10;
smallDigit = smallDigit*10;
}
/* Check to see if both digits are equal (Note: you can't use smallDigit here because it's been multiplied by 10 a few times) */
if (bigDigit != number%10)
return 0;
else
{
number = (number - smallDigit)/10; /* This is why smallDigit was multiplied by 10 a few times */
return pali(number); /* Recursion time */
}
}

Hamming Code - Error Detection & Correction

This question builds on a few other posts. I understand if this is not relevant to most of the people on the internet but at this point, just like anyone else, I am stuck and cannot find the bug in logic. This question requires checking a specified hamming code for a single-bit error and report/correct the error. Here is the program to do so:
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
/** Initializing the global variables */
int MaxLength;
int length;
int parity;
// Initialize the hamming string with a random or NULL memory address
char *HammingString=NULL;
/** Function to enter the values */
void EnterParameters(int *length, int *parity)
{
printf("Enter the maximum length: ");
/** %d reads an integer to be stored in an int. This integer can be signed */
scanf("%d", length);
printf("Enter the parity (0=even, 1=odd): ");
/** %d reads an integer to be stored in an int. This integer can be signed */
scanf("%d", parity);
}
void CheckHamming(char *HammingString, int parity)
{
// Initializing the local variables i, j, k, start, length, ParityNumber
int i, j, k, start, length, ParityNumber;
printf("Enter the Hamming code: ");
scanf("%s", HammingString);
int ErrorBit = 0; // Initialize the error bit
length = strlen(HammingString); // The strlen computes the length of a string up to, but not including the terminating null character
length--;
if (length > MaxLength)
{
printf("\n** Invalid Entry - Exceeds Maximum Code Length of %d\n\n", MaxLength);
return;
}
ParityNumber = ceil(log(length)/log(2)); // The ceil function returns the smallest integer that is greater than or equal to 'x'.
for(i = 0; i < ParityNumber; i++)
{
// pow returns x raised to the power y. In this case, 2 raised to the power i.
start = pow(2, i);
int ParityCheck = parity;
for(j = start; j < length; j=j+(2*start))
{
for(k = j; (k < ((2*j) - 1)) && (k < length); k++)
{
ParityCheck ^= (HammingString[length - k] - '0');
} // End the k for-loop
} // End the j for-loop
ErrorBit = ErrorBit + (ParityCheck * start);
} // End the i for-loop
if(ErrorBit == 0)
{
printf("No error \n");
}
else
{
printf("There is an error in bit: %d\n", ErrorBit);
if(HammingString[length - ErrorBit] == '0')
{
HammingString[length - ErrorBit] = '1';
}
else
{
HammingString[length - ErrorBit] = '0';
}
printf("The corrected Hamming code is: %s \n", HammingString);
}
} // End CheckHamming
int main()
{
int parity;
int choice = 0;
printf("Error detection/correction: \n");
printf("----------------------------\n");
printf("1) Enter parameters \n");
printf("2) Check Hamming code \n");
printf("3) Exit \n");
printf("\nEnter selection: ");
scanf("%d", &choice);
while (choice != 3)
{
if (choice == 1)
{
EnterParameters(&MaxLength, &parity);
HammingString = (char*) malloc (MaxLength * sizeof(char));
main();
}
else if (choice == 2)
{
CheckHamming(HammingString, parity);
main();
}
else
{
printf("Valid options are 1, 2, or 3. Quitting program. \n");
exit(0);
}
}//end while
exit(0);
}//end main
If the hamming code: 1000110 is entered, the hand-calculated error comes out to an error in but 6 with the corrected code being 1100110. This code displays an error in bit 3 with the corrected code being 1000010. Any help is be greatly appreciated.
I can't quite follow how your code is supposed to work. So here's a simple implementation that computes the syndrome for the code 1000110. The output from the program is 6, i.e. the error is in bit 6.
#include <stdio.h>
int main( void )
{ // 7654321
char input[] = "1000110";
int parity = 0;
for ( int mask = 4; mask; mask >>= 1 )
{
for ( int bit = 1; bit <= 7; bit++ )
if ( bit & mask )
if ( input[7-bit] == '1' )
parity ^= mask;
}
printf( "%d\n", parity );
}

Void Factorial Using Pointers in C

I want to do a void factorial function in C that uses pointers. This is my code but it won't run.
#include <stdio.h>
void factorial(int, int*);
int main()
{
int n;
int r = 0;
printf("Enter n : ");
scanf("%d", &n);
if (n < 0)
{
printf("No factorial for negative");
}
else
{
factorial(n , &r);
printf("factorial of %d is %d", n, r);
}
}
void factorial(int n , int *r)
{
if (n == 0 || n == 1)
{
*r= 1;
}
else
{
*r= n* factorial((n-1), r);
}
}
The error I get is in the last line of the code saying : invalid operands of type "int" and "void" to binary operator * , what does that mean and how to fix it?
Well, if factorial() returns void (which is basically , returning nothing) , this line does not make any sense
*result = num * factorial ((num-1), result);
It's because factorial() returns void.
Try:
factorial((num-1), result);
*result= num *(*result);
Instead of that line.
You cannot use return value of a function if it is declared to return void.
If you want to declare function this way then your factorial function can be:
void factorial(int num , int *result)
{
if (num == 0 || num == 1)
{
//*result = 1;
return;
}
else
{
*result *= num;
factorial ((num-1), result);
}
But also in main function you should change int res = 0 into int res = 1.
void Factorial(int *a,int *result){
if (*a == 0 || *a == 1){
return;
}
else{
*result *= *a;
*a -= 1;
Factorial(a,result);
}}

Return a blank integer in c

I want to write a power function that prints "Unable to compute o^o" when asked to do so or return the integer result. how can I accomplish this?
My current code prints the error statement as well as the result statement.
My code:
#include <stdio.h>
double power(int base,int n);
int main() {
int no1,no2;
printf("Enter two numbers:\n");
printf("If you want to compute x^y enter x y\n");
scanf("%i%i", &no1, &no2);
printf("The value of %i^%i is %f", no1, no2, power(no1,no2));
return 0;
}
double power(int base, int n) {
double result = 1;
if( n == 0 && base == 0){
printf("Unable to compute 0^0\n");
}
else if( n == 0 && base != 0) {
result = 1;
}
else if( n>0 ){
for(n ; n>0 ; n--) {
result = result*base;
}
}
else if( n<0 ){
int temp = -n;
result = power(base,temp);
result = (float)1.0/result;
}
return result;
}
EDIT: I am actually a novice. I am in the first chapter of K&R where I found a power function. I wanted to improve that thing and found this hurdle. Hence please provide resources if possible so that I understand your answers.
#BartoszMarcinkowski gives you the correct answer, as an alternative (if you can't pass an extra variable) you can return NAN and check the result with isnan().
In computing, NaN, standing for not a number, is a numeric data type
value representing an undefined or unrepresentable value, especially
in floating-point calculations.
#include <stdio.h>
#include <math.h>
double power(int base,int n);
int main(void)
{
double f;
f = power(2, 2);
if (isnan(f)) {
printf("Unable to compute power\n");
} else {
printf("%f\n", f);
}
return 0;
}
double power(int base, int n) {
double result = 1;
if( n == 0 && base == 0){
return NAN;
}
...
return result;
}
It is common for C functions to return 0 on success or error code in case of failure.
#include <stdio.h>
int power(int base,int n, double *result);
int main() {
int no1, no2;
printf("Enter two numbers:\n");
printf("If you want to compute x^y enter x y\n");
scanf("%i%i", &no1, &no2);
double result;
int error = power(no1, no2, &result);
if(error == 0)
printf("The value of %i^%i is %f\n", no1, no2, result);
return 0;
}
int power(int base, int n, double *result) {
*result = 1;
if(n == 0 && base == 0){
printf("Unable to compute 0^0\n");
return 1;
} else if(n == 0 && base != 0) {
*result = 1;
return 0;
} else if(n>0){
for(; n>0 ; n--) {
*result = *result*base;
}
return 0;
} else if(n < 0){
int temp = -n;
power(base,temp, result);
*result = (float) 1.0 / *result;
return 0;
}
return 1;
}
Note : My answer is in reference to the user's post saying that he is
a novice at learning C, so I have assumed that he might not have yet
be comfortable dealing with pointers.
You can have an error flag in your code that tells the main function that an error has occurred.
You can use a global integer, that is common to all functions of your program to inform of errors in code.
After calling the power function, you check whether the error FLAG is set, which means errors have occurred. So you print the error message instead of the result.
What value you should use for showing a error has occurred is arbitrary. In most cases, a non-zero (sometimes negative) value can indicate the specific error. For example:
FLAG = -1 might indicate invalid input
FLAG = -2 might indicate invalid operation
And so on. etc. Here I have chosen FLAG = non-zero value to mean error has occurred.
#include <stdio.h>
double power(int base,int n);
--> char FLAG = 0;
int main() {
int no1,no2;
---- your code ---
double and = power(no1,no2);
--> if (char) {
printf("error : both values cannot be negative);
char = 0;
} else {
printf("The value of %i^%i is %f", no1, no2, ans);
}
return 0;
}
double power(int base, int n) {
---- your code ---
if( n == 0 && base == 0){
printf("Unable to compute 0^0\n");
--> FLAG = -1;
return 0;
}
else if( n == 0 && base != 0) {
result = 1;
}
else ---- your code ---
}

Resources