Range queries for strings [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
My order is defined as 'A < a < B < b ...< Z < z'.
I have to find if a given string is in range or not.
Ex. If my range is AaA - BaB, AA or AaaB is inthe range, but not CbAA.
I am looking for any pointers, ideas, suggestions to help me start. I will implement this in C.

So all you need to implement is a single function that compares two strings according to your rules. It is kind of modified lexicograogical sorting:
int compare_letters(char x, char y) {
char lx = tolower(x);
char ly = tolower(y);
if (lx != ly) {
return lx < ly;
} else {
return x < y;
}
}
int smaller(const char* a, const char* b) {
.. use the above function ...
}
Now make use of the above function and to check if a given string x is in the range (a,b), check if smaller(a, x) and smaller(x, b). That's it.
Some tips on the function smaller - compare the strings char by char and if the two chars differ, return their compare_letter. If one of the strings runs out of letters, consider it smaller.

Related

Function to return a specific value in a given input range [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...

Why for loop does not run by other than int variables? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Why for loop does not run by other than int variables? I tried to run for loop by taking float variable but it does not run by any other variables than int type?
You can use a for loop with integer variables, floating-point variables, even no variables at all.
int i;
for(i = 0; i < 10; i++) continue;
float f;
for(f = 0.0; f < 5; f += 0.5) continue;
for(;;) break;
But see What Every Computer Scientist Should Know About Float-Point Arithmetic for why you should think twice before using example 2.
You should be able to do it by using the STEP command
float X = 0;
//
//increase in steps of 1 x 1 thousandth
for (X = 1; X <= 100; X += 0.001) {
// DISPLAY YOUR RESULT maybe using: Math.Round(X, 3)
}

Can't find a bug here... C code [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In this code I tried to write a function which returns 0 value if the two strings don't match, and a length of matching characters if i can find a substring in str that wholey resambles patt.
#include....
int check(char *str, char *patt, int *b)
{
if (*str == *patt && *patt != 0)
return *b * (1 + check(str+1,patt+1,&b));
else if (*patt == 0)
return 0;
else{
*b = 0;
return 0;
}
}
main()
{
char s1[SIZE] = "mama";
char s2[SIZE] = "mama";
int b = 1;
printf("%d\n",check(s1,s2,&b));
b = 1;
system ("pause");
return;
}
Here I should get the output 4, but I get -77779463.
Thanks for help!
PS I used recursion and set the b parameter as changable.
b is already a pointer to an int, so you want to make the recursive call with b and not &b.
Any decent compiler, with warnings enabled, would have alerted you to that mistake!
return *b * (1 + check(str+1,patt+1,&b));
^ dont pass address.
Pass b to it instead.

What is the best answer for finding the maximum sum possible in an array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The question is :
Find the maximum sum possible in an array of positive integers by selecting the elements in such a way that no two elements are next to each other.
there is an answer like this :
but what is the best answer for this question
Let's denote the array by "t" and index it from 0. Let f be a
function so that
f(k)=the maximal sum in the [0..k] subarray with the conditions of the problem.
Now use dynamic programming:
f(0) = t[0]
f(1) = max{ t[0], t[1] }
f(k) = max{ f(k-2) + t[k], f(k-1) } if k >= 2
If the array has n elements we need f(n-1).
Thanks in advance.
Solution you proposed is good one.
Similar approach (page 7 here):
Let m[i] be the maximum sum of any subarray that ends at the element a[i].Then
m[i] is simply max(a[i], m[i-1]+a[i]).
This is O(n).
and you cant get anything below O(n) as you have to visit every item of the array atleast once to compute the result.
Well, I think this is already the best answer.
Since you need O(n) to read in the data.
an O(n) algorithm is the fastest in the big-O notation.
public static int maxSum(int[] A){
return maxSum(A,0,1);
}
private static int maxSum(int[] A, int x, int y){
int c =0, d=0;
if(x<A.length){
c = A[x]+maxSum(A,x+2,x+3);
}
if(y<A.length){
d = A[y]+maxSum(A,y+2,y+3);
}
return c>d?c:d;
}

an efficent version similar to strstr [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
here is a general implementation
int stridx (char[] src, char[] str){
int i,j,k;
for(i=0;i < (src.len - str.len);i++){
for(j=i,k=0; str[k] != '\0' && str[k] == src[i]; j++,k++);
if( k> 0 && str[k]=='\0') return i;
}
return -1;
}
The worst case of the algorithm could be n^2, if we have aaaaaaaaaaaaaaaaaaaaaaaa (assuming both src and str are very long, and the length of them is very close).
Can I have a better algorithm?
You could use the Boyer-Moore algorithm, which is O(n). Here's sample C code.
It's not necessary to calculate the length of the strings :
char *strr(char *s1,char *s2)
{
int i,j;
for(i=0;s1[i];i++)
if(s1[i]==s2[0])
for(j=0;s1[i+j]==s2[j];j++)
if(!s2[j+1]) return &s1[i];
return NULL;
}
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm is another linear time algorithm. OTOH, the simple algorithms stay because in practise they are faster for most of the strings people actually search with.

Resources