Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include <string.h>

void init(char a[])
{
    int i;
    for(i=0;i<26;i++)
    {
        a[i]='a'+i;
        printf("%c",a[i]);
    }
}

int fin(char c,char a[])
{
    int i;
    for(i=0;i<26;i++)
    {
        if(c==a[i])
            return i;
    }
}

int main()
{
    int t,i,l,temp1,temp2,coun=0;
    char a[26],s[1000];
    init(a);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        l=strlen(s);
        printf("DEBUGGING LINE 1\n");
        for(i=0;i<l;i++)>
        {
            temp2=fin(s[i],a);
            if(i=0)
                temp1=temp2;
            if(temp2>temp1)
                coun+=(temp2-temp1);
            if(temp2<temp1)>
                coun+=(25-temp2+temp1);
            temp1=temp2;
            printf("DEB %d\t",i);
        }
        if(coun<=l*11)
            printf("YES\n");
        else
            printf("NO\n");
    }
}


What I have tried:

when i run this code the i value in for loop is behaving unexpectedly.
I can't find the problem. Please help me.
Posted
Updated 12-Apr-16 21:30pm
v2
Comments
Peter_in_2780 13-Apr-16 3:06am    
In your loop in main() you have if(i=0) . That will set i to zero, then not execute the followinf statememnt, since 0 evaluates to false.
Jochen Arndt 13-Apr-16 4:23am    
Good catch!

You should post that as solution because it is what has been asked (value of i behaves unexpectedly).
Mircea Diaconescu 13-Apr-16 3:08am    
What do you mean by "the i value in for loop is behaving unexpectedly" ? And also which "i", since you have "i" in a "loop" all over your code: init, fin and main methods.

There may be other errors too but one is in the function fin() which does not always return a value:
C++
int fin(char c,char a[])
{
    int i;
    for(i=0;i<26;i++)
    {
        if(c==a[i])
            return i;
    }
    // Insert this line and handle the special return value when calling this function
    return -1;
}

Your code would return a random value when no match has been found. The compiler should throw a warning here that not all pathes return a value. If not, you should compile with a higher warning level (-W option with most compilers).

Another error is that the variable temp1 is not initialised but may be used before a value is assigned. This should also throw a compiler warning.
 
Share this answer
 
I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
 
Share this answer
 
1) The code contains syntax errors (possibly because of copy/paste typos): a) in the main
method, there is a ">" sign after "for(i=0;i<l;i+b)"; b) in the main method, there is a ">" sign after "if(temp2<temp1)". As someone else already pointed out: is time to compile and run your code and stop guessing what it should happen.

2) You don't say what means "the i value in for loop is behaving unexpectedly". The result after fixing the syntax errors is: "abcdefghijklmnopqrstuvwxyz". Is this what you expect?

3) Pay attention to the "if" blocks. Not using "{...}" means that only one instruction, that being exactly the next one, is executed.

4) The "fin" method does return a value ONLY when "c==a[i]". That is logically wrong, since you define the fin method as returning int, so it should return a value in any case. Possibly you can add a return "some int value" as the last line of this method, but sure this depends on the expected program logic.
 
Share this answer
 
Comments
Member 12455840 13-Apr-16 12:50pm    
thank you.
Mircea Diaconescu 13-Apr-16 13:19pm    
you are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900