0

Im trying to make a game where it players can answer math equations in a 2D way, almost like sudoku. The operands need to be randomised for each new games aswell as the operators. Ive gotten to the point where I can make it in a 1D array, but cant figure out how to solve both column and rowwise functions.. could anyone make some suggestions to point in the right direction..

i can upload my code if that helps.. Thanks in advance.

    int by4equations (int num[2][2] ) {
    

    srand( time(0) ); 
    num [2][2] = {
                {(rand() % 9+1),(rand() % 9+1)},
                {(rand() % 9+1),(rand() % 9+1)} 
                    } ;

    int operator, sum = 0 ; 
    int i, j ;
        
    operator = rand()%4 ;  
}
    if (operator == 0)
        for(i = 0; i < 2; i++){
            for(j = 0; j < 2; j++){
        sum = num[i][j]  + num[i][j];
        }}

    else if (operator == 1){
        for(i = 0; i < 2; i++){
            for(j = 0; j < 2; j++){
        sum = num[i][j] * num[i][j];
        }}}


    else if (operator == 2){
        for(i = 0; i < 2; i++){
            for(j = 0; j < 2; j++){
        sum = num[i][j] / num[i][j] ;   
        }}}

    else if  (operator == 3){
         for(i = 0; i < 2; i++){
            for(j = 0; j < 2; j++){
        sum = num[i][j] - num[i][j];
        }}}

edit : something like this is what i want https://www.google.com/url?sa=i&url=http%3A%2F%2Fwww.tamaraladamsauthor.com%2Fday-15-stuck-at-home-free-activity-math-square%2F&psig=AOvVaw3ohQWT7Hen-ewfXGvKSYVf&ust=1630711269359000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCJDq6oO34fICFQAAAAAdAAAAABAD

lemon
  • 1
  • 1
  • 2
    Maybe show a sample 2D problem set so we don't have to imagine what it looks like. – Jerry Jeremiah Sep 02 '21 at 23:04
  • "*i can upload my code if that help*". Yes please. Please ensure it is not just a full code dump but a minimal example that illustrates just the problem and no other unrelated code. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – kaylum Sep 02 '21 at 23:05
  • @kaylum: Please do not ask for code for everything. This is not a debugging question, and showing the code is likely not going to be very helpful. More likely, a sample of what they want in the two-dimensional array will be helpful. E.g., a matrix with a number in each cell and an operator (“+”, “−”, “×”, etc.) at each border between cells, possibly “=” at the ends. I suspect OP needs help more with conceptualizing the matrix, the data structures needed for it, and algorithms to generate it than with any C code they currently have. – Eric Postpischil Sep 02 '21 at 23:27
  • @EricPostpischil Thanks for your opinion. I don't agree. Showing the code so far will arguably make it clearer regarding what has been tried and what the objective is. – kaylum Sep 02 '21 at 23:32
  • I know what i have to do, i am just having trouble with placing things where they need to be or how they work, ie. how to add the 2d array in both columwise and row wise and still be solvable if that makes sense – lemon Sep 02 '21 at 23:35
  • thats kinda the point of why im asking my question, i cant get the 2d array version, that is above to compile – lemon Sep 02 '21 at 23:44
  • 1
    @lemon: Re “I know what i have to do”: It does not help that **you** know what you have to do. For us to understand the problem and recommend solutions **we** need to know what you have to do. You have to explain it. – Eric Postpischil Sep 03 '21 at 00:37
  • @lemon In your words, what is `num [2][2] = { {(rand() % 9+1),(rand() % 9+1)}, {(rand() % 9+1),(rand() % 9+1)} } ;` suppose to do? – chux - Reinstate Monica Sep 03 '21 at 00:41
  • Okay, you want to make a matrix where there are interleaved numbers and operators, plus an auxiliary row and an auxiliary column where there are results of the operations. What parts of that do you need help with? Can you make a two-dimensional array? Can you express the array coordinates that will contain numbers, the array coordinates that will contain operators, and the array coordinates that will remain blank? E.g.., label the rows and columns of that diagram with row numbers and column numbers and identify what pattern of row numbers and column numbers the numbers are in. – Eric Postpischil Sep 03 '21 at 00:44
  • What do you want to do with this “math square”? Do you want to generate one from scratch? Let’s say you fill in all the operator positions with random operators. Do you know how to do that? Then, you want to fill in most but not all of the number positions. Do you have an idea how to do that? How many will you leave blank? – Eric Postpischil Sep 03 '21 at 00:45
  • Suppose all the positions were filled in with numbers and with operators. Do you know how to write code that would go through the matrix and evaluate all the operators and figure out the results to go in the auxiliary row and the auxiliary column? – Eric Postpischil Sep 03 '21 at 00:46
  • Which parts of the above can you do and which parts do you need help with? For Stack Overflow, you are supposed to ask a specific question. – Eric Postpischil Sep 03 '21 at 00:46
  • @kaylum: You are driving Stack Overflow to a low level of simplistic coding, detracting from algorithms, abstractions, and higher level concepts. Not everything is a coding problem, and treating it as such lowers the value of Stack Overflow. Additionally, writing code before you know what the task to be performed is and what code to write leads to bad code and wrong code. Programmers should be organized and have some grasp on the goal before they start writing code. – Eric Postpischil Sep 03 '21 at 01:12
  • Here is how I would start: https://onlinegdb.com/7niVMkuIW Then you need to mask one operand per line - which for 3x3 is easy but needs to be a bit random for squares larger than 3x3 – Jerry Jeremiah Sep 03 '21 at 02:03
  • you're calling `srand` every time you call `by4equations` which isn't the right thing to do: [srand() — why call it only once?](https://stackoverflow.com/q/7343833/995714) – phuclv Sep 03 '21 at 04:24
  • @lemon syntax errors are something different than the program logic. You do not need us to sort them out only a good C book. https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – 0___________ Sep 03 '21 at 07:59

0 Answers0