2

Not an expert in C++ but I am doing my best, I have this code and I am trying to ask the user to enter a number between 1 and 12, and whatever number they enter, they 2D array will be adjusted to their number, for example:

  • Please Enter a number: 3
  • Program will output a 2D array of size 3x3, if they entered "4", it would be a 4x4 grid, etc.

I looked at many examples for the past few days but I could not wrap my head around it and apply it to my code. I would really appreciate it if someone can help me, this is what I got to up till now:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <vector>

using namespace std;

double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;
const int ROWS = ?;
const int COLS = ?;

typedef float Table[ROWS][COLS];


void displayOverview ();

void playOrQuit();

void outputSubSquare(Table table, int x, int y);
void fillTableWithRands(Table table);
void outputTable(Table table);

void fillIntArray(int array[], int size);
void reverseArray(int array[], int size);
void outputIntArray(int array[], int n);

int main(){

    displayOverview();

    playOrQuit();

    int maxNum = 4;
    int intArray[maxNum];
    fillIntArray(intArray, maxNum);
    outputIntArray(intArray, maxNum);

    srand(time(NULL));
    Table myTable;

    fillTableWithRands(myTable);
    outputTable(myTable);

    return 0;
}

void displayOverview(){

}

void playOrQuit(){

}

void fillIntArray(int array[], int size){

    do{
        cout << "Please Enter numbers between 1 and 12: ";
        cin >> Umain;

        if(Umain <= 12){
            for (Utemp = Umain; Utemp > 0; Utemp--){
                cout << "Please enter a number between 1 and 4: ";
                cin >> Atemp;

                for(int i = Atemp; i<0;i++){
                    cin >> array[i];
                }
            }
        }
        else{
            cout << "Not within limit :(\n";
        }
    }

    while (Answer == 'y');
}

void fillTableWithRands(Table table){
    for(int i = 0; i<ROWS; i++){
        for (int j = 0; j<COLS; j++){
            table[i][j]  = rand()%4+ 1;
        }
    }
}


void outputTable(Table table){
    for(int i = 0; i<ROWS; i++){
        for (int j = 0; j<COLS; j++){
            cout << table[i][j] << " ";
        }
    cout << endl;
    }
}


void outputIntArray(int array[], int n){
    for(int i = 0; i<n; i++){
        printf("%d", array[i]);
    }
    cout << endl;
}
Laila Murish
  • 59
  • 1
  • 7
  • `std::vector> grid(rows, std::vector(cols));` The big question -- are you allowed to use `std::vector`? If not [try this](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048) – PaulMcKenzie Nov 22 '18 at 22:49
  • If `vector` is not allowed, may I recommend making yourself a simple Matrix class around a dynamically allocated array? Good example here: https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op – user4581301 Nov 23 '18 at 00:14

1 Answers1

1

Take a look at this answer here

You can either use std::vector and don't have to care about memory alllocation, or you create a dynamic array and allocate memory based on the given input.

For the dynamic array take a look here

Michael Prommer
  • 101
  • 1
  • 6
  • How do I use std::vector, where do I place it? sorry if this is a stupid question, but I never used std::vector. – Laila Murish Nov 22 '18 at 23:09
  • @LailaMurish *but I never used std::vector.* --Then how were you to complete the assignment if you were not told how to create a dynamic 2 dimensional array? A tutorial on `vector` usage is way too big of a topic -- that is best learned by reading **good** C++ books and tutorials. – PaulMcKenzie Nov 22 '18 at 23:32
  • Yes, I just asked, we are not allowed to use vectors, I don't understand though why? (btw, was able to work it out thankfully, thank you for your help, truly appreciated). – Laila Murish Nov 23 '18 at 00:04