How to fix the output of the following C program? - c

I have a problem with output of the following C program. It does not get me the correct output.I transform this program from the sum of two big numbers.
So I get the output with some "errors" like "232423-3-4-34--3424" instead of "23242334343424". How do i fix this?
#include<stdio.h>
int main() {
int num1[255], num2[255], dif[255];
char s1[255], s2[255];
int l1, l2;
printf("Enter Number1:\n");
scanf("%s", &s1);
printf("Enter Number2:\n");
scanf("%s", &s2);
for (l1 = 0; s1[l1] != '\0'; l1++)
num1[l1] = s1[l1] - '0';
for (l2 = 0; s2[l2] != '\0'; l2++)
num2[l2] = s2[l2] - '0';
int carry = 0;
int k = 0;
int i = l1 - 1;
int j = l2 - 1;
for (; i >= 0 && j >= 0; i--, j--, k++) {
dif[k] = (num1[i] - num2[j] - carry) % 10;
carry = (num1[i] - num2[j] - carry) / 10;
}
if (l1 > l2) {
while (i >= 0) {
dif[k++] = (num1[i] - carry) % 10;
carry = (num1[i--] - carry) / 10;
}
} else if (l1 < l2) {
while (j >= 0) {
dif[k++] = (num2[j] - carry) % 10;
carry = (num2[j--] - carry) / 10;
}
} else {
if (carry > 0)
dif[k++] = carry;
}
printf("Result:");
for (k--; k >= 0; k--)
printf("%d", dif[k]);
return 0;
}

Your result includes negative numbers because the code doesn't correctly implement modulo-10 arithmetic. In lines like this:
dif[k] = (num1[i] - num2[j] - carry) % 10;
If subtraction num1[i] - num2[j] - carry is less than 0, you want to store the result of 10-subtraction. There are languages where the % operator works like that in, but in C it returns a negative number, so -1 % 10 yields -1 and not 9.
To fix the issue, the code needs to be more explicit, e.g.:
int sub = num1[i] - num2[j] - carry;
if (sub >= 0) {
dif[k] = sub;
carry = 0;
} else {
dif[k] = 10 - sub;
carry = 1;
}

Related

K&R C Book: Exercise 3-5 itob function

