Comparing hashses in C [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Hi I am comparing the hashes and I can't get the correct output. So please can you guys help me out.
for(int i=0;i<len;i++)
{
if(hash1[i]==hash2[i])
{
return 1;
break;
}
else
return 0;
break;
}

You are using the return keyword to early, once the code hits a return, it goes out of the for-loop. So what you are doing there is comparing just the first element of both hashes.
The break statement also breaks the loop cycle, but in your code it never actually reaches this statement, because there is always a return before.
You should probably try something like:
for (int i=0; i<len; i++) {
if (hash1[i] != hash2[i]) {
return 0;
}
}
return 1;

Related

How to write character to an array of pointers in C? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hello this might be asked before, but I couldn't find an answer to my problem.
for (int i = 0; i < StrLenght - 1; i++) {
if (szPattern[i] == '%') {
// DO stuff....
} else {
szBuffer[i] = szPattern[i];
}
}
So, before the if statement everything seems to be alright, I mean szPattern is correctly written to szBuffer, but once I go into the if statement nothing is written in the szBuffer....
I suspect it is because of the null terminated string "\0"..
Any idea how I can solve this problem??
Thanks.. :)
You need
A separate index for writing to szBuffer
Code that adds a zero termination to szBuffer
Like:
int j = 0; // Index for storing in szBuffer
for (int i = 0; i < StrLenght - 1; i++) {
if (szPattern[i] == '%') {
// DO stuff....
} else {
szBuffer[j] = szPattern[i]; // Assign using index j
++j; // Increment index
}
}
szBuffer[j] = '\0'; // Zero terminate
puts(szBuffer);

Can I run a for loop inside a if statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Code written in c language trying to get desired output but else condition is not running
#include<stdio.h>
int main()
{
int i;
if (i <= 10)
{
for (scanf("%d", &i);i<=10;i++)
{
printf("%d\n",i);
}
}
else
{
printf("Please enter a valid number");
}
}
You are not initialized your variable named i. You must have to initialize it before using.

Friends Pair Algorithm Recursive solution in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Given n friends, each one can remain single or can be paired up with
some other friend. Each friend can be paired only once. Find out the
total number of ways in which friends can remain single or can be
paired up. Examples: Input : n = 3 Output : 4
Explanation: [{1}, {2}, {3} : all single ]
[{1}, {2,3}] : 2 and 3 paired but 1 is single]
similarly
[{1,2}, {3}]
[{1,3}, {2}]
finally answer 4
here I'm stuck to construct the recursion
int friends(int i)
{
if(i==0){
return 0;
}
if(i==1){
return 1;
}
friends(i)=friends(i-1)+(i-1)*friends(i-2);
}
For further reference : http://www.geeksforgeeks.org/friends-pairing-problem/
you are missing return statement
return friends(i-1)+(i-1)*friends(i-2);
and as code from link says, there is
if (i <= 2)
dp[i] = i;
and you are missing i == 2 in your code
for example this recursion works, I do not know what do you need to know more:
#include <stdio.h>
int friends(int i)
{
if(i<=2)
return i;
return friends(i-1)+(i-1)*friends(i-2);
}
int main()
{
printf("%d", friends(3));
return 0;
}

C code to find 3 digit numbers in an interval which satisfies the criteria sum of first and last=middle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have to find out how many 3 digit numbers are there in an interval [a,b] which satisfies criteria sum of first and last digit = middle digit . For eg: 121,143 etc.
Below is the code of loop for the same
for(int i=a;i<=b;i++)
{
first=a;
last=a%10;
temp=a/10;
middle=temp%10;
while(first>10)
{
first=first/10;
}
sum=first+last;
if(sum == middle)
{
count=count+1;
}
}
printf("%d",count);
But I am not getting the correct answer. Eg: in interval [100,130] , output should be 2 whereas I am getting 0 itself.
Please help out. Thanks .
for(int i=a;i<=b;i++)
{
first=i;
last=i%10;
temp=i/10;
middle=temp%10;
while(first>10)
{
first=first/10;
}
sum=first+last;
if(sum == middle)
{
count=count+1;
}
}
printf("%d",count);
just replace a with i in your loop.
for(int i=a;i<=b;i++)
{
first=i;
last=i%10;
temp=i/10;
middle=temp%10;
while(first>10)
{
first=first/10;
}
sum=first+last;
if(sum == middle)
{
cnt=cnt+1;
}
}
printf("%d",cnt);
This will give you correct result :)

Recursive function to get sequence not working correctly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to program a sequence x(n) in C with the following conditions:
x(0)=x
x(n)=1 if x(n-1)=1
x(n)=3*x(n-1)+1 if x(n-1)!=1 and x(n-1) not even
x(n)=x(n-1)/2 if x(n-1) even
I tried the following:
int sequence(int x, int n){
if(n==0){
return x;
}
if (sequence(x,n-1)==1){
return 1;
}
if((sequence(x,n-1)!=1)&&((sequence(x,n-1)%2)!=0)){
return 3*sequence(x,n-1)+1;
}
if((sequence(x,n-1)%2)==0){
return sequence(x,n-1)/2;
}
}
It should give me the n-th element of the sequence with the starting point x. However, it does not work...
Your code does what you proposed it to do. Maybe your original logic is flawed.
f(x,0)=x
f(x,n)=1 if f(x,n-1)=1
f(x,n)=3*f(x,n-1)+1 if f(x,n-1)!=1 and f(x,n-1) not even
f(x,n)=f(x,n-1)/2 otherwise
You can clean up your code somewhat to improve its readability and make it slightly faster/better.
int sequence(int x, int n){
if(n==0){
return x;
}
if (sequence(x,n-1)==1){
return 1;
}
if((sequence(x,n-1)%2)!=0){
return 3*sequence(x,n-1)+1;
}
return sequence(x,n-1)/2;
}

Resources