I am trying to print a series of multi-line strings (ascii art letters here), and when printing them out, the top of each letter moves to the right while the rest of the letter stays in the same position. Here is a screenshot of what occurs:
I do not know why this is happening, as I am fairly new to C; if you have any knowledge about this, please share it!
#include <stdio.h>
#include <curses.h>
typedef const char letter[];
letter Y =
"___ __\n
\\ \\__ / /\n
\\ \\ / /\n
| | |\n
| | |\n
|__|__|\n";
letter O =
"_______ \n
/ __ \\\n
| | | |\n
| |__| |\n
\\_______/\n";
letter U =
" __ __ \n
/ | | \\\n
| | | |\n
| \\_/ |\n
\\_________/\n";
letter L =
" _\n"
"| |\n"
"| |\n"
"| |__\n"
"|____/\n";
letter S =
" _________\n"
"/ _____/\n"
"\\_____ \\\n"
"/ \\\n"
"/_______ /\n"
" \\/\n";
letter T =
"___________\n"
"\\__ ___/\n"
" | |\n"
" | |\n"
" |___|\n";
letter EXCLAMATION_POINT =
"_________\n"
"\\\\\\\\|////\n"
" \\\\\\|///\n"
" \\\\|//\n"
" \\|/\n"
" ***\n"
" ***\n"
" *\n";
const char *MESSAGE[] = {Y, O, U, L, O, S, T, EXCLAMATION_POINT};
int main() {
initscr();
cbreak();
noecho();
int maxY, maxX;
getmaxyx(stdscr, maxY, maxX);
int spacingPerLetter = maxX / 8;
for (int i = 0; i < 8; i++) {
mvprintw(maxY / 2, spacingPerLetter * (i + 1), MESSAGE[i]);
refresh();
getch();
clear();
}
endwin();
return 0;
}
The main problem is the newline embedded inside the strings you print.
The first "line" of the letters will be printed in the correct position, but then the newline will reset the position to the first column on the next line.
I recommend that you print each "letter" line by line (without the newlines). This could be helped by having each "letter" be an array of arrays of characters, where each sub-array is one line of the letter:
#define LETTER_WIDTH 11
#define LETTER_HEIGHT 6
const char Y[LETTER_HEIGHT][LETTER_WIDTH] = {
"___ __",
"\\ \\__ / /",
" \\ \\ / / ",
" | | | ",
" | | | ",
" |__|__| "
};
Related
I am trying to develop a code for a tic tac toe game. I have to use a 2d array and the board must look like this.
| _ | _ | _ |
| _ | _ | _ |
| _ | _ | _ |
So far I've got the board down and I have it to where I can take input from the user for the row and column they wish to place in. But I'm having trouble with getting the symbol to be placed in the right spot.
When I enter in 1 1 (row 1, col 1) this is the output I get
| _ | _ | _ |
| _ x| _ |
| _ | _ | _ |
when I need to get this
| x | _ | _ |
| _ | _ | _ |
| _ | _ | _ |
This is my code that I have so far. Ive tried changing the array to a char type but then I don't know how to get the board to be multiple chars which I would need to get the board to look like the one above.
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Let's play Tic-Tac-Toe!");
String[][] board = new String[3][3];
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
board[i][j] ="| _ ";
}
}
printBoard(board);
boolean playerX = true;
String symbol = " ";
if(playerX) {
symbol = "x";
} else{
symbol = "o";
}
int userRow = 0;
int userCol = 0;
while(true){
System.out.print("Enter row and col");
userRow = scnr.nextInt();
userCol = scnr.nextInt();
if(userRow < 1 || userCol < 1 || userRow > 3 || userCol > 3){
System.out.println("Please enter valid row and col numbers from 1 to 3:");
} else if(board[userRow][userCol] != "| _ "){
System.out.println("That spot is full!");
} else {
break;
}
}
board[userRow][userCol] = symbol;
printBoard(board);
}
public static void printBoard(String[][] board){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
System.out.print(board[i][j]);
}
System.out.println('|');
}
}
}
On line 46, you should change this
board[userRow][userCol] = symbol;
to this
board[userRow][userCol] = "| " + symbol + " ";
Question
How can I give spaces between numbers? When I add a <<" " after cout<<j the pattern changed. Is there any other way to give spaces between numbers?
code
#include<iostream>
using namespace std;
int main(){
int i,j=1,space,star,n;
cin>>n;
i=1;
Looping
while(i<=n){
space=n-i;
while(space){
cout<<" ";
space--;
}
star=i;
while(star){
cout<<j<<" ";
j++;
star--;
}
cout<<"\n";
i++;
}
return 0;
}
output
for n=4
1
23
456
78910
I want this output :-
1
2 3
3 4 5
7 8 9 10
For the expected output you only need to add a second space in the while(space) loop:
space = n - i;
while (space) {
std::cout << " "; // note: two spaces
space--;
}
or multiply space by 2 before the loop:
space = 2 * (n - i);
while (space) {
std::cout << ' ';
space--;
}
You could also #include <string> and skip the loop:
space = 2 * (n - i);
std::cout << std::string(space, ' ');
Another way of skipping the loop is to #include <iomanip> and use std::setw.
Note that you can use std::setw and std::left to correct the while (star) loop too to make this pattern hold for up to n = 13.
space = 2 * (n - i) + 1;
std::cout << std::setw(space) << "";
while (star) {
std::cout << std::setw(2) << std::left << j;
j++;
star--;
}
Demo
I am trying to print each char of an array of matrices for a brick breaker game (the full message would be YOU LOSE). I am new to C and I don't feel too confident about using pointers; I feel that that may be the source of my problem. To try to solve the problem, I've read plenty of online guides on how to deal with strings in C; but the fact that I'm dealing with an array of arrays of arrays of chars makes this task quite a bit harder. If you know how to print matrices of strings (in yet another array) in C, or you have a better solution, please let me know!
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define LETTER_WIDTH 13
#define LETTER_HEIGHT 6
char Y[LETTER_HEIGHT][LETTER_WIDTH] = {
"___ __\n",
"\\ \\__ / /\n",
"\\ \\ / /\n",
"| | |\n",
"| | |\n",
"|__|__|\n"};
char O[LETTER_HEIGHT][LETTER_WIDTH] = {
" _______ \n",
" / __ \\\n",
"| | | |\n",
"| |__| |\n",
" \\_______/\n"};
char *SENTENCE[2][LETTER_HEIGHT][LETTER_WIDTH] = {*Y, *O};
void printLetter(char letter[LETTER_HEIGHT][LETTER_WIDTH]) {
for (int i = 0; i < LETTER_HEIGHT; i++) {
for (int j = 0; j < LETTER_WIDTH; j++) {
printf("%c", letter[i][j]);
}
}
}
void printSentence() {
for (int i = 0; i < 2; i++) {
char letter[LETTER_HEIGHT][LETTER_WIDTH];
strcpy(*letter, **SENTENCE[i]);
printLetter(letter);
sleep(1);
}
}
int main() {
printSentence();
return 0;
}
Firstly this should be better
char* Y[LETTER_HEIGHT] = {
"___ __\n",
"\\ \\__ / /\n",
"\\ \\ / /\n",
"| | |\n",
"| | |\n",
"|__|__|\n"};
char* O[LETTER_HEIGHT] = {
" _______ \n",
" / __ \\\n",
"| | | |\n",
"| |__| |\n",
" \\_______/\n"};
Now these are arrays of size 6 (you must add one line because O now have height of 5) containing pointers to arrays of chars. Next
char** SENTENCE[2] = {Y, O};
You did some really weird things with this line before, this defines SENTENCE as 2 element array of pointers to array of pointers to char arrays (which are Y and O).
Next
void printLetter(char** letter) {
for (int i = 0; i < LETTER_HEIGHT; i++) {
printf("%s", letter[i]);
}
}
This function takes pointer to array of pointers to char arrays. Then goes 6 times and print each array as string. Next
void printSentence() {
for (int i = 0; i < 2; i++) {
printLetter(SENTENCE[i]);
sleep(1);
}
}
Here you can use simple for loop to pass to printLetter each pointer to array of pointers to char arrays (which are these letters) from SENTENCE.
or you have a better solution, please let me know!
Yes, there is a much simpler and, I would argue, better solution, it's to place the SENTENCE in a single 2D array and print it in one go, even if you are to use ncurses, this makes your job easier.
Note that with ncurses you can reposition the cursor so you can print each letter separately in one line, you wouldn't need to join them together like you try to do in SENTENCE.
#define LETTER_WIDTH 100
#define LETTER_HEIGHT 6
char SENTENCE[LETTER_HEIGHT][LETTER_WIDTH] = {
"__ __ ______ \n",
"\\ \\ / / / __ \\\n",
" \\ \\/ / | | | |\n",
" | | | | | |\n",
" | | | |__| |\n",
" |__| \\______/\n"};
void printSentence()
{
for (int i = 0; i < 6; i++)
{
printf("%s", SENTENCE[i]);
}
}
Output:
__ __ ______
\ \ / / / __ \
\ \/ / | | | |
| | | | | |
| | | |__| |
|__| \______/
This question already has answers here:
printing a square with diagonals
(4 answers)
Closed 3 years ago.
Guys i'm pretty stuck here. I'm trying to learn c and create some very basic code which asks the user to insert a number. Then, this number enters the following formula : 2x+1, then I want it to print a hollow square pattern with a different symbol for rows and columns, and add a + in the corners, diagonals, and a "X" in the middle.
I'm stuck in the very very beginning of the code. I don't know where should I even start. I mean I can't even learn how to make different symbols for the rows and columns.
I'm trying to learn and study it for 3 hours already, watched 20 different YouTube videos and read 20 different coding guides.
It's so frustrating..
Thanks.
I'm attaching a picture of my code & my output, and the desired output on the right.
the code itself:
int size;
printf("Please enter a number that will define the size of the square: \n");
scanf("%d", &size);
size = 2 * size + 1;
for (int i = 1; i <= size-2; i++) {
for (int j = 1; j <= size-2; j++) {
if (j == 1 || j == size - 1) {
printf("|");
}
else {
printf(" ");
}
if (i==1 || i==size-2){
printf("-");
}
else {
printf(" ");
}
}
printf("\n");
}
#include <stdio.h>
int main(void) {
int size;
printf("Please enter a number that will define the size of the square: \n");
scanf("%d", &size);
size = 2 * size + 1;
const char *spaces=" ";
const char *dashes="-----------------------------------------";
printf("+%.*s+\n", size, dashes);
for(int i=1; i<size/2+1; ++i)
{
printf("|%.*s\\%.*s/%.*s|\n", i-1, spaces, size-2*i, spaces,i-1, spaces);
}
printf("|%.*sX%.*s|\n", size/2, spaces, size/2, spaces);
for(int i=size/2+1; i<size; ++i)
{
printf("|%.*s/%.*s\\%.*s|\n", size-i-1, spaces, 2*(i-size/2)-1, spaces, size-i-1, spaces);
}
printf("+%.*s+\n", size, dashes);
return 0;
}
Example Run:
Please enter a number that will define the size of the square: 8
Success #stdin #stdout 0s 4568KB
+-----------------+
|\ /|
| \ / |
| \ / |
| \ / |
| \ / |
| \ / |
| \ / |
| \ / |
| X |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
|/ \|
+-----------------+
I recently created a program that calculates flow rate through a pipe and generates, line by line, a scatter graph of the output. My knowledge of C is rudimentary (started with python) and I get the feeling that I may have made the code overly complicated. As such, I am asking if anyone has any alternatives to the code below. Critiques of code structure etc. are also welcome!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define PI 3.1415926
double
flow_rate(double diameter, double k, double slope){
double area, w_perimeter, hyd_rad, fr;
area = (PI*pow(diameter,2.0))/8.0;
w_perimeter = (PI*diameter)/2.0;
hyd_rad = area/w_perimeter;
fr = (1.0/k)*area*pow(hyd_rad,(2.0/3.0))*pow(slope,(1.0/2.0));
return fr;
}
int
main(int argc, char **argv) {
double avg_k=0.0312, min_slope=0.0008;
float s3_diameter;
int i=0, num=0, flow_array[6] ,rows, align=29;
char graph[] = " ";
char graph_temp[]= " ";
printf("\nFlow Rate (x 10^-3) m^3/s\n");
for (s3_diameter=0.50;s3_diameter>0.24;s3_diameter-=0.05){
flow_array[i] = (1000*(flow_rate(s3_diameter, avg_k, min_slope))+0.5);
i += 1;
}
for (rows=30;rows>0;rows--){
strcpy(graph_temp,graph);
for (num=0;num<6;num++){
if (rows==flow_array[num] && rows%5==0){
graph_temp[align] = '*';
printf("%d%s\n",rows,graph_temp);
align -= 5;
break;
}
else if (rows==flow_array[num]){
graph_temp[align] = '*';
printf("|%s\n",graph_temp);
align -= 5;
break;
}
else {
if (rows%5==0 && num==5){
printf("%d%s\n",rows,graph_temp);
}
else if (rows%5!=0 && num==5){
printf("|%s\n",graph_temp);
}
}
}
}
printf("|----2----3----3----4----4----5----\n");
printf(" 5 0 5 0 5 0\n");
printf(" Diameter (x 10^-2) m\n");
return 0;
}
Output as below.
Flow Rate (x 10^-3) m^3/s
30
|
|
|
|
25
|
|
| *
|
20
|
|
| *
|
15
|
|
| *
|
10
| *
|
|
| *
5
| *
|
|
|
|----2----3----3----4----4----5----
5 0 5 0 5 0
Diameter (x 10^-2) m
GNUPlot is by far the simplest way to draw graph in C.
It can draw from simple plotting to complex 3d graph, and even provides an ASCII Art output (if ASCII output is really required)
You can find more information on how to use GNUPlot in a C program here: http://ndevilla.free.fr/gnuplot/