1

I'm trying to understand how the return value of a function works, through the following program that has been given to me,

It goes like this :

  • Write a function that given an array of character v and its dim, return the capital letter that more often is followed by its next letter in the alphabetical order.

And the example goes like : if I have the string "B T M N M P S T M N" the function will return M (because two times is followed by N).

I thought the following thing to create the function:

I'm gonna consider the character inserted into the array like integer thank to the ASCII code so I'm gonna create an int function that returns an integer but I'm going to print like a char; that what I was hoping to do,

And I think I did, because with the string BTMNMPSTMN the function prints M, but for example with the string 'ABDPE' the function returns P; that's not what I wanted, because should return 'A'.

I think I'm misunderstanding something in my code or into the returning value of the functions.

Any help would be appreciated,

The code goes like this:

#include <stdio.h>
int maxvolte(char a[],int DIM) {    
    int trovato;
    for(int j=0;j<DIM-1;j++) {
        if (a[j]- a[j+1]==-1) {
            trovato=a[j];
        }   
    }
    return trovato;
}

int main()
{
    int dim;
    scanf("%d",&dim);
    char v[dim];
    scanf("%s",v);
    printf("%c",maxvolte(v,dim));
    return 0;
}

P.S

I was unable to insert the value of the array using in a for scanf("%c,&v[i]) or getchar() because the program stops almost immediately due to the intepretation of '\n' a character, so I tried with strings, the result was achieved but I'd like to understand or at least have an example on how to store an array of character properly.

Any help or tip would be appreciated.

18
  • 1
    If you are using scanf you need to allocate dim+1 bytes for the array to leave room for the string terminator char \0. Commented Oct 28, 2018 at 11:51
  • 1
    This if (a[j]- a[j+1]==-1); in fact does nothing. Mind the trailing ;. I recommend to learn using a debugger to trace your code. Commented Oct 28, 2018 at 11:51
  • 2
    Also for the sake of readability, please intend your code properly. Commented Oct 28, 2018 at 11:52
  • 1
    Some example input you show with blanks between the letters other you do not. Is this by intention? Commented Oct 28, 2018 at 11:56
  • 1
    @alk - hehe. Intend or Indent?! :p Commented Oct 28, 2018 at 11:57

2 Answers 2

2

There are a few things, I think you did not get it right. First you need to consider that there are multiple pairs of characters satisfying a[j] - a[j+1] == -1 . Second you assume any input will generate a valid answer. That could be no such pair at all, for example, ACE as input.

Here is my fix based on your code and it does not address the second issue but you can take it as a starting point.

#include <stdio.h>
#include <assert.h>
int maxvolte(char a[],int DIM) {
    int count[26] = {0};

    for(int j=0;j<DIM-1;j++) {
        if (a[j] - a[j+1]==-1) {
            int index = a[j] - 'A'; // assume all input are valid, namely only A..Z letters are allowed
            ++count[index];
        }
    }

    int max = -1;
    int index = -1;
    for (int i = 0; i < 26; ++i) {
        if (count[i] > max) {
            max = count[i];
            index = i;
        }
    }

    assert (max != -1);
    return index + 'A';
}

int main()
{
    int dim;
    scanf("%d",&dim);
    char v[dim];
    scanf("%s",v);
    printf("answer is %c\n",maxvolte(v,dim));
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

-1
#include <stdio.h>

int maxvolte(char a[],int DIM) { 
    int hold;
    int freq;
    int max =0 ;
    int result;
    int i,j;

    for(int j=0; j<DIM; j++) {

       hold = a[j]; 
       freq = 0;

       if(a[j]-a[j+1] == -1) {
           freq++;
       }

       for(i=j+1; i<DIM-1; i++) {       //search another couple
           if(hold==a[i]) {
              if(a[i]-a[i+1] == -1) {
                freq++;
              }
           }
       }

       if(freq>max) {       
            result = hold;
            max=freq;
       }       
}
    return result;
}

int main()
{
    char v[] = "ABDPE";
    int dim = sizeof(v) / sizeof(v[0]);
    printf("\nresult : %c", maxvolte(v,dim));
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.