Question

C++ question: How would I make code that cin a dimension froma user between 3 to...

C++ question:

How would I make code that cin a dimension froma user between 3 to 11 and generate a tic tack toe grid? Should I set my array size to121 (an 11x11 grid) even if I don't use the entire array? Say the grid is only 3x3 then I would technically only use grid spaces 0-8 right?

It would look like this:

 ? | ? | ?
-----------
 ? | ? | ?
-----------
 ? | ? | ?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

In this problem use Two dimensional arrays instead of single dimension array so that instead of creating 121 size array create a grid or array based on the dimension that user enters . That means either create a single dimension array based on dimensions or create two dimension array .

For example if user enters the dimension 3 then create array size of 3 * 3 i.e 9 like this int grid[9]

in the similar way if we need better performance create two dimension array like int grid[3][3].

For better understanding refer the below code snippet:

Code:

#include <iostream>
using namespace std;
int main()
{
cout<<"Enter any dimension between 3 and 11: ";
int dim,i,j;
cin >> dim; //asking user to enter the dimensions of a grid
  
char two_dim_grid[dim][dim],single_dim_grid[dim*dim]; // create both single and two dimensions grid

for (i=0;i<dim*dim;i++)
single_dim_grid[i]='?'; //assign ? to each place in a single dimension grid

for (i=0;i<dim;i++)
{
for (j=0;j<dim;j++)
two_dim_grid[i][j]='?'; //assign ? to each place in a two dimension grid
}
  
cout << "Grid created Using single dimension array\n";
for (i=0;i<dim*dim;i++)
{
if (i%dim==0) //condition for new line
cout << "\n------\n";
cout << single_dim_grid[i] <<"|"; //print single dimension grid as a tic tack grid
}
  
cout << "\n\nGrid created Using two dimension array\n\n"; //print two dimension grid
for (i=0;i<dim;i++)
{
for (j=0;j<dim;j++)
cout << two_dim_grid[i][j] <<"|";
cout << "\n------\n";
}
return 0;
}
Code and Output Screenshots:

Note : if you have any queries please post a comment thanks a lot..always available to help you..

Add a comment
Know the answer?
Add Answer to:
C++ question: How would I make code that cin a dimension froma user between 3 to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

  • C++ language I am having some trouble with user validation, can someone look at my code...

    C++ language I am having some trouble with user validation, can someone look at my code and tell me what I am doing wrong: char firstInital, lastInitial;    int userAge;    std::cout << "Program 1-2: Get user initials and age in days\n ";    std::cout << "-------------------------------------------------------------------------\n";    std::cout << "Please enter the first letter of your first name: \n " << flush;    std::cin >> firstInital;    std::cout << "Please enter the first letter of your last name: \n...

  • Write a C program that prompts the user to enter a series of integers that represent...

    Write a C program that prompts the user to enter a series of integers that represent a coded message. I have given you the decoder program so that you can see how it is written using array notation. Your assignment is to convert the code to a program that uses only pointers and pointer math. Your program must use pointer notation and pointer math (i.e. no array index notation other than in the declaration section, no arrays of pointers and...

  • I don't know how to terminate the code in the second time that whether or not...

    I don't know how to terminate the code in the second time that whether or not the users want to continue to get a new number. PLEASE HELP. To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the...

    ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the attachments below and would greatly appreciate some help. ---------------------------------------            case 6:                printf("You chose determinate!\n\n");                printf("Press 2 for 2x2\n OR \n Press 3 for 3x3 Matrix: ");                scanf("%d", &detChoice);                printf("\n");                if (detChoice == 2) {                    printf("Matrix: ");...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

  • How to replace elements in a 2D array? Okay so for my program, I am trying...

    How to replace elements in a 2D array? Okay so for my program, I am trying to create a fish tank. My program is to generate 4 different FISH: ><))'> in a tank of tilde (~) characters. The tank is a 2D array of 8 rows and 32 columns so it would look like ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for one of the eight rows (before I generate the random positions of the fish in the rank). Then one row could look like ~~~~~~~~~~><))'>~~~~~~~~~~~~~~~~...

  • C++ i want Lab#3 done can u make clear code so I could understand it. Lab#2The...

    C++ i want Lab#3 done can u make clear code so I could understand it. Lab#2The objective of this lab is compare the populations of various cities that lie in between Toledo and Dayton on I-75. Write a program that produces a bar illustrating the populations. The program should read the name of the city and its population from a file. Have the program continue this process until the end of file is reached. For each city, your program should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT