Thread: A problem

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    5

    A problem

    Hello everyone , i have started practicing c in codefoces and i can't get this code to work for the problem Problem - A - Codeforces
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int max(char tab[], int taille)
    {
        int c=0, indice_max=0;
    
    
        while(c < taille)
        {
            if(tab[c] > tab[indice_max])
                indice_max = c;
            c+=2;
        }
    
    
        return indice_max;
    }
    
    
    void echanger(char tab[], int x, int y)
    {
        int tmp;
    
    
        tmp = tab[x];
        tab[x] = tab[y];
        tab[y] = tmp;
    }
    tri(char tab[], int taille)
    {
        int indice_max;
    
    
        for(int taille; taille > 1 ; taille-=2)
        {
            indice_max = max(tab, taille);
    
    
            echanger(tab, taille-2, indice_max);
        }
    }
    
    
    
    
    int main()
    {
    
    
     char mot[100]="";
     scanf("%s",&mot);
     tri(mot[100],strlen(mot));
     char ch="";
     for (int j=2;j<strlen(mot);j+=1)
    {
      strcat(ch,mot[j]);
    }
    printf("%s",ch);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work? For example, if there is a compile error, what is the error message? If there is some kind of logic error, describe the context and show the test input, expected output, and actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    5
    A problem-sans-titre-png
    A problem-sans-titre-jpg
    A problem-sans-titre1-jpg
    For the problem's full description check the red link at the top of the thread.
    Last edited by Kerrons; 09-30-2018 at 08:26 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Before you even run the code, make sure you've fixed all the warnings.
    Code:
    $ gcc -Wall foo.c
    foo.c:32:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
     tri(char tab[], int taille)
     ^
    foo.c: In function ‘main’:
    foo.c:54:8: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat=]
      scanf("%s",&mot);
            ^
    foo.c:55:6: warning: passing argument 1 of ‘tri’ makes pointer from integer without a cast [-Wint-conversion]
      tri(mot[100],strlen(mot));
          ^
    foo.c:32:1: note: expected ‘char *’ but argument is of type ‘char’
     tri(char tab[], int taille)
     ^
    foo.c:56:10: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
      char ch="";
              ^
    foo.c:59:10: warning: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
       strcat(ch,mot[j]);
              ^
    In file included from foo.c:3:0:
    /usr/include/string.h:133:14: note: expected ‘char * restrict’ but argument is of type ‘char’
     extern char *strcat (char *__restrict __dest, const char *__restrict __src)
                  ^
    foo.c:59:13: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
       strcat(ch,mot[j]);
                 ^
    In file included from foo.c:3:0:
    /usr/include/string.h:133:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
     extern char *strcat (char *__restrict __dest, const char *__restrict __src)
                  ^
    foo.c:61:8: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
     printf("%s",ch);
            ^
    foo.c: In function ‘tri’:
    foo.c:44:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    It is not sufficient to just fix the errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    5
    i am new at c programming so actually i don't understand the meaning of the errors.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you should have stopped at the first warning you didn't understand and posted a question.
    Rather than carrying on digging an ever deeper hole to climb out of.

    > foo.c:32:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    > tri(char tab[], int taille)
    > ^
    > foo.c: In function ‘tri’:
    > foo.c:44:1: warning: control reaches end of non-void function [-Wreturn-type]
    > }
    Does tri return a value or not?
    If you don't specify a return type, the compiler assumes you mean int (that's the first message)
    Having assumed it returns int, it then complains you don't return a value.

    The best thing from looking at the code would be to make it return void.


    > foo.c:55:6: warning: passing argument 1 of ‘tri’ makes pointer from integer without a cast [-Wint-conversion]
    > tri(mot[100],strlen(mot));
    > ^
    > foo.c:32:1: note: expected ‘char *’ but argument is of type ‘char’
    > tri(char tab[], int taille)
    > ^
    To pass an array to a function, you just use the array name - ie, just mot.
    The second output (the note: ) is there to remind you what the function expects.


    Try those for starters, and think about what the other things might mean.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread