I have a matrix written out here, and it appears on the screen fine but I want to put symbols into it. I can't seem to get the symbols (X) to appear in it. Sorry this is my first time working with arrays.
#include <iostream>
#define WIDTH 70
#define HEIGHT 20
using namespace std;
void main ()
{
char world[HEIGHT][WIDTH];
// draws matrix
for ( char i = 0; i < HEIGHT; i++ ) {
for ( char j = 0; j < WIDTH; j++ ) {
world[i][j] = '.';
cout << world[i][j];
}
cout << endl;
}
// 1st symbol at coordinates 1, 1
int x, y;
x = 1, y = 1;
world[x][y] = 'x';
cout << world[x][y];
// 2nd symbol at coordinates 2, 2
x = 2, y = 2;
world[x][y] = 'x';
cout << world[x][y];
//so on..
}
You have to first insert the xs, and then print the grid, not the other way around.
First of all, main should be declared as either int main() or int main(int argc, const char* argv[]); it certainly doesn't return void.
The standard output cout works sequentially. You just tell it what to print and it does it, one after the other. Consider the following:
int x = 1;
std::cout << x;
x = 2;
std::cout << x;
Because we changed the value of x and then passed it to cout again, it seems like you're expecting the output would be:
2
Actually the output is:
12
cout doesn't care that we passed it the same variable twice. It just prints each thing we give it one after the other.
You're going to have to set each element of the matrix you want to be an 'x', then loop through it again and print it all out.
Related
For e.g. X=12 need to get the same number of 0's added to the end of the below var
The output should look something like allHeKnows = 1000000000000
For any X = single digit will display the correct output, however
Not sure how to reset, or what condition to add that in order for any X >= 10 to display the int allHeKnows value accordingly
#include <iostream>
using namespace std;
int main () {
int X, allHeKnows = 1, i=0;
cin >> X;
while ((X > 0 && X <100) && (i!=X)) {
i++;
allHeKnows = allHeKnows * 10;
cout << "this is allHeKnows: " << allHeKnows << "\n";
}
return 0;
}
One problem that I see initially is that once you get past 10 digits you surpass the max int value allowed by c++. You would be better off converting to a string if you are trying to reach those higher numbers.
I'm writing a battleship game in the console, and I'm writing a function that will draw one grid based on a 2-dimensional array. The approach I'm taking is such:
--> Draw 1 row which contains a character X amount of times (like 10)
--> Draw that row, putting a newline at the end of the drawing process, 10 times to get a nice field.
Now, I do need to insert a newline at the end of 1 row, right? But how do I compare only the x-element of the array, and not the y-element?
Here's my code:
// Includes
#include <iostream> // For IO
#include <cstdlib> // For rand()
// Important game stuff
const int empty = 0; // Water
const int occupied = 1; // Ship
const int hit = 2; // Hit a ship
const int missed = 3; // Missed
// Variables
const int fields = 10;
// We want a 10x10 field
int board[fields][fields]; // board[x][y]
// Initialize board
void initb(int array[fields][fields]);
// Draw board x-axis
void drawbx(int array[fields][fields]);
int main(void)
{
drawbx(board;)
// game(Players);
return 0;
}
// Initialize the board, make everything hit
void initb(int array[fields][fields])
{
for(int x = 1; x <= 10; x++)
{
for(int y = 1; y <= 10; y++)
{
array[x][y] = hit;
}
}
}
void drawbx(int array[fields][fields])
{
for(int i = 1; i <= fields; i++)
{
if(array[i][] == empty || array[i][] == occupied)
{
if(i == 10)
std::cout << " X\n";
else if(i == 1)
std::cout << "X ";
else
std::cout << " X ";
}
}
}
Take a look specifically at the drawbx() function. I want to draw something like
X X X X X X X X X X\n
The syntax that I tried, if(array[i][] == empty || array[i][] == occupied), doesn't work. There must be an expression in the second pair of square brackets. Can someone help me?
I see two major problems:
1) Array indexing is out of range. You use index 1 to 10. It shall be 0 to 9.
2) Code array[i][] == empty is illegal syntax. You can't leave one index empty.
If you want a function that draw one row, perhaps pass the row number to the function like:
void draw_one_row(int array[fields][fields], int row_to_draw)
{
for(int i = 0; i < fields; i++)
{
if(array[row_to_draw][i] == empty || array[row_to_draw][i] == occupied)
{
...
}
}
}
To draw the whole board:
void draw_board(int array[fields][fields])
{
for(int i = 0; i < fields; i++)
{
draw_one_row(array, i);
}
}
BTW: Since you write C++, I'll recommend that you use vector instead of arrays.
#include <cstdlib>
#include <iostream>
using namespace std;
int myFunction(int n)
{
int x;
if (n==1 || n==2)
x = 1;
else
x = myFunction(n-2) + myFunction(n-1);
return x;
}
int main(int argc, char *argv[])
{
int n,a;
n = 7;
a = myFunction(n);
cout << "x is: " << a;
system("PAUSE");
return EXIT_SUCCESS;
}
The output to this is "x is: 13".
How do I get x = 13 when I do n = 7?
It seems that the function repeats some number of times until x=1.
I will walk you through it for a simpler example where the function is 4, but the overall concept is the same for 7 as well.
First call: myFunction(4) and inside the function n = 4.
Since n does not equal 1 or 2, we skip to the else:
x = myFunction(3) + myFunction(2);
x = myFunction(1) + myFunction(2) + 1; // If there is a 1, that means
// that we went to the if
// clause, not the else, since
// n was either 1 or 2
x = 1 + 1 + 1;
x = 3;
This concept is called recursion and can be very useful at times. Your function currently calculates the nth fibonacci number.
On a side note, you should read why using system("PAUSE"); is a bad idea.
I've asked such question before but it was kinda misleading since i didn't include printing order.Since i learned whole concept changes with that i thought asking it again would be more approtiate.
#include <iostream>
using namespace std;
int main()
{
int a, b, c, i;
cin >> a >> b >> c;
for ( i = 0; i < a; i++)
cout << "*" << endl;
for ( i = 0; i < b; i++)
cout << "*" << endl;
for ( i = 0; i < c; i++)
cout << "*" << endl;
}
Im aware that output is as same as:
for ( i = 0; i < a + b + c; i++ ){
cout << "*" << endl;
}
So for 2 3 1 i get:
*
*
*
*
*
*
What i want is:
*
* *
* * * //Horizontal distance between 2 shapes don't matter.
And it must be done in exactly in that order.Also printing of each column must be done with a seperate function.
First loop:
*
*
Second loop:
*
* *
* *
Last loop:
*
* *
* * *
*EDIT:*Appereantly there was an other solution to do this without any using cursor manipulation at all.My teacher suggested that i should first store the characters in a char pointer and then print that char pointer memory row by row.Which worked just fine.
Here's a curses program that will do it
#include <iostream>
#include <curses.h>
using namespace std;
int main(int argc, char** argv)
{
int a,b,c,i;
cin >> a >> b >> c;
initscr(); // initialise curses
int rows, cols;
getmaxyx(stdscr, rows, cols); // get screen size
for (i=0; i<a; i++) {
mvprintw(rows - 1 - i, 0, "*"); // plot at bottom column 0
}
for (i=0; i<b; i++) {
mvprintw(rows - 1 - i, 1, "*"); // plot at bottom column 1
}
for (i=0; i<c; i++) {
mvprintw(rows - 1 - i, 2, "*"); // plot at bottom column 2
}
refresh(); // update screen
getch(); // exit when key is pressed
endwin(); // exit curses
return 0;
}
You cannot do this the way you are wanting to. You'll need to print a horizontal line at a time, since you can't output to the console vertically.
So first you'll need to find out how many lines you need in total, totalLines, which is the maximum value a, b, and c. Then you should iterate over each of these lines.
Within the line iterations, you need to print out the correct number of *s in the correct positions. The condition for whether you need to draw a point for a is a >= totalLines - line (where line is the current line, starting from 0 for the first line). Similarly for b and c, so you'll need 3 if statements with these conditions which each print out either whitespace or a *.
For this code I created that outputs the ASCII characters corresponding to ints, I need to print out 16 ASCIIs per line. How would I go about doing so? I'm not sure how to approach these? Do I create another for loop inside?
int main()
{
int x = 0;
for (int i = 0; i <= 127; i++)
{
int x = i;
char y = (char) x;
cout << y;
}
return 0;
}
Or should I put the cout outside with 16 separate lines? I am trying to print 17 ASCIIs starting from 1 in a row.
Use another variable that counts up along with i. When it reaches 16, reset it and print a new line. Repeat until the loop terminates.
i.e.(I may be off by one here, I didn't think about it too deeply)
for (int i=0, j=1; i<=127; i++,j++)
{
int x = i;
char y = (char) x;
cout << y;
if (j == 16) {
j = 0;
cout << '\n';
}
}
Alternatively, you could just check if (i % 16 == 0)
You don't need another variable to track it. i is already an int.
so if i modulo 16 equals 0 then print a newline
else print (char)i
EDIT:
Note, using variables like i is ok for simple iteration but its always good practice to name them better.
So think about how changing i to ascii in your program improves the readability. It instantly makes it even more clear what is it that you are trying to do here.
int main()
{
int charsThisLine =0;
for (int currentChar=0; currentChar<128; currentChar++)
{
if(charsThisLine==16)
{
cout<<endl;
charsThisLine = 0;
}
else
{
cout<<(char)currentChar;
charsThisLine++;
}
}
}
How about:
#include <iostream>
int main()
{
for(int i = 0, j = 0; i < 128; ++i, ++j)
{
if(j == 16)
{
j = 0;
std::cout << std::endl;
}
std::cout << static_cast<char>(i);
}
return 0;
}
Every iteration, j increases by 1; after 16 iterations, j is reset to 0, and a newline is printed.
Alternatively, as #Sujoy points out, you could use:
if((i % 16) == 0)
std::cout << std::endl;
But this introduces the problem of printing an extra newline character at the beginning of the output.
Yes, you need a second loop inside the first. (I misunderstood what is being requested.)
You also need to clean up the code. The first x is unused; the second x isn't needed since you could perfectly well use char y = (char)i; (and the cast is optional). You should normally use a loop for (int i = 0; i < 128; i++) with a < condition rather than <=.
You will also need to generate a newline somewhere (cout << endl; or cout << '\n';). Will you be needing to deal with control characters such as '\n' and '\f'?
Finally, I'm not sure that 'asciis' is a term I've seen before; the normal term would be 'ASCII characters'.