This is a problem where you have to enter a number having non zero digits and the program will convert it into a IP address having 4 parts and each part less than 255, and have to print all the IP addresses.
I have tried this recursive method and am running into an infinite loop.
#include<stdio.h>
#include<math.h>
int a[3];
void comuni(unsigned long n,int count){
int i=count;
do{
if(count<0){
return;
}
int t=pow(10,i);
a[count]=n/t;
int rem=n%t;
if(a[count]<=255 &&a[count]>0 && count>=0){
printf("%d.",a[count]);
comuni(rem,count-1);
}
i++;
}while(1);
}
int main()
{
// Insert your code here.
unsigned long n;
scanf("%ul",&n);
comuni(n,3);
return 0;
}
Your question is why there is an infinite loop.
Starting with a look at while(1) it does not surprise me.
But wait, there is a return inside, which is used if count is less than 0.
Nothing inside the loop changes count. So if count is greater or equal 0 to begin with you got your infinite loop.
I think the problem which causes that situation is that this
int i=count;
does NOT cause count to change when you change i.
You do change i inside the loop, but you know - it does not help.
Related
My code generates 5 random numbers and I want the user to guess these numbers after 5 seconds of flashing it.
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
main()
{
menuchoose1();
}
int menuchoose1(){
int menu1choose;
int score=0;
int mode,i,j;
int n=5;
printf("1.Continuous Mode\n");
printf("2.Stage Mode\n");
scanf("%d",&menu1choose);
switch(menu1choose){
int answers;
case 1:
srand(time(NULL)* getpid() );
int a[5];
unique(a,5,10,99);
int i;
printf("You have 5 seconds to remember these numbers\n");
for(i=0;i<5;i++)
printf("%d\t",a[i]);
sleep(5);
system("cls");
scanf("%d",&answers);
if(answers==a[i]){
printf("Correct");
}else
printf("Incorrect");
break;
}
return;
}
void unique(int array[], int length, int min, int max){
int new_randomnum;
bool unique;
int i;
for(i=0;i<length;i++){
do{
new_randomnum = (rand()%(max - min + 1 )) + min;
unique = true;
int j;
for(j=0;j<0;j++)
if(array[j] == new_randomnum) unique = false;
}while(!unique);
array[i] = new_randomnum;
}
}
I've tried using scanf but it always ends up incorrect and generating it one by one then checking it one by one would be inefficient.
First off, this program produces a ton of compiler warnings. I strongly suggest you get in the habit of enabling warnings, and never ignore them! For instance, you are missing a lot of headers and forward declarations. If your compiler does not give you those warnings, then you should consider switching to a better one.
Below I will only mention those bugs that do not trigger warnings.
srand(time(NULL)* getpid() );
If you call srand on each round of the game, you risk getting the same sequence of numbers again. Just call it once at the start of the program, e.g. in main.
for(i=0;i<5;i++)
printf("%d\t",a[i]);
sleep(5);
system("cls");
scanf("%d",&answers);
if(answers==a[i]){
printf("Correct");
}else
printf("Incorrect");
break;
I fixed the indentation to emphasize the bug. The if(answers == a[i]) is after the loop and therefore i always has the value 5 when it is reached, which is out of bounds for the array a, causing undefined behavior. You probably want the code to get and check the user's answer to be in a loop of its own.
for(j=0;j<0;j++)
if(array[j] == new_randomnum)
unique = false;
The j<0 condition means this loop will never execute. You probably meant j<i.
Detail on one part of OP's code.
srand(time(NULL)* getpid() ); is a weak way to initialize the random number generator.
Example: if either of the least significant bits of time(NULL) or getpid() are 0, the product with have a least significant bit of zero. Thus that bit has a 75% change of being 0.
Instead, call srand() once, before the loops and use the 2 function calls like: (unsigned) time(NULL) ^ getpid(). By using ^, the bits will not get biased as discussed above.
What's the differences between the next three codes.
1:
struct node
{
int data;
}tree[100050];
/*
some codes
*/
int main()
{
int tot;
scanf("%d",&tot);
int tmp;
for(int i=0;i<=tot;++i){
scanf("%d",&tmp);
tree[i].data=tmp;
insert(i,1);
}
}
Wrong Answer
2:
struct node
{
int data;
}tree[100050];
/*
some codes
*/
int main(){
int n;
scanf("%d",&n);
int tmp;
for(int i=0;i<=n;++i){
scanf("%d",&tree[i].data);
insert(i,1);
}
}
Accepted
3:
struct node
{
int data;
}tree[100050];
/*
some codes
*/
int main()
{
int tot;
scanf("%d",&tot);
int tmp;
for(int i=0;i<=tot;++i){
scanf("%d",&tmp);
tree[i].data=tmp;
insert(i,1);
tmp=0;
}
}
Accepted
The first codes can not pass all tests, but next two codes can pass all tests.
The problem is here POJS024。It is written in Chinese.
Input is a list of number which builds a Binary Sort Tree,the first number is the root.
Output the In-Order Traversal and the Post-Order Traversal。
All three use wrong logic. You got lucky in the second and third, probably because insert has some special handling of values that are equal to 0.
The mistake in all three is that you are using
for(int i=0;i<=n;++i){
instead of
for(int i=0;i<n;++i){
^^^ Needs to be < not <=
If you had code to check the return value of scanf, you would have caught your error sooner.
if ( scanf(...) == 1 )
{
insert(i,1);
}
That's why it's very important that you make a habit of checking the return value of scanf family of functions ALWAYS to make sure that you were able to read all data that you were expecting to.
The problem (which actually exists in some sense in all three cases) is that you call scanf() one too many times because you're using a <= comparison in the for loop instead of the using the < comparison. As a result the last call to scanf() actually fails. and you call insert(i,1) one more time than it should be called.
In example 1 that results in tree[i].data being set to the last value that was read into tmp, in examples 2 and 3 that value is left at zero. Apparently the extra call to insert(i,1) doesn't cause a problem in that particular scenario.
#include<stdio.h>
int p(int);
void main()
{
int j;
scanf("%d",&j);
p(j);
printf("Return value: %d",j);
}
int p(int j)
{
int i;
for( i=j; i>0; p(i-1) )
{
printf("%d",i);
}
return(j);
}
Please explain the dry run and output as well. I have trouble finding the logic behind this function.
The function p() will never return on positive input. In the loop, i is not modified, therefore the condition i > 0 always holds true for a positive input. With a negative or zero input, a single line will be printed: "Return value: x" where x is your input value.
Without knowing what output you are expecting, we really can't help you improve your code.
may be this will give an error because the function p() return int value so you need to store return value in any int variable.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int prime(long long int);
long long int *arr; //array to hold n prime numbers
int main()
{
int i,count=4;;
long long int n;
scanf("%lli",&n);
arr=malloc(sizeof(long long int)*n);
arr[0]=2;
arr[1]=3;
arr[2]=5;
arr[3]=7;
if (n==1) printf("%lli",arr[0]);
else{ if (n==2) printf("%lli",arr[1]);
else{ if (n==3) printf("%lli",arr[2]);
else{ if (n==4) printf("%lli",arr[3]);
else
{
for(i=2;count<n;i++)
{
if(prime(6*i-1)) { /*As prime nos are always 6k+1 or
arr[count]=6*i-1; 6k-1fork>=2 I checked only for those*/
count++; }
if(prime(6*i+1)&&count<=n) {
arr[count]=6*i+1;
count++; }
}
printf("%lli",arr[count]);
}}}}
//free(arr);
return 0;
}
int prime(long long int x)
{
int j=1,flag=1;
while(arr[j]<=sqrt(x))
{
if (x%arr[j]==0)
{
flag=0;
break;
}
j++;
}
return flag;
}
The code is working only for n=1,2,3,4, i.e i=0,1,2,3 for which the values are explicitly given. For n=5 onwards it is giving 0 as O/P
There is some glitch related to the global dynamic array as free(arr) is giving core dump error.
Q: Is this the right way to declare a global dynamic array? What could be the problem in this code?
Thank You in advance.
If that is your actual code you have 4 bugs:
2 line comment scopes out a line of your code
the second if should check count < n not count <= n as if count == n you cannot write to arr[count]
You cannot print arr[count] only arr[count-1] which is probably what you mean
In the case where n is less than 4 you still set arr[1], arr[2] and arr[3] which may be out of bounds
It is of course also inefficient to call sqrt(x) in every loop iteration, potentially you should call it outside and there may be a potential rounding issue bug due to the way square roots are calculated, so you might prefer:
while( arr[j] * arr[j] < x )
It would be preferable not to make this global and to pass it into your function.
It would also be preferable to move the main loop logic of your program outside of main().
I'm surprised you say you program works for n=1, 2 and 3 as it looks like you are setting out of bounds.
Your counter goes beyond the size of the array. Specifically both conditions (6i-1 and 6i+1) are met for i=2, and therefore counter is incremented twice, resulting in using arr[5] where you only allocated 5 places in the array. This is because you check counter<=n and not counter
Not sure this could be also be the reason for free creating a core dump, but it is possible (because once corrupting the memory, free may access corrupted data).
Problem : Consider the following algorithm to generate a sequence of
numbers. Start with an integer n. If n is even, divide by 2. If n is
odd, multiply by 3 and add 1. Repeat this process with the new value
of n, terminating when n = 1. The input will consist of a series of
pairs of integers i and j, one pair of integers perline. All integers
will be less than 1,000,000 and greater than 0.
For each pair of
input integers i and j, output i, j in the same order in which they
appeared in the input and then the maximum cycle length for integers
between and including i and j. These three numbers should be separated
by one space, with all three numbers on one line and with one line of
output for each line of input.
sample input :
1 10
sample output:
1 10 20
so i wrote this :
#include <stdio.h>
#include <string.h>
struct line{int in1;int in2;int result;};
int cycle(int in);
int main(int argc, char *argv[]) {
int cycle(int in);
char c;
int firstIn=0;
struct line l[500] ;
int pointer=0;
while(2<3){
l[pointer].in1=0;
l[pointer].in2=0;
scanf("%u %u",&l[pointer].in1,&l[pointer].in2);
if(l[pointer].in1<1||l[pointer].in2<1){
break;
}
int maxCyc=0;
int j,m;
int min,max;
if(l[pointer].in1>l[pointer].in2){
max=l[pointer].in1;
min=l[pointer].in2;
}
else{
max=l[pointer].in2;
min=l[pointer].in1;
}
for(j=min;j<=max;j++){
m = cycle(j);
if(m>maxCyc)
maxCyc=m;
}
l[pointer].result=maxCyc;
printf("%d %d %d\n",l[pointer].in1,l[pointer].in2,l[pointer].result);
pointer++;
}
}
int cycle(int in){
int cyc = 1;
while(in>1){
if(in%2==0){
cyc++;
in=in/2;
}
else{
cyc++;
in=in*3+1;
}
}
return cyc;
}
Its completly ok but when you change while(in>1) in cycle method to while(in!=1) it gets much more slower. my question is why?!
Time when its while(in>1) : 0.683 sec
and when its while(in!=1) : I waited more than 5 min nothing
happened yet :)
for input : 1 1000000
there is no infinite loop or something because in cant get below 1 at all(for that it must be already 1) .
Best regards
When you call cycle with the input value 113383, the process eventually sets n to
827370449, and 3*827370449+1 is 2482111348, which is greater than the maximum signed int and is interpreted as -1812855948. So there's your first negative number where there should be no negative number.
If this process then eventually sets n to -2, it will loop infinitely between -2 and -1 from then on. There may be other loops I haven't considered.
If you were to use an unsigned int, there is a possibility (I haven't checked) that this too will overflow eventually, which will not result in a negative value but will result in an incorrect value, invalidating your results.
No matter what integer representation you use, it would probably be a good idea to compare n with (maximum-1)/3 at the top of each loop, where maximum is the largest possible positive value of your integer type, just to be sure you do not overflow.
As you told me it was a simple overflow problem thx everyone.
max int value is 2,147,483,647; So when i changed int cycle(int in) to int cycle(long long int in) my problem was solved.
i also figured it out that my first answer with while(in>1) was wrong.
When an integer overflow occurs,the value will go below 0 .That was the reason while(in!=1) was an infinte loop.
I was really tired that i didn't figure it out by myself. sorry for that :)