unexpected output -scanf and function check - c

So i am trying to write this program but i get 10 no matter what the input is. My function seems correct to me so is it a scanf issue?
Write a program to input 10 integers and decide how many of them satisfy the
following rule:
abcd = (ab + cd)2 e.g. 3025=(30+25)
Use a function that receives an integer parameter returns 1 if it satisfies the above
rule, returns 0 otherwise.
int check(int n);
int main(void)
{
int n, i, j = 0;
printf("Input 10 integers: ");
for (i = 0; i < 10; i++)
{
scanf("%d", &n);
if (check(n) == 1)
{
j++;
}
}
printf("%d\n", j);
}
int check(int x)
{
if (((x / 100) + (x % 100)) * ((x / 100) + (x % 100)))
{
return 1;
}
else
{
return 0;
}
}

The issue I think is the check function,
if (((x / 100) + (x % 100)) * ((x / 100) + (x % 100))) // <---- anything not zero will be true
{
return 1;
}
The expression inside if will convert any integer which is not zero to true. The expression as it is written is if (x * x) which only false if x == 0.

Related

Recurrence relation of recursive function that contains while loop

I have a recursive function int digit_sum(int number); that returns the sum of all the digits in the number. For example, digit_sum(159) = 1 + 5 + 9 = 15.
Here is the function:
int digit_sum(int n)
{
if (n < 0)
n = -n;
if (n < 10)
return n % 10;
while (n % 10 == 0 && n > 0)
n = n / 10;
return n % 10 + digit_sum(n - (n % 10));
}
I'm not sure exactly how to write a recurrence relation for this function. I know that T(0) is the sum of the first two if statement constants. However, with T(n), I am unsure how to express the while loop term and T(n-k).
The modulo operators are throwing me off. This is a guess, and I'm fairly certain this is wrong:
T(n) = c_1 + c_2 + c_3*n (while loop) + (n mod 10 + T(n - (n mod 10))) for n >= 10
I know that the entire T(n-k) term is wrong.
Here is a possible solution:
#include <stdio.h>
int digit_sum(int n)
{
if (n < 0)
return digit_sum(-n);
if (n < 10)
return n;
return (n % 10) + digit_sum(n / 10);
}
int main(int argc, char *argv[])
{
printf(" 9->%d\n", digit_sum(9));
printf(" 59->%d\n", digit_sum(59));
printf(" 159->%d\n", digit_sum(159));
printf("-159->%d\n", digit_sum(-159));
}

Sum of Digits of a Five Digit Number in c?Could you spot the error?