In the following function itob() what does the snippet j + 'A' - 10 do? I don't get why we subtract 10:
/* itob: convert n to characters in s - base b */
void itob(int n, char s[], int b) {
int i, j, sign;
i = 0;
if ((sign = n) < 0)
n = -n;
do {
j = n % b;
s[i++] = (j <= 9) ? j + '0' : j + 'A' - 10;
} while ((n /= b) != 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}

program to add one to each digit of a number

As new to competitive programming, I was solving this practice question. The goal is to write a program to display numbers whose digits are 1 greater than the corresponding digits of the entered number. So if the number input is 12345 then the output number should be 23456. I have figured out how to separate each number and add them, but I was unable able to take a number of test cases in the following program.
The question is as follows
Input
First line of input will contain a number N = number of test cases. Next N lines will contain number n as test case where 1<=n<=99999.
Output
For each input case, add one to each digit of n, and print the new number.
As a beginner in competitive programming would be helpful if you give some tips to optimize the code.
here is the code that I have written.
#include<stdio.h>
void main()
{
int n, t, sum = 0;
scanf("%d", &t);
int a[t];
for (int j = 0; j < t; j++)
{
for (int i = 0; i < t; i++)
{
scanf("%d", &n);
a[i] = n;
if (t == 1) {
if (i == 0) {
a[i] = (a[i] + 1) * 1;
}
}
else if (t == 2) {
if (i == 0) {
a[i] = (a[i] + 1) * 10;
}
else if (i == 1) {
a[i] = (a[i] + 1) * 1;
}
}
else if (t == 3) {
if (i == 0) {
a[i] = (a[i] + 1) * 100;
}
else if (i == 1) {
a[i] = (a[i] + 1) * 10;
}
else if (i == 2) {
a[i] = (a[i] + 1) * 1;
}
}
else if (t == 4) {
if (i == 0) {
a[i] = (a[i] + 1) * 1000;
}
else if (i == 1) {
a[i] = (a[i] + 1) * 100;
}
else if (i == 2) {
a[i] = (a[i] + 1) * 10;
}
else if (i == 3) {
a[i] = (a[i] + 1) * 1;
}
}
else if (t == 5) {
if (i == 0) {
a[i] = (a[i] + 1) * 10000;
}
else if (i == 1) {
a[i] = (a[i] + 1) * 1000;
}
else if (i == 2) {
a[i] = (a[i] + 1) * 100;
}
else if (i == 3) {
a[i] = (a[i] + 1) * 10;
}
else if (i == 4) {
a[i] = (a[i] + 1) * 1;
}
}
else if (t == 6) {
if (i == 0) {
a[i] = (a[i] + 1) * 100000;
}
else if (i == 1) {
a[i] = (a[i] + 1) * 10000;
}
else if (i == 2) {
a[i] = (a[i] + 1) * 1000;
}
else if (i == 3) {
a[i] = (a[i] + 1) * 100;
}
else if (i == 4) {
a[i] = (a[i] + 1) * 10;
}
else if (i == 4) {
a[i] = (a[i] + 1) * 1;
}
}
}
}
for (int i = 0; i < t; i++)
{
sum = sum + a[i];
}
printf("%d\n", sum);
}
I've reworked on the code from beginning and I've made a solution for you:
#include <stdio.h>
int main(void)
{
int num, sum, remainder, check; // check used as a boolean expression
sum = check = 0;
printf("Enter the sequence: ");
scanf("%d", &num);
while (num > 0)
{
remainder = num % 10; // each time num is reduced
if (remainder != 9)
{
if (check == 0)
sum = (10 * sum) + (remainder + 1);
else
{
sum = (10 * sum) + (remainder + 2);
check = 0;
}
}
else
{
sum = (10 * sum) + 0;
check = 1;
}
num /= 10; // will divide and execute in each iteration until it's true
}
num = sum; // final number will be equal to the sum
sum = 0;
// Summing up the results
while (num > 0)
{
remainder = num % 10;
sum = (10 * sum) + remainder;
num /= 10;
}
printf("Result: %d\n", sum);
return 0;
}
Test Output
Enter the sequence: 23456
Result: 34567
It's just all about the sum & remainder. Hope it helps you understand better.
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int num,i=1,j;
Scanner scan=new Scanner(System.in);
int numo=scan.nextInt();num=numo;
for(;numo>0;numo=numo/10,i=i*10)
{
num=num+i;
if(numo%10==9)
num=num-i*10;
}
System.out.println(num);
}
}
The below solution uses the basic remainder and reverse approach:
int addOne(int n)
{
int rem, ans=0, p=1 ;
while(n>0)
{
rem = n%10;
(rem == 9)?rem = 0:rem+=1;
ans+=p*rem;
p*=10;n/=10;
}
return ans;
}
int main() {
int n;
cin>>n;
cout<<addOne(n);
return 0;
}

Why is my binary output different than the expected output?

I am having issue with my code giving me the correct output. For example, I am trying to do binary subtraction. The first test case is suppose to test 0x00CD - 0x00AB and the correct output is 0000000000100010 (0x0022). I am getting output of 0000000001100110 (0x0066), which is a few digits off. Can someone help me debug my problem?
#include <stdio.h>
int main(void)
{
int borrow, i, j;
int x[16] = {0}, y[16] = {0}, difference[16] = {0};
int n= 16;
unsigned int hex1, hex2;
scanf("%x %x", &hex1, &hex2);
i = 0;
while (hex1 != 0)
{
x[i] = hex1 % 2;
hex1 = hex1 / 2;
i = i + 1;
}
i = 0;
while (hex2 != 0)
{
y[i] = hex2 % 2;
hex2 = hex2 / 2;
i = i + 1;
}
borrow = 0;
for (i = 15; i >= 0; i--)
{
if(y[i] <= x[i])
{
difference[i] = (x[i] - y[i]);
}
else if (i == n - 1)
{
borrow = 1;
x[i] = x[i] + 2;
difference[i] = (x[i] - y[i]);
}
else
{
j = i + 1;
while (x[j] == 0)
{
j = j + 1;
}
x[j] = x[j] - 1;
j = j - 1;
while (j > i)
{
x[j] = x[j] + 2 - 1;
j = j - 1;
}
difference[i] = (2 + x[i] - y[i]);
}
}
for (i = 15; i >= 0; i--)
printf("%d", difference[i]);
return 0;
}
The logic for your borrow code is broken. When x[i] is greater than y[i] and i is not n-1, you go into this code that looks for a non-zero x[j] to borrow from. But the higher-indexed x[j] were already operated on (because i runs from 15 to 0). You should not be borrowing from them.
Usually subtraction proceeds from the low digits to the high digits (i from 0 to 15). Then, if we need to borrow, we calculate the current digit as if we borrowed, set a counter or flag to remember that we borrowed, and go on to the next digit, incorporating the borrow into the calculations for it.
Alternately, if working from high to low, then, when a borrow is needed, we need to take it from the previously calculated digits (in difference), not from the minuend (x). (And the code for that would have to be alert for running off the high end.)

