Jump to content

need help c++

kiril.zh

How to make a program that prompts the user to enter a 6 × 6 array with 0 and 1?

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know C++, but I do know programming.

 

You'd make a multi-dimensional array [6x6] filled with ints for 1 or 0, or booleans for true or false.

I might be wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

I get that but I don't know how to prompt the user to insert such an array.

Link to comment
Share on other sites

Link to post
Share on other sites

Ask for a value using cin. You can have local variables to determine the current index of the array you are in. 0,0 then add into the index, and increase the index. 0, 1. Continue until 0, array[0].size. Once the index is above the size, reset to 1, 0. Then continue.

Link to comment
Share on other sites

Link to post
Share on other sites

int matrixSize;

cin>>  matrixSize;

 

 

In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin.

For formatted input operations, cin is used together with the extraction operator, which is written as >> (i.e., two "greater than" signs). This operator is then followed by the variable where the extracted data is stored. For example:
 

12
int age;cin >> age;


The first statement declares a variable of type int called age, and the second extracts from cin a value to be stored in it. This operation makes the program wait for input from cin; generally, this means that the program will wait for the user to enter some sequence with the keyboard. In this case, note that the characters introduced using the keyboard are only transmitted to the program when the ENTER (or RETURN) key is pressed. Once the statement with the extraction operation on cin is reached, the program will wait for as long as needed until some input is introduced.

The extraction operation on cin uses the type of the variable after the >> operator to determine how it interprets the characters read from the input; if it is an integer, the format expected is a series of digits, if a string a sequence of characters, etc.
// i/o example#include <iostream>using namespace std;int main (){  int i;  cout << "Please enter an integer value: ";  cin >> i;  cout << "The value you entered is " << i;  cout << " and its double is " << i*2 << ".\n";  return 0;}

http://www.cplusplus.com/doc/tutorial/basic_io/

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

How to make a program that prompts the user to enter a 6 × 6 array with 0 and 1?

I'm not sure exactly what you want to do; do you want to prompt the user to input the size of the array in binary, prompt the user to have binary inputs for each segment of the 6x6 array, or something else?

Link to comment
Share on other sites

Link to post
Share on other sites

My guess is, you need to create 6x6 array, and if program will always ask user to fill 6x6 array, the array can be hardcoded as 6x6.

int array[6][6] = {0}; // Array 6x6 of integers, all cells initialised by 0.

Then you need to ask user for input, to get 1 or 0, you need to repeat this process 6 times for each row.

You need to create a loop that will ask user for number 6 times, so you will get row of 1 and 0 for first row of your 6x6 array.

for(int x = 0; x < 6; x++){	std::cin >> array[x][0]; // it will fill each array's cell of first row (indexed by 0) for each column of array iterated by x.}

Then you can insert this loop into another loop that will take care to ask 6 times for those 6 zeros or ones.

for(int y = 0; y < 6; y++){	for(int x = 0; x < 6; x++){		std::cin >> array[x][y]; // it will fill each array's cell of each row (indexed by y that is iterated now by outer loop) for each column of array iterated by x.	}}

You can add some checks for user input, if user inputs actuall 0 or 1 and if it's not 0 or 1 ask user again.

int i;do {	std::cin >> i;	if(i < 0 || i > 1){		std::cout << "You mustn't enter anything other than 0 or 1, try again." << std::endl;	}} while(i < 0 || i > 1);

It will make keep asking user for input if user's input was incorrect.

 

Also a hint, you can enter 6 values at once at program runtime by splitting values by space, so by entering: 0 1 0 0 1 1 0 and hitting enter you will fill whole row of 6x6 array at once. It happens because of cin works that way.

I haven't compile/check those lines of code, so there may be some syntax or other errors.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×