BTW I know that's not the most efficient way to do it but if I wanted to do it like I did, what did I do incorrectly? The task was: Given a five digit integer, print the sum of its digits.
Constraint: 10000 <= n <= 99999
Sample Input: 10564
Sample Output: 16
My code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, sum;
int remainder_array[4] = { n % 1000; n % 100, n % 10, n };
int digits_array[4];
scanf("%d", &n);
// Complete the code to calculate the sum of the five digits on n.
if (10000 <= n && n <= 99999) {
else if (remainder_array[0] = 0) {
digits_array[0] = (n - remainder_array[0]) / 1000;
n = remainder_array[1];
} else if (remainder_array[1] != 0) {
digits_array[1] = (n - remainder_array[1]) / 100;
n = remainder_array[2];
} else if (reminder_array[2] != 0) {
digits_array[2] = (n - remainder_array[2]) / 10;
n = remainder_array[3];
} else if (reminder_array[3] != 0) {
digits_array[3] = n - remainder_array[3];
} else {
printf("%d", n / 1000);
}
sum = digits_array[0] + digits_array[1] + digits_array[2] + digits_array[3];
printf("%d", sum);
}
return 0;
}
your algorithm is far too complicated, It can be done much easier way without arrays.
int sumof5LSD(int x)
{
int result = 0;
for(int digit = 1; digit <=5; digit++)
{
result += abs(x % 10);
x /= 10;
}
return result;
}
int main(void)
{
printf("SUM: %d", sumof5LSD(10564));
}
https://godbolt.org/z/bcdM8P
or if you are not allowed to use loops:
int sumof5LSD(int x)
{
int result = 0;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
return result;
}
It is good to use functions to perform similar tasks.
Version with scanf
int main(void)
{
int n;
scanf("%d", &n);
printf("SUM of 5 digits of %d = %d", n, sumof5LSD(n));
}
it will also calculate the sum of 5 digits of the negative number
There are multiple issues in your code:
you initialize remainder_array from the value of n before reading the value of n.
the initializer is incorrect: the separator should be ,, not ;.
you start the statement inside the if body with else, which is a syntax error.
the test if (remainder_array[0] = 0) sets remainder_array[0] to 0 and evaluates to false.
remainder_array is misspelt a reminder_array
Your approach is fine, but you should intialize remainder_array with the actual remainders (5 of them), after reading and checking n:
#include <stdio.h>
int main() {
int n;
if (scanf("%d", &n) == 1 && 10000 <= n && n <= 99999) {
int remainder_array[5] = { n / 10000, n / 1000 % 10, n / 100 % 10, n / 10 % 10, n % 10 };
int sum = remainder_array[0] + remainder_array[1] + remainder_array[2] +
remainder_array[3] + remainder_array[4];
printf("%d\n", sum);
}
return 0;
}
Note that you don't actually need this remainder_array, you could just write:
#include <stdio.h>
int main() {
int n;
if (scanf("%d", &n) == 1 && 10000 <= n && n <= 99999) {
int sum = n / 10000 + n / 1000 % 10 + n / 100 % 10 + n / 10 % 10 + n % 10;
printf("%d\n", sum);
}
return 0;
}
Here is a more readable and more generic version:
#include <stdio.h>
int main() {
int n;
if (scanf("%d", &n) == 1 && 10000 <= n && n <= 99999) {
int sum = 0;
while (n >= 10) {
sum += n % 10;
n = n / 10;
}
sum += n;
printf("%d\n", sum);
}
return 0;
}
Apart from the errors and modifications what #P_J_ and #chqrlie has mentioned I have noticed some major logical errors and misunderstanding of a basic concept in your code(assuming that first else if is replaced by if)
you have given else if statement repeatedly, now what this does is that whenever the first condition it encounters is true it executes the block inside and exits from the branch i.e remaining statements after else if is not executed, this might cause a major logical error in your program.
if (10000 <= n && n <= 99999) {
else if (remainder_array[0] = 0) {
digits_array[0] = (n - remainder_array[0]) / 1000;
n = remainder_array[1];
} else if (remainder_array[1] != 0) {
digits_array[1] = (n - remainder_array[1]) / 100;
n = remainder_array[2];
} else if (reminder_array[2] != 0) {
digits_array[2] = (n - remainder_array[2]) / 10;
n = remainder_array[3];
} else if (reminder_array[3] != 0) {
digits_array[3] = n - remainder_array[3];
} else {
printf("%d", n / 1000);
}
Now in this picture if you notice the output you can see that the digits_array[1-3] are 0 this is because of the reason mentioned above(it is zero cause I have initialized it beforehand) hence the sum is zero.
And the second logical error is that you are dividing a 5 digit number by 1000 this will give you thousand's place i.e in example 10546 this step will result in 10 this is wrong, take another example 12233 not the sum for the digits should result in 11 but you will get 20 because when you divide 12233 by 1000 the first value of digits_array (digit_array[0]) is 12 so the entire output goes wrong. so to correct this divide it by 10000 (only for this program statement as it has 5 digits).
But still, if you wish to continue without changing the algorithm then this code should work fine.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, sum;
int digits_array[5]={0}; //5 because it is a five digit number.
scanf("%d", &n);
int remainder_array[] = { n % 1000, n % 100, n % 10, n };//This is still a useless array this is kept so as to enter if.
// Complete the code to calculate the sum of the five digits on n.
if (10000 <= n && n <= 99999) {
if (remainder_array[0] != 0) {
digits_array[0] = (n) / 10000;//subtracting n with contents of remainder_array added to the complexity of the algorithm so took out the statement.
n =(n)%10000;
} if (remainder_array[1] != 0) {
digits_array[1] = (n) / 1000;
n =(n)%1000;
} if (remainder_array[2] != 0) {
digits_array[2] = (n) / 100;
n =(n)%100;
} if (remainder_array[3] != 0) {
digits_array[3] = (n)/10;
n=(n)%10;
}
digits_array[4]=n;//the last element of the number
}
sum = digits_array[0] + digits_array[1] + digits_array[2] + digits_array[3]+digits_array[4];
printf("%d",sum);
return 0;
}
Note: This program is not the best way and some changes are made refer to other answers for a more effective code and get the basics right before going to nested if condition.
Enjoy coding

