Click here to Skip to main content
16,020,080 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
#include <stdlib.h>

void swap(char *ap,char *bp,int x);
void compare(char *ap,char *bp);
int main()
{
    int b,x,y,j,damn;
    int i=0;
    char info[13][20]=
    {
        "christina",
        "victor",
        "chris",
        "chester",
        "elta",
        "kezia",
        "bew",
        "grace",
        "mavis",
        "tony",
        "oat",
        "adonique",
        "ploy"
    };
    char *ap;
    ap=info[i];
    char *bp;
    bp=info[i+1];
    for(b=0; b<13; b++)
    {
        while(i<13)
        {
            compare(ap,bp);
            i++;
        }
        for(y=0; y<13; y++)
        {
            printf("%s\n",info[y]);
        }
        return 0;
    }
}

void compare(char *ap,char *bp)
{
    int i=0;
    int x;
    int damn;
    if(*ap==*bp)
    {
        int j=1;

        while (*(ap+j)==*(bp+j))
        {

            if (*(ap+j) == '\0' && *(bp+j) == '\0')
            {
                damn=0;
                break;
            }
            j++;
        }
        if (*(ap+j)<*(bp+j))
        {
            damn=1;
        }
        if (*(ap+j)>*(bp+j))
        {
            damn=-1;
        }
    }
        if (*ap<*bp)
        {
            damn=1;
        }
        if (*ap>*bp)
        {
            damn=-1;
        }
        if (damn==-1)
        {

            while(*(ap+x) != '\0' || *(bp+x) != '\0')
            {
                swap(ap,bp,x);
                x++;
            }
        }
}

void swap(char *ap,char *bp,int x)
{
    char tmp[20];
    for(x=0; x<19; x++)
    {
        printf("k");
        *tmp=*ap;
        *ap=*bp;
        *bp=*tmp;
    }
}


What I have tried:

i tried to print k in swap function but it didn't show
Posted
Updated 19-Dec-17 4:43am

1 solution

If "k" doesn't get printed, then swap is not being called.
If swap isn't being called, then the body of the while loop in compare is never being executed.
That means that the condition is not being matched.

So why not? That's the question.

And that's up to you to find out - debugging is a part of your homework: writing the code is the easy bit, the skill is in getting it to work afterwards!
So, its going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the compare function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

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