this program is build to know all the huiwen number from 1 to 256
like 11^2=121 so 11 is a huiwen.
actually I have a good one
but I am confuse about why this program doesn't work well.
#include<stdio.h>
int is_huiwen(int l)
{
//DON'T WORK?WHY?
int number[20]={0};
int i,j,k;
int f=l*l;
for(j=0;f;j++)
{
number[j]=f%10;
f/=10;
}
for(k=0;j-k>0;k++,j--)
{
if(number[j]!=number[k] )
{
return 0;
}
}
return 1;
/* this is ok
int y=0;
int t = l*l;
int x = t;
do
{
y=y*10+t%10;
t /= 10;
}
while(t);
if(x==y)
return 1;
else
return 0;
*/
}
int main()
{
int i;
int flag;
for(i=0;i<256;i++)
{
flag=is_huiwen(i);
if(flag)
{
printf("%d is huiwen",i);
}
else
{
printf("\n");
}
}
printf("end\n\n\n\n");
return 0;
}
end.
The error is the if( number[j] != number[k] )line as pointed out by BluePixy.
The correction is if( number[j-1] != number[k] )
I would to point out that the code has hidden the problem partly because of a smattering of one letter variable names and inconsistent naming.
Here is the code that I used to find the same solution
int is_huiwen_2(int L )
{
int ret = 1;
int j = 0;
int k = 0;
int t = L * L;
int number[8] = { 0 };
for( j = 0; t; j++)
{
number[j] = t % 10;
t /= 10;
}
for( k = 0; (j - k) > 0 && ret; k++, j-- )
{
if( number[j-1] != number[k] )
{
ret = 0;
}
}
return( ret );
}
int is_huiwen_1( int L )
{
int ret = 1;
int y = 0;
int t = L * L;
int x = t;
do
{
y = y * 10 + t % 10;
t /= 10;
}
while( t );
if( x != y )
{
ret = 0;
}
return( ret );
}
int main()
{
int i = 0;
int flag = 0;
for( i = 0; i < 256; i++)
{
flag = is_huiwen_1(i);
if(flag)
{
printf("1: %d is huiwen\n",i);
}
}
for( i = 0; i < 256; i++)
{
flag = is_huiwen_2(i);
if(flag)
{
printf("2: %d is huiwen\n",i);
}
}
printf("end\n");
return( 0 );
}
I don't know what all the variables do. The use of 'l' looks like a '1' in some editors.
Being a Code Refactoring monk or zealot, I did some modifications such as reducing the code to one consistent return condition, value and point of return.
While it might seem overly simple, having multiple declarations on one line hides trouble, such as int i not being used in the second function.
The use of int numbers[20] = { 0 }; was also misleading us as for the range of values here [20] was overkill, having an explanation of what the range of values "12321", "1234321" where could have meant easier debugging of this code. A simple comment of what the outcome should look like would have solved several problems.
// A Huiwen number looks like this "12321", "1589851"
While this might be a simple exercise it portends greater problems down the line if this was not a trivial exercise. Try not to make it hard on the next coder.
Document your algorithms, arithmetic and variables please.
Related
I know I'm asking pretty basic question here, but could anyone give me a hint how to properly loop through an array which is initialized in another function ? I tried googling, found tons of videos but I didn't manage to get it right just yet. Would anyone please help me find out, what I'm missing in my code ? I'm a struggling beginner. Thanks in advance for your time.
My code (not functioning):
#include <stdio.h>
#define ARR_RANGE 1000000
#define GREATEST_NUMBER 1000000
void sieve(int eratosthenes[]);
int main()
{
int eratosthenes[ARR_RANGE];
int n = 999;
int c;
for(i = 2; i <= arrLen; ++i)
{
if(eratosthenes[i]!= -1)
{
int c = 0;
while(n % i == 0)
{
n /= i;
++c;
}
if(c >= 2)
{
printf("%d^%d x ", i, c);
}
else if(c == 1)
{
printf("%d x ", i);
}
}
else
continue;
}
return 0;
}
void sieve(int eratosthenes[])
{
for(int i = 1; i < GREATEST_NUMBER; ++i)
{
eratosthenes[i] = i;
}
for(int i = 2; i*i < GREATEST_NUMBER; ++i)
{
if(eratosthenes[i] != -1)
{
for(int j = 2*i; j < GREATEST_NUMBER ; j += i)
eratosthenes[j] = -1;
}
}
int arrLen = sizeof eratosthenes / sizeof eratosthenes[0];
}
In the main is not visible the call of function sieve,so the array is not passed into function, to do this you have to write in the main sieve(eratosthenes); (Passage by reference)
I really have no clue why this won't compile. Some feedback would be appreciated. If possible, I would just like small hint or a point in the right direction instead of giving me the answer. Thanks much, folks.
The purpose of this code is as follows:
./a.out R S
R is the number of rolls
S is the seed for srand
if not supplied, R is 30 S is 13
can supple R&S, R, none
roll 2 dice; frequency of the sum of 2 values
use 11 sided dice
output should be well formatted & informative
#include <stdio.h>
#include <stdlib.h>
int main( int argc, int argv[] )
{
int dice1, dice2, sum;
int R, S, k, g, j, x, count = 0;
int list[22];
int nums[22] = {0};
for( j = 2; j < 23; j++ )
{
list[j] = j;
}
if( argc != 3 )
{
R = 30;
S = 13;
}
else
{
R = argv[1];
S = argv[2];
}
srand( S );
for( k = R; k > 0; k-- )
{
dice1 = rand() % 11 + 1;
dice2 = rand() % 11 + 1;
sum = (dice1 + dice2);
for( g = 2; g < 23; g )
{
if( sum == list[g] )
{
nums[count] += 1;
count++;
}
}
}
count = 0;
for( x = 2; x < 23; x++ )
{
printf("Frequency of %d: %d\n", x, nums[count]);
count++;
}
return 0;
}
You should be able to do it yourself. It's more than basic. Check the comments.
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[] ) // argc is number of arguments, argv[][] is list of arguments that are strings
{
int dice1, dice2, sum;
int R, S, k, g, j, x, count = 0;
int list[23]; // increase size of these arrays as you will go out of bounds
int nums[23] = {0};
for( j = 2; j < 23; j++ ) // j==22 is out of bounds, if you need j==22, increase `list' and `nums' size (above)
{
list[j] = j;
}
if( argc != 3 )
{
R = 30;
S = 13;
}
else
{
R = atoi(argv[1]); // convert string to int
S = atoi(argv[2]);
}
srand( S );
for( k = R; k > 0; k-- )
{
dice1 = rand() % 11 + 1;
dice2 = rand() % 11 + 1;
sum = (dice1 + dice2);
for( g = 2; g < 22; g++ ) // do something with g, like increment for example, as it's infinite loop
{
if( sum == list[g] )
{
nums[count] += 1;
count++;
}
}
}
count = 0;
for( x = 2; x < 23; x++ )
{
printf("Frequency of %d: %d\n", x, nums[count]);
count++;
}
return 0;
}
Here is a diff that will hopefully help you to see the difference. You can get it yourself with $ diff -rup rst1.c rst2.c > rst2.diff.
PS. I think all things had been pointed in the comments so you should give thumb up to everyone of them.
Let a row of 8000 lamps. Initially, only the one located to the left is lit.
Then, every second, the following operation is performed: each lamp changes state (on or off) if the one on its left was lit a second before. The leftmost lamp stays on all the time. This operation is instantaneous.
The process stops when the lamp at the right end lights for the first time.
How many lights are on?
My following implementation of the problem is false, can you help me?
#include <cstdio>
int t[8001][2];
int main()
{
t[1][0] = 1;
t[1][1] = 1;
int cpt1 = 0, ip = 0;
while (t[8000][0] != 1 && t[8000][1] != 1)
{
ip++;
for (int j=2;j<8001;j++)
{
if(t[j-1][!(ip&1)])
t[j][(ip & 1)] = !t[j][!(ip & 1)];
}
}
for(int j = 1;j < 8001; j++)
cpt1 += t[j][1];
printf("cpt=%d\n", cpt1);
}
Code is missing an update when the left does not change.
Code simplified (zero based offset, use of bool) and corrected below
#include<stdbool.h>
#include<stdio.h>
#define N 8000
bool t[N][2];
int main(void) {
t[0][0] = true;
t[0][1] = true;
int ip = 0;
while (t[N - 1][0] == 0 && t[N - 1][1] == 0) {
ip = !ip;
for (int j = 1; j < N; j++) {
if (t[j - 1][!ip]) {
t[j][ip] = !t[j][!ip];
} else {
t[j][ip] = t[j][!ip]; // add
}
}
}
int cpt1 = 0;
for (int j = 0; j < N; j++) {
cpt1 += t[j][1];
}
printf("N=%d cpt=%d\n", N, cpt1);
return 0;
}
Output
N=8000 cpt=2048
the following proposed code:
cleanly compiles
uses C header files rather than C++ header files
performs the desired operation, but not the fastest possible algorithm
is liberally commented
And now the proposed code:
#include <stdio.h>
int t1[8000]; // initially all zeros
int t2[8000];
int main( void )
{
// setup initial conditions
int numLitLights = 0;
t1[0] = 1;
// while stop condition not true
while ( t1[7999] != 1 )
{
// make one pass through lamps
// update values
for (int j=0; j<7999; j++)
{
if( t1[j] )
{
t2[j+1] = ( t1[j+1] )? 0 : 1;
}
}
// update original
for( int j=0; j< 8000; j++ )
{
t1[j] = t2[j];
}
}
// count lit lamps
for(int j = 0; j < 8000; j++)
{
if( t1[j] )
{
numLitLights++;
}
}
// output number of lit lamps
printf( "number of lit lamps: %d\n", numLitLights );
} // end function: main
The result (number of lamps lit) is
1024
I was trying to solve Project Euler question 16 using c. I did not use bignnum libraries. The question asks 2^1000. I decided to store every digit of that number in an array.
For Example: 45 means arr[0]=4, arr[1]=5;
The problem is definitely i the function int multi.
#include<stdio.h>
#include<conio.h>
int multi(int *base, int k);// does the multiplication of array term by 2
void switcher();//switches every term when the fore mostvalue is >10
int finder();// finds the array address of last value
int arr[1000];
int summer();//sums all values of the array
int main()
{
arr[1000] = { 0 };
arr[0] = 1;
int i, j, sum, k, p;
for (i = 0; i < 1000; i++)
{
j = 0;
k = finder();
p = multi(arr + k, j);
}
sum = summer();
printf("sum of digits of 2^1000 is %d", sum);
_getch();
}
int multi(int *base, int k)
{
int p;
if (base == arr)
{
*base = *base - 1;
*base = *base + k;
if (*base > 10)
{
*base = *base - 10;
switcher();
}
return 0;
}
*base = *base * 2;
*base = *base + k;
if (*base > 10)
{
*base = *base - 10;
p = multi(base - 1, 1);
}
else
{
p = multi(base - 1, 0);
}
}
void switcher()
{
int j;
for (j = 0;; j++)
{
if (arr[j] == 0)
{
break;
}
}
j--;
for (; j > 0; j--)
{
arr[j + 1] = arr[j];
}
arr[0] = 1;
}
int finder()
{
int j;
for (j = 0;; j++)
{
if (arr[j] == 0)
{
break;
}
}
return --j;
}
int summer()
{
int summ, i;
summ = 0;
for (i = 0; i<1000; i++)
{
summ = summ + arr[i];
if (arr[i] == 0)
break;
}
return summ;
}
It compiles but during runtime it shows Access Write Violation, base was ......
Please explain this error and how to resolve it ?
Array is of 100 Bytes but you are looping for 1000. Also in function Finder() , you do not have a limit on variable j so your array size is going beyond 100 bytes.
Also use memset to assign array variables to 0.
As said in the comments, 2^1000 has 302 decimal digits.
You're going far outside your array.
But your code is very complicated because you store the digits with the most significant one first.
Since you're only interested in the digits and not the order in which they would be written, you can store the number "in reverse", with the least significant digit first.
This makes the code much simpler, as you can loop "forwards" and no longer need to shuffle array elements around.
Using -1 as "end of number" marker, it might look like this:
void twice(int* digits)
{
int i = 0;
int carry = 0;
while (digits[i] >= 0)
{
digits[i] *= 2;
digits[i] += carry;
if (digits[i] >= 10)
{
carry = 1;
digits[i] -= 10;
}
else
{
carry = 0;
}
i++;
}
if (carry)
{
digits[i] = 1;
digits[i+1] = -1;
}
}
int main()
{
int digits[302] = {1, -1}; /* Start with 1 */
twice(digits); /* digits is now { 2, -1 } */
return 0;
}
The code is trying to find the largest palindrome made from the product of two 2-digit numbers. The answer is 91*99 = 9009 but I keep getting 990, which is not even a palindrome. I really appreciate the help!
#include <stdio.h>
int main()
{
int i = 10;
int j = 10;
int a = 0;
int b = 0;
int array[100] = {0};
int divider = 10;
int num;
int great;
int product;
int n;
int flag;
/*Loop through first 2 digit number and second 2 digit number*/
while (i<100)
{
while (j < 100)
{
product = i*j;
array [a] = product % 10;
n = product / divider;
while (n != 0)
{
a++;
num = n%10;
divider *=10;
array[a]=num;
n = product/divider;
}
flag = 0;
while (b<a)
{
if (array[b] != array[a])
{
flag = 1;
}
b++;
a--;
}
if (flag == 0)
{
great = product;
}
j++;
a = 0;
b = 0;
}
i++;
}
printf("The largest palindrome is %d \n", great);
return 0;
}
Here is a code snippet you can try.
#include <stdio.h>
void main()
{
int a = 1; // first integer
int b = 1; // second integer
int currentNumber;
int currentPalin; if a palindrome is found, its stored here
while (a<100){ //loop through the first number
while (b<100){ // loop through the second number
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){ //check for palindrome
currentPalin = currentNumber;
}
b = b+1; //increment the second number
}
b = a; // you could have set b=1 but it would not be an efficient algorithm because
//some of the multiplication would occur twice. eg- (54*60) and (60*54)
a = a +1; //increment the first number
}
printf ("Largest palindrom is %d \n", currentPalin);
getchar();
}
// method for finding out reverse
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
// when you divide a number by 10, the
//remainder gives you the last digit. so you are reconstructing the
//digit from the last
n = n/10;
}
return reverse;
}
Update:- As suggested by M Oehm, I have modified the code to make it more general.
#include <stdio.h>
void main()
{
int a = 1;
int b = 1;
int currentNumber;
int currentPalin=0;
while (a<100){
while (b<100){
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){
if (currentNumber>currentPalin){
currentPalin = currentNumber;
}
}
b = b+1;
}
b = 1;
a = a +1;
}
if (currentPalin==0){
printf("No Palindrome exits in this range");
}
else {
printf ("Largest palindrome is %d \n", currentPalin);
}
getchar();
}
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
return reverse;
}
An alternative approach to solve the problem.
#include<stdio.h>
int reverse(int num)
{
int result = 0;
while( num > 0)
{
result = result * 10 + (num%10);
num/=10;
}
return result;
}
int main()
{
int last_best = 1;
int best_i=1;
int best_j = 1;
const int max_value = 99;
for( int i = max_value ; i > 0 ; --i)
{
for(int j = i ; j > 0 ; --j){
int a = i * j;
if( last_best > a )
break;
else if ( a == reverse(a) )
{
last_best = a;
best_i = i;
best_j = j;
}
}
}
printf("%d and %d = %d\n", best_i,best_j,last_best);
}
And it is quite simple to follow.
It seems that you do not reinitialize variables at the beginning of loop. They keeps values from previous iterations. For example, j and divider. Put
j = 10;
before starting "j" loop, i.e.:
j = 10;
while (j < 100) ...
The same for divider:
...
j = 10;
while (j < 100) {
divider = 10;
...
If you were using for loops you would avoid this problem naturally:
for(i=10; i<100; i++) {
for(j=10; j<100; j++) {
...
}
}