2

I have this as my homework. Have a bit of trouble coding it. I am using C as the language.

Question:

Mastermind is a game of two players. In the beginning, first player decides a secret key, which is a sequence (s1,s2,...sk) where 0 < si <= n, Then second player makes guesses in rounds, where each guess is of form (g1,g2, ...gk), and after each guess first player calculates the score for the guess. Score for a guess is equal to number of i's for which we have gi = si.

For example if the secret key is (4,2,5,3,1) and the guess is (1,2,3,7,1),then the score is 2, because g2 = s2 and g5 = s5.

Given a sequence of guesses, and scores for each guess, your program must decide if there exists at least one secret key that generates those exact scores.

Input:

First line of input contains a single integer C (1 <=C <= 100). C test-cases follow. First line of each test-case contains three integers n,k and q. (1 <=n,k <=11, 1<=q<=8). Next q lines contain the guesses.

Each guess consists of k integers gi,1, gi,2,....gi,k separated by a single space, followed by the score for the guess bi (1 <= gi,j <=n for all 1 <=i <=q, 1 <=j <=k; and 0 <= bi <=k )

Output:

For each test-case, output "Yes" (without quotes), if there exists at least a secret key which generates those exact scores, otherwise output "No".

The code I wrote is this:

#include <stdio.h>
#include <string.h>
#include <stdarg.h>  
#include <stdlib.h>
#include <stdbool.h>

typedef struct set_t{    
    int count;     
    void **values; 
} *SetRef;

SetRef set_create(void *values, ...); 
int  set_count(SetRef this);
bool set_contains(SetRef this, void *value);  
int rscore(SetRef set1, void *value, int score);


int main(int argc,char **argv )
{
    int t = 0, n = 0 ,k = 0,q = 0, score = 0;
    char ch;
    int arr1[n];
    int arr2[n];
    printf("Please enter the number of test cases(between 1 to 100):");
    scanf("%d",&t);
    printf("\n");
    for ( int i = 1; i<=t;i++)
    {
        printf("Please enter values of n,k, q:");
        scanf("%i %i %i",&n, &k, &q);

        printf("\n");
        printf("Enter the values of secret key");
        score = 0;
        for ( int c = 0 ; c < n ; c++ )
        {
            scanf("%d",&arr1[c]);
         }
        printf("\n");
        printf("Enter the values of guess");
            for ( int c = 0 ; c < n ; c++ )
            {
                scanf("%d",&arr2[c]);
             }

        }
       SetRef set1 = set_create(&arr1); 
       SetRef set2 = set_create(&arr2); 
       for ( int i = 0; i < set2->count; i++){
            void *val = set2->values[i]; 
            score = rscore(set1, val,score);
        }        
        if ( score == set1->count)
            printf("Yes");
        else
            printf("No");
        printf("\n");
    }
}

SetRef set_create(void *values, ...) 
{   
    SetRef set = calloc(1, sizeof(struct set_t));    
    if (values)   
    {        
        int count = 1;     
         va_list args;       
         va_start(args, values);       
        while (va_arg(args, void *)) 
        {             
            count++;    
        }          
        va_end(args);       
        set->count = count;      
        set->values = calloc(count, sizeof(void *));      
        set->values[0] = values;    
        va_start(args, values);       
        int i = 1;         
        void *val;       
        while ((val = va_arg(args, void *)))
        {            
            set->values[i++] = val;      
        }       
        va_end(args);    
    }      
    return set; 
}  
int set_count(SetRef this) 
{    
    return this->count; 
} 
bool set_contains(SetRef this, void *value) 
{    
    for (int i = 0; i < this->count; i++)
    {      
        if (value == this->values[i])   
            return true;   
    }    
    return false;
}  

int rscore(SetRef set1, void *value, int score){
    void *val = value;
    if (set_contains(set1, val))
        score ++;
    return score;
}  

I have these errors:

solution.cc:60:1: error: expected declaration before ‘}’ token

Besides that, is my logic correct? Have I made any major mistakes?

Not sure how to solve this. Need some guidance.

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
lakshmen
  • 28,346
  • 66
  • 178
  • 276

3 Answers3

4

You are compiling C source code as C++. C and C++ are different languages.

Rename your file to solution.c and NOT solution.cc, and make sure to compile with a C compiler (e.g., gcc and NOT g++)

Edit: Actually, it looks like the source code is neither C nor C++. Pick one and stick with it. It is almost C, you would need to change #include <cstdarg> to #include <stdarg.h> and you would need to add #include <stdbool.h> for the bool type. You will also need to change YES and NO to true and false.

The bool type might not be available if you use Microsoft's MSC compiler, which uses an outdated version of C.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
2

In C++ this is a keyword used as a pointer to the current object inside class member functions.

You have to use some other name that is not reserved by the language.

Or, if this is actually C code, not include C++ headers like <cstdarg> and rename the files from .cc to .c.

Choose your language!

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
1

You can't use this as a variable name in C++. It's a language keyword. If you want to compile a C program, don't use a C++ compiler.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469