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


int compare_str(char info[][20]);
void swap(char info[20]);

int main()
{
    int n=0;
    int b,c,i,j;
    c =2;
    char tmp[20];
    char info[20][20]=
    {
        "christina",
        "victor",
        "chris",
        "chester",
        "elta",
        "kezia",
        "bew",
        "grace",
        "mavis",
        "tony",
        "oat",
        "adonique",
        "ploy"
    };
    for(b=0; b<13; b++)
    {
        printf("%d", c);
        c=compare_str(info[20]);
        printf("%d", c);
        if (c==1)
        {
            swap(info[20]);
        }
    }
    printf("Sorted list in ascending order:\n\n", info[20]);
    for(j=0; j<12; j++)
    {
        printf("%s\n",info[j]);
    }
    return 0;
    system("pause");

}


int compare_str(char info[][20])
{
    int i=0;
    int j=0;

    if(info[i][j]==info[i+1][j])
    {
        while(info[i][j]==info[i+1][j])
        {
            if (info[i][j]=='\0' && info[i+1][j]=='\0')
            {
                return 0;
                break;
            }
            i++;
            j++;
        }
        if (info[i][j]>info[i+1][j])
        {
            printf("should swap");
            return 1;
        }
        else if(info[i][j]<info[i+1][j])
        {
           printf("bla");
            return -1;
        }
    }
    if(info[i][j]<info[i+1][j])
    {
        return -1;
    }
    else if(info[i][j]>info[i+1][j])
    {
        printf("should swap");
        return 1;
    }


}


void swap(char info[20])
{
    printf("swap");
    char tmp[20];
    int i;
    for(i=0; i<=19; i++)
    {
        tmp[i]=info[i];
        info[i]=info[i+1];
        info[i+1]=tmp[i];
    }
}


What I have tried:

i tried to print something in code and test but i still don't know how to solve it
Posted
Updated 18-Dec-17 21:07pm
v2

C++
int compare_str(char info[][20])
void swap(char info[20])

Never put an array size in function declaration.
Your code is all wrong, you need to learn how to use pointers because you want to swap names in array on main from a subroutine.
C++
c=compare_str(info[20]);

How do you tell which names to compare?
C++
swap(info[20]);

How do you tell which names to swap?

Quote:
i tried to print something in code and test but i still don't know how to solve it

Advice: use the debugger to see what your code is doing.
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
CPallini 19-Dec-17 3:08am    
5.
Patrice T 19-Dec-17 3:22am    
Thank you
As suggested, use pointers, e.g.
C
#define ITEMS 13
//.....    
const char * info[ITEMS]=
{
  "christina",
   "victor",
   "chris",
   "chester",
   "elta",
   "kezia",
   "bew",
   "grace",
   "mavis",
   "tony",
   "oat",
   "adonique",
   "ploy",
};


Then, for instance, the swap operation becomes
C
void swap( const char * info [], int i, int j)
{
  const char * tmp = info[i];
  info[i] = info[j];
  info[j] = tmp;
}


And other opeartions, like compare and sort, might be implemented in a similar way.
 
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