scrabble issue c++ using vector - c++

im programming a little scrabble game, here are the code which i prefer to simulate the "playfield", but i got some issue
#include "Scrabble.h"
#include <iostream>
#include <cstdlib>
#include <stdexcept>
using namespace std;
Scrabble::Scrabble() {
Character *pointer;
pointer = NULL;
reihe = 15;
spalte = 15;
for (int i = 0; i < reihe; i++) {
playground.push_back(vector <Character>());
for (int j = 0; j < spalte; j++) {
playground[i].push_back('a');
}
}
}
void Scrabble::print() {
cout << " ";
for (int i = 0; i < column; i++) {
cout << i << " ";
}
cout << endl;
for (int r = 0; r < row; r++) {
cout << r << " ";
for (int s = 0; s < cloumn; s++) {
cout << playground[r][s] << " ";
}
cout << endl;
}
}`
the output looks a little strange
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
1 a
a
some advice how i get it looks normal like:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 a
1 a
2 a
3 a
4 a
5 a
6 a
7 a
8 a
9 a
10 a
11 a
12 a
13 a
14 a
any advice will be helpful, thx

Your code snippet doesn't compile (e.g. variable cloumn), so I suppose this is not exactly the code that you are testing.
One problem you'll face, is to ensure the with of a field. For this you have to include <iomanip>:
void Scrabble::print() {
cout<<" ";
for (int i = 0; i < column; i++) {
cout << setw(2)<<i << " ";
}
cout << endl;
for (int r = 0; r < row; r++) {
cout << setw(2)<< r << " ";
for (int s = 0; s < column; s++) {
cout << setw(2)<< playground[r][s] << " ";
}
cout << endl;
}
}
Here an online version (compiled without class, for demo purpose, as I don't know the definition of Scrabble

Related

How can I alter my program for the desired output?

I am trying to write a program that takes user input for the number of hours worked by 6 employees of a company to store in a two-dimensional array and prints the output in the following format:
Employee Number Hours
01 a
02 b
03 c
04 d
05 e
06 f
where a-f would be the user entered integer values.
I have experimented with several methods but am unable to get my desired output. I am attaching the recent attempt:
#include <iostream>
using namespace std;
int main()
{
int array[6][2]; // 6 by 2 matrix to store employee name and hours worked
int i, j;
cout << "Enter the number of hours worked by 6 employees: " << endl;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> array[i][j];
cout << array[i][j] << "\t" << endl;
break;
}
cout << endl;
}
}
What changes should I make to get the required output?
You should take the input first. Then in order to print with column name you should first print those column names and then print the values.
Your code should be like this -
#include <iostream>
using namespace std;
int main()
{
int array[6][2]; // 6 by 2 matrix to store employee name and hours worked
int i, j;
cout << "Enter the number of hours worked by 6 employees: " << endl;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> array[i][j];
}
}
cout << "Employee Number \t Hours" << endl;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
cout << array[i][j] << "\t\t\t ";
}
cout << endl;
}
}
Output:
Edit:
For your requirement from the comment, new code will be
int main()
{
int array[6][2]; // 6 by 2 matrix to store employee name and hours worked
int i, j;
cout << "Enter the number of hours worked by 6 employees: " << endl;
cout << "Employee Number \t Hours" << endl;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> array[i][j];
cout << array[i][j] << "\t\t\t ";
}
cout << endl;
}
}

Output a triangle of numbers in C++

My task is to ask the user for an int, then output a "number triangle" like the one below (in this case, the int is equal to 5).
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
However, the code which I have written for this task outputs this:
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
For reference, here is my code:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Size: " << std::endl;
cin >> size;
for(int i = size; i >= 0; i--)
{
for(int j = 0; j <= i; j++)
{
if (j < i){
cout << j << " ";
}
else{
cout << j << " ";}
}
cout << endl;
}
return 0;
}
Can somebody tell me what to change in my program to make it output the correct triangle? Thanks in advance.
You should print the spaces at the beginning of the line instead of at the end.
for(int i = size; i >= 0; --i){
for(int j = 0; j < size-i; ++j){cout << " ";} // spaces at the beginning
for(int j = 0; j <= i; ++j){
cout << j << " ";
}
cout << endl;
}

How to get symetrical matrix?

I have to write function:
void function (int * a [], int sq) checking if the sq-degree square matrix is ​​symmetrical
(relative to the main diagonal upper left - lower right) and performing any transposition of this matrix.
My main :
#include <iostream>
using namespace std;
void function(int* a[], int sq);
int main() {
const int sq = 10;
int a[sq][sq];
int *tab[sq];
for (int i = 0; i < 11; i++) {
tab[i] = a[i];
}
function(tab,sq);
return 0;
}
My function:
void function(int* a[], int sq) {
cout << "tell me the number and i give you (sq x sq) matrix:";
cin >> sq;
for(int i = 0; i < sq; i ++) {
for(int j = 0 ; j < sq; j++) {
cin >> a[i][j];
}
}
// 3 7 8 2
// 7 4 5 5
// 8 2 1 6
// 2 5 6 1
cout << "Matrix is " << sq << " degree:" << endl;
for(int i = 0; i< sq; i++) {
for(int j = 0; j < sq; j++) {
if(a[0][j] == a[i][0] && a[i-1][j] == a[i][j-1] ) {
cout << a[i][j] << " "; // if symetrical
} else {
cout << a[j][i] << " "; //if not, lets do transposition
}
}
cout << endl;
}
}
Example:
// 3 7 8 2
// 7 4 5 5
// 8 2 1 6
// 2 5 6 1
This is symetrical and should give me output:
Matrix is 4 degree:
3 7 8 2
7 4 5 5
8 2 1 6
2 5 6 1
But it's doing transposition and give me:
Matrix is 4 degree:
3 7 8 2
7 4 2 5
8 5 1 6
2 5 6 1
If i understand contents of exercise...
replace the for loop like below
your code :
for(int i = 0; i< sq; i++) {
for(int j = 0; j < sq; j++) {
if(a[0][j] == a[i][0] && a[i-1][j] == a[i][j-1] ) {
cout << a[i][j] << " "; // if symetrical
} else {
cout << a[j][i] << " "; //if not, lets do transposition
}
}
cout << endl;
}
Replace with below code :
for(int i = 0; i< sq; i++) {
for(int j = i; j < sq; j++) {
if(a[i][j] == a[j][i] ) {
cout << a[i][j] << " "; // if symetrical
} else {
cout << a[j][i] << " "; //if not, lets do transposition
}
}
cout << endl;
}

How to print one side of the diagonal of an array?

Lets suppose we have a 5 X 5 random array
1 2 3 7 8
4 7 3 6 5
2 9 8 4 2
2 9 5 4 7
3 7 1 9 8
Now I want to print the right side of the diagonal shown above, along with the elements in the diagonal, like
----------8
--------6 5
------8 4 2
---9 5 4 7
3 7 1 9 8
The code I've written is
#include <iostream>
#include <time.h>
using namespace std;
int main(){
int rows, columns;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter colums: ";
cin >> columns;
int **array = new int *[rows]; // generating a random array
for(int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int)time(NULL)); // random values to array
for(int i = 0; i < rows; i++){ // loop for generating a random array
for(int j = 0; j < columns; j++){
array[i][j] = rand() % 10; // range of randoms
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Max: " << endl;
for(int i = 0; i < rows; i++){//loop for the elements on the left of
for(int j = columns; j > i; j--){//diagonal including the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for(int i = rows; i >= 0; i++){ //loop for the lower side of
for(int j = 0; j < i - columns; j++){ //the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
return 0;
}
After running the code the shape I get is correct , but the elements do not correspond to the main array. I have no idea what the problem is.
Left side:
for (size_t i = 0; i < rows; i++) {
for(size_t j = 0; j < columns - i; j++) {
cout << array[i][j] << " ";
}
cout << "\n";
}
Right side:
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < columns; j++) {
if (j < columns - i - 1) cout << "- ";
else cout << vec[i][j] << " ";
}
cout << "\n";
}

C++ simple matrix

can someone fix my code?
this is the result that should be showed when i input number 5 in c++
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
my result:
1
2 6
3 7 10
4 8 11 14
5 9 12 15 18
my code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
int n,i,j;
cout<<"insert number"<<endl;
cin>>n;
for (i=1;i<=n;i++)
{
int y=1;
int g=1;
cout<<i<<" ";
for (j=1;j<=i-1;j++)
{
int x=n;
int b=i;
x--;
g--;
cout<<(x*y)+b+g<<" ";
y++;
}
cout<<endl;
}
getch ();
}
what did i do wrong?
sorry if my code messy i'm a c++ new learner.
You could it like this:
#include <iostream>
using namespace std;
int main()
{
int n , i ,j, sum;
cout << "masukkan bilanga" << endl;
cin >> n;
for(i = 0; i < n; i++)
{
cout << i + 1 << " ";
sum = i + 1;
for(j = 0; j < i; j++)
{
sum += n - 1 - j;
cout << sum << " ";
}
cout << endl;
}
return 0;
}
where the key point is that you want to print all the numbers, starting from 1, in a column-major manner, until a triangular n x n matrix is created.
Driven from the output, one can easily see that every element of the next column is what the current element is, plus n - 1, and that factor decreases by one as we advance to the right part of the matrix.
Try this code, hope this is what you need:
#include <iostream>
#include <string>
void printMatrix(int number);
void main()
{
int number = 1;
std::cout << "Enter a number: ";
std::cin >> number;
printMatrix(number);
}
void printMatrix(int number) {
std::cout << std::endl;
for (int i = 1; i <= number; i++) {
std::cout << i << " ";
int n = i;
for(int j = 1; j < i; j++) {
n += number - j;
std::cout << n << " ";
}
std::cout << std::endl;
}
}