Collatz sequence from 1 to a given value

#include <stdio.h>
int main() {
int rangeValue;
int x;
printf("Please input a number to be considered in the range:\n");
scanf("%d", &rangeValue);
while (rangeValue != 1) {
x = rangeValue;
if ((x % 2) == 0) {
x = x / 2;
printf("%d,", x);
} else {
x = (3 * x) + 1;
printf("%d,", x);
}
rangeValue--;
}
return 0;
}
My goal is to do the Collatz sequence of every number from 1 to the number I give to rangeValue. I expected this to work. Can anyone help me make it work?
You are mixing the range of sequences to print, the maximum number of iterations and the current number in the sequence.
Here is how to fix the code:
#include <stdio.h>
int main(void) {
int rangeValue;
printf("Please input a number to be considered in the range:\n");
if (scanf("%d", &rangeValue) != 1)
return 1;
// iterate for all numbers upto rangeValue
for (int n = 1; n <= rangeValue; n++) {
printf("%d", n);
for (long long x = n; x != 1; ) {
if ((x % 2) == 0) {
x = x / 2;
} else {
x = (3 * x) + 1;
}
printf(",%lld", x);
}
printf("\n");
}
return 0;
}

Multiply digits of a number using recursion

I am doing the following exercise:
Given a four digit number such as 3183, compare each digit with the last and if greater or equal multiply it with the following
Example: for the number 3183 it would be n = 3*8*3 = 72.
My code:
#include <stdio.h>
int f ( int n )
{
if ( n < 10 )
return n ;
return (((n/10) % 10) >= (n%10) ? ((n/10)10) : 1) * f((n/100 )* 10 + n % 10 ) ;
}
int main()
{
printf( "%d", f( 3183 );
return(0);
}
Is there any way to shorten it or make it better?
Leave another approach more compacted than the original:
#include <stdio.h>
int f (int n, int u)
{
if (u > n) return(1);
return (n % 10 >= u ? n % 10 : 1) * f(n/10, u);
}
int main (void)
{
int n = 3284;
printf ("%d", f (n , n%10));
return(0);
}
EDIT I mis-read this yesterday. No point in effectively re-creating #Red Alert's answer now, but I can't delete it either since't it's accepted so here goes.
I assume we can create our own "inner" function to maintain state. I also assume digits are to be processed from the right, the original example isn't clear.
static int g(int n, int ack, int last)
{
const int here = n % 10;
const bool mult = here >= last;
if(n < 10)
return mult ? here * ack : here;
return g(n / 10, mult ? here * ack : ack, here);
}
int f(int n)
{
return g(n, 1, 0);
}
After accept answer
OP's code fails to compile, missing %
// (((n/10) % 10) >= (n%10) ? ((n/10) 10) : 1) * f((n/100 )* 10 + n % 10 ) ;
return (((n/10) % 10) >= (n%10) ? ((n/10)%10) : 1) * f((n/100 )* 10 + n % 10 ) ;
As #interjay recommend, save results rather than recalculating.
#include <stdio.h>
int f(int n) {
if (n < 10)
return n;
int lastdigit = n % 10;
int nextlastdigit = (n / 10) % 10;
return (nextlastdigit >= lastdigit ? nextlastdigit : 1)
* f((n / 100) * 10 + lastdigit);
}
int main(void) {
printf( "%u", f(2183); // --> 24
return(0);
}
To make better, I would reduce division calls and multiplication by 1. But better is subjective at this point.
unsigned cheroky(unsigned x) {
if (x < 10)
return x;
unsigned lastdigit = x % 10;
unsigned firstdigits = x / 10;
unsigned lastfirstdigit = firstdigits % 10;
unsigned nextx = firstdigits - lastfirstdigit + lastdigit;
unsigned product = cheroky(nextx);
if (lastfirstdigit >= lastdigit)
product *= lastfirstdigit;
return product;
}
To really improve, would use a non-recursive loop.
unsigned cheroky2(unsigned x) {
unsigned lastdigit = x % 10;
unsigned product = lastdigit;
while (x >= 10) {
x /= 10;
unsigned nextdigit = x % 10;
if (nextdigit >= lastdigit)
product *= nextdigit;
}
return product;
}
Are you allowed to use an intermediate recursive function? This eliminates the extra math you are doing to maintain the state of the last digit:
int f2 ( int n, int lastDigit )
{
int currentDigit = n%10;
int returnDigit = currentDigit;
if(currentDigit < lastDigit)
returnDigit = 1;
if(n < 10)
return returnDigit;
return returnDigit * f2(n/10, lastDigit );
}
int f ( int n )
{
if ( n < 10 )
return n ;
return n%10* f2(n/10, n%10);
}

Print integer as character digits (no arrays/printf/etc...)

I am attempting to print integers to the console in C with a few constraints, the most significant of which being that I may only write individual characters to the console as follows:
void my_char(char ch)
}
write(1, &ch, 1);
}
Other constraints include NO predefined methods (printf, log, etc). No recursion. Lastly, I may NOT create an array.
So far I have come up with a method that prints the numbers out perfectly well... backwards.
int main()
{
int i = -345320;
my_int(i);
return 0;
}
void my_int(int x)
{
char *a;
int n;
if(x < 0)
{
x = x * -1;
my_char('-');
}
while(x)
{
n = x % 10;
a = (char*)&n;
my_char(*a + 48);
x /= 10;
}
}
Are there other good ways to approach this or am I at least going in the right direction? I would ideally like to expand this to print an integer in any base I provide, but I need to start here.
I was playing with iterating a pointer over each Byte of the integer but I can't grasp how I would use those character values to re-create the integer.
Any advice is appreciated. I'd much rather receive some insight than just a code solution. I'd also love input on making it more lean.
Here's a general (ugly!) solution following your constraints. It uses the idea I gave in the comment above. It assumes 32-bit ints.
void my_int(int x) {
int n = 1000000000;
if (x == 0) {
my_char('0');
return;
}
if (x == INT_MIN) { // INT_MIN is in limits.h
my_char('-'); my_char('2'); my_char('1');
my_char('4'); my_char('7'); my_char('4');
my_char('8'); my_char('3'); my_char('6');
my_char('4'); my_char('8');
return;
}
if (x < 0) {
x *= -1;
my_char('-');
}
while (n > x) n /= 10;
while (n != 0) {
my_char(x / n % 10 + '0');
n /= 10;
}
}
This should do the trick. It prints the integer forwards.:
void my_int(int x)
{
int temp = 0;
int divFactor = 10;
if(x==0)
{
my_char('0');
return;
}
if(x < 0)
{
x = x * -1;
my_char('-');
}
temp = x;
while((temp /= 10) > 10) {divFactor *= 10;}
for(;divFactor > 0;divFactor /= 10)
{
temp = x;
temp /= divFactor;
my_char(temp + '0');
x -= divFactor * temp;
}
printf("\n done!");
}
int main()
{
int i = -1234001;
my_int(i);
return 0;
}
void my_int(int x)
{
int n;
int copy;
char digit;
// handle 0
if (!x)
{
my_char('0');
return;
}
// emit sign
if(x < 0)
{
x = x * -1;
my_char('-');
}
// count base-10 digits in x, store 10^n in n
n = 1;
copy = x/10; // shorten loop by 1 iteration
while (copy)
{
n *= 10;
copy /= 10;
}
// 'n' is now a digit selector
while (n)
{
digit = x/n;
my_char(digit + '0'); // print the most significant digit
x -= digit*n; // remove the most significant digit from x
n /= 10;
}
}

Resources