Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:50:16+00:00 2026-05-29T09:50:16+00:00

I have this as my homework. Have a bit of trouble coding it. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:50:16+00:00Added an answer on May 29, 2026 at 9:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my homework, but please read my problem description first. I have to
I have a question about my C++ homework. I am just confused about *this.
This might seem a bit like a do-my-homework-for-me question (and it is), but I
I have this homework question: Assume a Database table has 0 records at time
I'm getting a bit confused with this homework i have to do for college,
I have this homework to do in C. I'm beginner so it is probably
(Before anyone says anything Yes this was homework but i have already turned it
(this is indirectly a part of a much larger homework assignment) I have something
I have a homework assignment to sort an array in ascending order. Obviously, this
I have this homework problem where I need to use regex to remove every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.