i build a program that translate base 10 ti base 2 and base 16:
#include <stdio.h>
int main(void)
{
int b10, b2, b16;
scanf("%d", &b10);//getting a number in base 10
b16 = b10;
b2 = b10;
//******print the number in base 2
int* ba2 = (int*)malloc(b10/2*4);
int i = 0,j;
while (b2 > 0){
ba2[i] = b2 % 2;
b2=b2 / 2;
i++;
}
for (j = i-1; j >= 0; j--){
printf("%d", ba2[j]);
}
free(ba2);
//**************************
//******print the number in base 16
printf("\n");
int* ba16 = (int*)malloc(b10 / 16 * 4);
i = 0;
while (b16 > 0){
ba16[i] = b16 % 16;
b16 = b16 / 16;
i++;
}
for (j = i - 1; j >= 0; j--){
if (ba16[j] < 10)
printf("%d", ba16[j]);
else
printf("%c", 'A' + (ba16[j] - 10));
}
free(ba16);
//****************************
getch();
return 0;
}
for some reason the program stop at the second free().
when i created a break point the program just stoped when i got to the free, no msg or warning.
can someone help me with is?
You are allocating memory as ba10/16 * 4. For input values less that 16, that evaluates to zero. As per malloc documentation, you will either get null pointer or a unique pointer which can be passed to free. But you are assigning values in ba16[0]. Same holds true for input value of 16. you have allocated 4 bytes. But the program goes on to assign ba16[0] and ba16[1], which will corrupt the array. The same problem exists with ba2 as well. I don't know if you tested with input values less than 16 or 2. Hope this helps.
Related
Problem: To display the sum of this pattern for n terms like 1+11+111+1111+11111..n terms
Test Data:
Input the number of terms: 5.
Expected Output:
1 + 11 + 111 + 1111 + 11111
The Sum is : 12345
I am trying this way->
//To display the sum of series like 1+11+111+11111
#include <stdio.h>
int
main(void){
//Here i declared some variables for storing information
int number,iteration,value=1,j,summation=0;
//Message to user
printf("Input the number of terms : ");
//taking input from the user
scanf("%d",&number);
//this condition will work till the iteration reaches to the inputted number
for(iteration=1; iteration<=number; iteration++){
for(j=1; j<=iteration; j++){
//To display the series like 1 11 111 1111 11111
printf("%d",value);
if(j==1){
summation=summation+value;
}
else if(j==2){
summation=summation+value*10;
}
else if(j==3){
summation=summation+value*100;
}
else if(j==4){
summation=summation+value*1000;
}
else if(j==5){
summation=summation+value*10000;
}
}
printf(" ");
}
printf("\n");
//To display the summation
printf("The summation is : %d",summation);
return 0;}
Now my problem is: This code does not work according to my expectation. It is working up to input value 5. But when I want to give input 6 times then I need to add an else if condition additionally in my code. I need to do this task whenever I increase the input value.
When the input value is 6 and i need to add and make the condition like that->
else if(j==6){
summation=summation+value*100000;
}
So I think, this is not the way of a proper solution to a problem. Every time I need to do the same thing for the inputted value. How can I solve this problem?. After that how can I simplify the solution? I believe that you guys are expert than me. Please share your knowledge with me. Thank you in advance.
Pass the input number to this function.
int findSum(int n)
{
int sum=0, cnt= 1;
for (int i = 1; i <= n; i++) {
sum += cnt;
cnt = (cnt * 10) + 1;
}
return sum;
}
If you want to make this work for large N (say, 1,000 or 20,000,000), you won’t be able use int or long long values. Instead, you could allocate an array of uint8s, and do your own digit-by-digit addition arithmetic, including the carry operation. Then print the results at the end. It wouldn’t be fast but it would work.
To keep your code simple, think right-to-left. Start with the least significant digit in the zero-th array element.
Here's an example that uses uint64_t to represent larger numbers. It shows the output you want for 1 up to 20 digits (longer causes an overflow).
The trick is to generate the numbers 1, 11, 111, and so on from the previous one by multiplying by 10 and adding 1. For example, 11111 = 1111 * 10 + 1.
#include <inttypes.h>
#include <stdio.h>
void sum(int n) {
uint64_t t = 0;
uint64_t x = 1;
for (int i = 0; i < n; i++) {
if (i > 0) printf(" + ");
printf("%" PRIu64, x);
t += x;
x = (x * 10) + 1;
}
printf(" = %" PRIu64 "\n", t);
}
int main() {
for (int i = 1; i < 21; i++) {
sum(i);
}
}
Here's a version that works for any n. It computes the total in time linear in n, although printing the terms being summed necessarily requires O(n^2) time.
The code works by noting that the last digit of the total consists of n 1s being added, the next-to last n-1 1s and so on. Plus carry of course. Note that the result is always exactly n digits long.
#include <stdio.h>
#include <stdlib.h>
void sum(int n) {
for (int i = 1; i <= n; i++) {
if (i > 1) printf(" + ");
for(int j = 0; j < i; j++) putchar('1');
}
printf(" = ");
char *s = malloc(n + 1);
s[n] = '\0';
int t = 0;
for (int i = n - 1; i >= 0; i--) {
t += i + 1;
s[i] = '0' + (t % 10);
t /= 10;
}
printf("%s\n", s);
free(s);
}
int main() {
sum(50);
}
Output (wrapped):
1 + 11 + 111 + 1111 + 11111 + 111111 + 1111111 + 11111111 + 111111111 + 1111111111 + 11111111111 + 111111111111 +
1111111111111 + 11111111111111 + 111111111111111 + 1111111111111111 + 11111111111111111 + 1111111111111111 11 +
1111111111111111111 + 11111111111111111111 + 111111111111111111111 + 1111111111111111111111 + 11111111111111111111111 +
111111111111111111111111 + 1111111111111111111111111 + 11111111111111111111111111 + 11111111111 1111111111111111 +
1111111111111111111111111111 + 11111111111111111111111111111 + 111111111111111111111111111111 +
1111111111111111111111111111111 + 11111111111111111111111111111111 + 111111111111111111111111111111111 +
1111111111111111111111111111111111 + 11111111111111111111111111111111111 + 111111111111111111111111111111111111 +
1111111111111111111111111111111111111 + 11111111111111111111111111111111111111 + 1111111111111111111111111
11111111111111 + 1111111111111111111111111111111111111111 + 11111111111111111111111111111111111111111 +
111111111111111111111111111111111111111111 + 1111111111111111111111111111111111111111111 + 1111111111111111111111111
1111111111111111111 + 111111111111111111111111111111111111111111111 + 1111111111111111111111111111111111111111111111 +
11111111111111111111111111111111111111111111111 + 111111111111111111111111111111111111111111111111 +
1111111111111111111111111111111111111111111111111 + 11111111111111111111111111111111111111111111111111 =
12345679012345679012345679012345679012345679012340
For handling numbers greater than int/long limits, you can use an array to get the sums per digit and print the output as a string.
#include <stdio.h>
int
main (int argc, char *argv[])
{
int n, i, j;
scanf("%d", &n);
char ones[n];
char sum[n + 1]; // + 1 index in case of a carry out
char output[n + 2]; // +1 more index than sum for null byte
// initialize to 0s
for (i = 0; i < n; i++) {
ones[i] = sum[i] = output[i] = 0;
}
sum[n] = output[n] = output[n+1] = 0;
for (i = 0; i < n; i++) {
ones[i] = 1;
output[i] = '1';
for (j = 0; j <= i; j++) { // add the current number of ones to sum
sum[j] += ones[j];
if (sum[j] >= 10) { // if theres a carry
sum[j + 1] += (sum[j] / 10); // add the carry to the next index
sum[j] %= 10; // keep the last digit
}
}
if (i == n - 1) {
printf ("%s ", output);
} else printf ("%s + ", output);
}
if(sum[n] == 0) {// leading digit is 0
i = n - 1;
} else i = n;
for (j = 0; i >= 0; i--, j++) {
output[j] = sum[i] + '0';
}
printf ("The sum is: %s\n", output);
return 0;
}
Given your user input variable number, the code may look something like this:
//...
if (number < 0)
{
// do some error handling
return -1;
}
int value_to_add = 1;
int sum = 0;
while (number--)
{
sum += value_to_add;
value_to_add = value_to_add * 10 + 1;
}
// ... (result is in "sum")
You also may consider the possibility of overflow (when the result gets so big that it does not fit in an int). You could, for instance, limit the user input (number).
glad to help!
(it seems like a homework, so hope you can learn something)
You're doing this with many 'if's to decide how much it should plus. And another way is to use *10+1 every time.
Please see the code:
#include <stdio.h>
long long sum,tmp=1,n;
int main(void){
scanf("%lld",&n);
for(int i=0;i<n;i++){
if(i<n-1)printf("%lld + ",tmp);
else printf("%lld ",tmp);
sum+=tmp;
tmp=tmp*10+1;
}
printf("= %lld",sum);
return 0;
}
That's it.
Wish you a good day:)
If I understood correctly you want to be able to programmatically add new terms without having to use an if statement. To do it I suggest you
for (j=0; j<=iteration; j++){
int powerOf10 = (int) pow((double) 10,j); //power elevation: notice 10^0=1, 10^1=10..
summation+=value*powerOf10;
}
This was just to give you an idea. Obviously, this code can be further refined.
If you don't understand all the casting I performed to compute powerOf10 I leave you this post: Why is my power operator (^) not working?
Paul Hankin's answer shows how to solve this problem for values of n greater than the number of digits storable in a long long.
That approach could be combined with another, based on a simple observation. If we write the sum starting from the greatest number, we can note an emerging pattern.
111111111111111111111111111111111111111111111...111 +
11111111111111111111111111111111111111111111...111 +
...
1111111111111111111111111111111111111...111 =
----------------------------------------------------
123456789999999999999999999999999999999999999...999 +
111111111111111111111111111111111111...111 =
----------------------------------------------------
123456790111111111111111111111111111111111111...110 +
11111111111111111111111111111111111...111 +
...
1111111111111111111111111111...111 =
----------------------------------------------------
123456790123456789999999999999999999999999999...998 +
111111111111111111111111111...111 =
----------------------------------------------------
123456790123456790111111111111111111111111111...109 +
11111111111111111111111111...111 +
...
1111111111111111111...111 =
----------------------------------------------------
123456790123456790123456789999999999999999999...997 +
111111111111111111...111 =
----------------------------------------------------
123456790123456790123456790111111111111111111...108 +
^ ^ ^ ^ ...
In practice, we can start by "filling" the number (represented as a string of n characters) with the repeating pattern "123456790" from left to right (the most significant digit beeing always '1').
Then, starting from the least significant digit, we can apply the algorithm of the sum with carry, but only as long as the calculated digit is different from the one already there (except the last one, which is always n % 10).
Only a few steps are needed, just around the number of decimal digits of n.
I do not know why, gcc version 4.9.2 (Ubuntu 4.9.2-10ubuntu13 x86_64)
Breakpoint 1, convertToTitle (n=1000000001) at excel_sheet_column_title.c:14
14 printf("%d\n", n);
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400697 in convertToTitle (n=1000000001) at excel_sheet_column_title.c:14
14 printf("%d\n", n);
(gdb) p n
$1 = 1000000001
The complete code of the function, just called the function with 1000000001 in the main function:
char *convertToTitle(int n)
{
int mod, idx, last = n / 26 + 1;
char str[last], *title, *tp;
printf("%d\n", n);
idx = last;
while (n > 26) {
mod = n % 26;
if (mod > 0) {
str[--idx] = mod - 1 + 'A';
} else if (mod == 0) {
str[--idx] = 'Z';
n -= 1;
}
n /= 26;
}
if (n > 0) {
str[--idx] = n - 1 + 'A';
}
title = (char *)malloc((last - idx + 1) * sizeof(char));
tp = title;
for (; idx < last; idx++) {
*tp++ = str[idx];
}
*tp = '\0';
return title;
}
Your last is very large. Move it outside of local function (or mark it static) to avoid segfault.
As an alternative (and correct) solution, calculate correct value of last.
(I think you wanted log26n + 1)
26last >= nlast = log26n
last = ceil(log(n) / log(26)) + 1;
Weak calculation of needed buffer size for str[] resulted in an excessively large array size last of 1000000001/26 + 1. This array size was unsupportable as coded as a local variable.
What is needed is a much smaller array about log26(n).
There is little need to "right-size" the buffer per various values of int n. Simply use a constant size that works for INT_MAX.
As the bit size of an int is about log2(INT_MAX)+1,
#include <limits.h>
#define ABOUT_LOG2_26 4.7
#define BUF26_SIZE ((int)(sizeof(int)*CHAR_BIT/ABOUT_LOG2_26 + 2))
char *convertToTitle(int n) {
char str[BUF26_SIZE];
...
// Also cope with negative values of n
if (n < 0) Handle_Error();
As far as I can tell, in C all the numeric types have a fixed upper limit. Therefore, to convert a string to a number, you have to know how big the number could possibly be.
Is there any way to convert strings to numbers without placing any kind of limit on the size of the numbers? In case it matters, the numbers I care about are negative.
The core C language only supports integers of a definite size. The stock facility for converting decimal (text) numbers to binary (machine) numbers is the strto* family of functions1 and, as you have probably already noticed, they require you to choose an appropriately-sized integer type for the input you expect. Normally, when programming in C, it's possible to say that your program only needs to be able to support numbers in some fixed range, and just raise an error if you receive input outside that range.
If you truly need to support arbitrarily large2 numbers, then you need an add-on library. The general terms for these libraries are "bignum", "multiple precision arithmetic", and "arbitrary precision arithmetic". One well-written, freely-licensed bignum library is GNU MP.
1 The related ato* and *scanf functions are broken as designed - never use them for anything. One of the ways they are broken is that they make it impossible to tell when you've received input outside the supported range.
2 in absolute value, i.e. arbitrarily far away from zero in either direction
here's my attempt , I'm working with a maximum of 64bit int (but you can change the type to whatever it is that you like) with 8 offset , meaning if you surpass 8bit (ex. 257), it'll output 16 bits , if you surpass 16 bits it'll output 24bits ... etc, I also used the first bit as the sign bit 1 for negative and 0 for positive;
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <math.h>
void btd(int64_t num , char* res);
int act_size(int64_t num);
int main(void)
{
char res[64];
btd(-200 , res);
printf("%s\n" , res);
}
void btd(int64_t num , char* res)
{
int64_t tmp;
int neg = 0;
int size = 0;
int64_t one_check;
int i;
if(num < 0)
neg++;
if(num < 0)
tmp = num * -1;
else
tmp = num;
size = act_size(tmp);
one_check = pow(2 , size - 1);
printf("size %d\none flag : %ld\n" , size , one_check);
for( i = 0 ; i < size + 1; i++)
{
if(!i)
{
if(neg)
{
neg = 0;
res[0] = '1';
num <<= 1;
}
else
{
res[0] = '0';
num <<= 1;
}
continue;
}
if(tmp & one_check)
{
res[i] = '1';
tmp <<= 1;
}
else
{
res[i] = '0';
tmp <<= 1;
}
}
res[i] = '\0';
}
int act_size(int64_t ar)
{
int count = 1;
int last_one;
int size;
int64_t num = ar;
if(num < 0)
num *= -1;
while(num)
{
printf("NUM : %ld\n" , num);
if(num & 1)
{
last_one = count;
num >>= 1;
}
else
num >>=1;
count++;
}
printf("NUM : %ld\nLAST : %d\n" , num , last_one);
if(last_one <= 8)
return 8;
else if (last_one <= 16)
return 16;
else if (last_one <= 24)
return 24;
else if (last_one <= 32)
return 32;
else if (last_one <= 40)
return 40;
else if (last_one <= 48)
return 48;
else if (last_one <= 56)
return 56;
else
return 64;
}
the output of this will be (since we gave it -200 as an argument)
NUM : 200
NUM : 100
NUM : 50
NUM : 25
NUM : 12
NUM : 6
NUM : 3
NUM : 1
NUM : 0
LAST : 8
size 8
one flag : 128
111001000
I am hoping someone can help me with this. I am a complete and utter C newbie.
This is for a school assignment in a class on C (just plain old C, not C# or C++), and the professor is insistent that the only compiler we're allowed to use is Borland 5.5.
The general assignment is to run an algorithm that can check the validity of a credit card number. I've successfully gotten the program to pick up the user-input CC number, then portion that number out into an array. It prints out mostly what I want.
However, when I entered the last function (the one I commented as such) and then compiled, the program just started to hang. I have no idea what could be causing that.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//global variables declared.
//in an earlier version, I was going to use multiple functions, but I couldn't make them work
float array[16];
double num, ten;
int i, a, b, x, y, check;
int main()
{
ten = 10;
//pick up user-input number
printf("Enter your credit card number\n>");
scanf("%lf", &num);
//generate the array
for (i = 15; i >= 0; i--)
{
array[i] = fmod(num, ten);
num /= 10;
printf("Array is %1.1lf\n", array[i]);
}
//double every other number. If the number is greater than ten, test for that, then parse and re-add.
//this is where the program starts to hang (I think).
{for (i = 2; i <= 16; i + 2)
{
array[i] = array[i] * 2;
if (array[i] >= 10)
{
a = (int)array[i] % 10;
b = (int)array[i] / 10;
array[i] = a + b;
}
}
printf("%f", array[i]);
}
//add the numbers together
x = array[2] + array[4] + array[6] + array[8] + array[10] + array[12] + array[14] + array[16];
y = array[1] + array[3] + array[5] + array[7] + array[9] + array[11] + array[13] + array[15];
check = x + y;
//print out a test number to make sure the program is doing everything correctly.
//Right now, this isn't happening
printf("%d", check);
return 0;
}
for (i = 2; i <= 16; i + 2)
should be
for (i = 2; i <= 16; i = i + 2)
or
for (i = 2; i <= 16; i += 2)
As you have it, the value of i is never modified, so the loop never terminates.
You declare your array
array[16] so array[0] .. array[15]
In the second for loop you have
when i = 16 array[16]!
valter
I had an exam, and I've been struggling ever since.
You have an array of integers(ex. 13, 6, 21, 4), and I need to make an output that looks like:
13 = 2^3 + 2^2 + 2^0
6 = 2^2 + 2^1
21 = 2^4 + 2^2 + 2^0
4 = 2^2
here's what i've got so far.
#include <stdio.h>
#define MAX 100
int main() {
int niz[MAX], nizb, n, i, ones, k;
while(1) {
printf("Array length: ");
scanf("%d", &n);
if (n<=0 || n>MAX) break;
printf("Array elements: ");
for(i=0;i<n;i++){
scanf("%d", &niz[i]);
if (niz[i] <=0) {
printf("Error! Wrong value. Enter new one: ");
scanf("%d", &niz[i]);
}
}
for(i=0;i<n;i++) {
nizb = niz[i];
ones = 0;
for(k=0; k < 16; k++) {
//What should i do here?
}
}
}
}
I'm stuck here. I dont know how many bits should i use, and how does C sees those bits of integer. I'm using var 'k' to add to a string that is in format '2^3 + 2^2 ...', where k is the value of 'for' iteration. I have made an assumption that length of the integer is 16, but im really not sure since we do this on a sheet of paper.
I want to say BIG THANKS TO EVERYONE!!!
You can calculate how many bits to use by using the sizeof operator and CHAR_BIT:
int bitsPerInt = sizeof(int) * CHAR_BIT;
CHAR_BIT is definied in limits.h.
After you have that limit, you can use the bitwise & operator to extract each bit:
for (k = bitsPerInt - 1; k >= 0; k--)
{
if (nizb & (1U << k))
// output
else
// don't
}
I'll leave the details up to you.
Aside: It looks like you're trying to use niz as an array, but you haven't declared it as one. Does this code even compile? Also, the return value of main should be int.
Not sure what this has to do with twos-complement (which is a particular way of representing negative numbers). What you are trying to do is express an integer as a sum of powers of 2, apparently. Here's the way I'd do it, which isn't necessarily better or worse than the other answers...
void powersum(int n)
{ int powers[sizeof(int) << 3];
int i;
char *sep = "";
printf("%d = ", n);
powers[0] = 0;
for (i = 0; n; n >>= 1, ++i)
powers[i] = n & 1;
while (--i >= 0)
{ if (powers[i])
{ printf("%s2^%d", sep, i);
sep = " + ";
}
}
printf("\n");
}
EDIT: Here's another version that doesn't use the stack-allocated array, but as a tradeoff has to go around the loop more (once for each bit, as opposed to only looping until the highest 1-bit is found):
void powersum2(int n)
{ int i = (sizeof(int) << 3) - 2;
int m = 1 << i;
char *sep = "";
printf("%d = ", n);
while (m)
{ if (n & m)
{ printf("%s2^%d", sep, i);
sep = " + ";
}
m >>= 1;
--i;
}
printf("\n");
}
This is complete conjecture, since I'm not really good with math, but I think I'd go about it like this:
int potency = 0, base = 1;
while(base < NumberInQuestion) {
base *= 2;
++potency;
}
After the loop finishes, you'll know the highest potency which still fits into 'Number'.
Number -= base/2; //Removes the base you just calculated from the number.
printf("2^%d", potency);
Rinse and repeat, until Number falls to 0, which should be at 2^0 at latest.
For your use-case, the code may look somewhat like this:
for(i=0; i < n; ++i) {
int Number = niz[i];
while(Number > 0) {
int potency = 0, base = 1;
do { //Executes at least once, making a number of '1' possible.
base *= 2;
++potency;
} while(base < Number);
Number -= base/2; //Reverts the last step you made, making the 'base' smaller than 'Number'.
printf("2^%d", potency);
}
}
There's a possible alternative, which can give you a more complete picture of things and will save you iterations. For this we use a two-step process.
for(i=0; i < n; ++i) {
int Number = niz[i];
int potency = 0, base = 1;
do { //Executes at least once, making a number of '1' possible.
base *= 2;
++potency;
} while(base < Number);
base /= 2; //Reverses the last iteration.
//At this point, we know the maximum potency, which still fits into the number.
//In regards of base 2, we know the Most Significant Bit.
while(base > 0) {
Number -= base; //Removes the MSD (Most significant digit)
printf("2^%d", potency); //Prints your '1'.
while(base > Number) { //Executes at least once.
base /= 2; //Goes back one potency. (Ends at '0' latest.)
--potency; //For each potency (except for first), it's a '0'.
}
}
}
quotient = niz[i];
int k=0,c[MAX];
while(quotient!=0){
binaryNumber[i++]= quotient % 2; //this will convert your numbers to binary form
quotient = quotient / 2; //and store in reverse in array
}
for(j = 0 ;j<i;j++)
{
if(binaryNumber[j]==1) */e.g binary of 4 is stored in array as 001 ie 1 atpos2*/
{ c[k]=j;
k++;}
}
while(k--)
printf("2^%d +",c[k]);
If you can tolerate a GCC-dependency, a hack on #twalberg's solution get's really nice and small ;)
void powersum(int n)
{
char *sep = "";
printf("%d = ", n);
while (n) {
int pos = 31 - __builtin_clz(n);
printf("%s2^%d", sep, pos);
sep = " + ";
n ^= 1 << pos;
}
printf("\n");
}