C - Print all numbers with same count of set and unset bits

I have to print numbers with max N bits where count of bits set to 1 = count of bits set to 0. I ignoring leading zeros. I thinking that this applies only when count of bits is even.
My code:
int power(k) {
return 1 << k;
}
void print_numbers(int n){
n -= (n % 2); // FOR EVEN COUNT OF BITS
int exp = 1; // EXPONENTS WILL BE ODD (2^1, 2^3, 2^5, ...)
while (exp < n) {
int start = power(exp);
int end = power(exp + 1);
int ones = (exp + 1) / 2; // ALLOWED COUNT OF 1
for (int i = start; i < end; i++) {
int bits_count = 0;
for (int j = 0; j <= exp; j++){ // CHECK COUNT OF 1
bits_count += ((i >> j) & 1);
}
if (bits_count == ones){
printf("%d\n", i);
}
}
exp += 2;
}
For N = 12 this function print 637 numbers. Is this solution correct or am i wrong? Any idea for more efficient or better solution?
I came up with this, which is a totally different approach (and perfectible) but works:
#include <stdio.h>
void checker(int number)
{
int c;
int zeros = 0;
int ones = 0;
for (c = 31; c >= 0; c--)
{
if (number >> c & 1)
{
ones++;
}
else if(ones > 0)
{
zeros++;
}
}
if(zeros == ones)
{
printf("%i\n", number);
}
}
int main()
{
int c;
for (c = 4095; c >= 0; c--)
{
checker(c);
}
return 0;
}
Which get me 638 values (including 0)

Why this code works with visual studio on Windows 7 and doesnt work for linux correctly

long long int fun2(int a, int b, int m)
{
long long int res = 1;
long long int c = a % m;
for (int i = 1; i <= b; i <<= 1)
{
c = c % m;
if ((b & i) != 0)
{
res = res * c;
res = res % m;
}
c = c * c;
}
return res;
}
int fun(int num, int k)
{
srand((unsigned)time(NULL));
if (num <= 1)
{
return num * 10;
}
if (num == 2 || num == 3 || num == 5)
{
return num * 10 + 1;
}
if (num % 2 == 0)
{
return num * 10;
}
if (num % 3 == 0)
{
return num * 10;
}
if(num % 5 == 0)
{
return num * 10;
}
int s = 0;
int s_pow = 1;
while ((s_pow & (num - 1)) == 0)
{
s = s + 1;
s_pow = s_pow << 1;
}
int d = num / s_pow;
for (int i = 0; i < k; i++)
{
int a = (int)((num - 1) * rand() / (RAND_MAX + 1.0)) + 1;
if (fun2(a, d, num) != 1)
{
is_prime = false;
for (int r = 0; r <= s - 1; r++)
{
if (fun2(a, (1 << r) * d, num) == num - 1)
{
is_prime = true;
break;
}
}
if (!is_prime)
{
return num * 10;
}
}
}
return num * 10 + 1;
}
Where is a problem, maybe these long long int with int compares doesnt work correctly.
Compilation for windows and linus is without any warnings. It works but gives bad results for linux, for windows is ok. Please help.
#EDIT
I deleted code with INT_MIN and INT_MAX I just tried to fix the problem with this. (Sorry, should have delete it)
Problem SOLVED by myself !!!! Imagine that problem was in random a. I exchange this
int a = (int)((num - 1) * rand() / (RAND_MAX + 1.0)) + 1;
with this
int a = (int)(rand()%(num-1)) + 1;
and everything works perfect – user3144540

Resources