#include <stdio.h>
#include <conio.h>
#include <string.h>
int main() {
int x,y,z,a;
char arr[221];
char temp;
printf ("Enter values: ");
gets(arr);
a = strlen(arr);
x=a;
while (x!=-1){
printf("\n%s", &arr[x]);
x--;
}
getch ();
}
the output must be like this
sample inputs:
A
2
1
R
X
D
W
note: since A is the first entry to the stack, the stack should look like this:
W D X R 1 2 A
"A" is at the very far end of the array or the stack, it means that "A" is the first one to go out from the stack. Then it is followed by 2, 1, R, and so on...
Your output should look like this:
A
W D X R 1 2
2
W D X R 1
1
W D X R
R
W D X
X
W D
D
W
W
Try this:
x=a-1;
while (x!=-1){
printf("\n%c", &arr[x]);
x--;
}
use x=a-1 and %c instead of x=1 and %s
First of all you are using gets() so the input must follow this format A21RXDW. that is a string. Only in this case the top of stack can have 'A'.
Now
char input[100]={'\0'}; //best practice
scanf("%s",input);
int stack_top = strlen(input) - 1;
//iterate
while(stack_top >= 0){
printf("%c\n", input[stack_top]);
--stack_top;
}
Related
There is a char pointer variable, and its value coming from a function.
char* apple = ....(function call)
I wanted to print this as follows:
int len = strlen(apple);
for(i=0;i<len;i++){
printf("%c ", apple[i]);
}
But in the console, it gives a question mark in a box as an output. What should I do, how should I print it? Thanks.
I dont see issue in the printing part, through the fucntion that retuns pointer to char array needs to be investigatd.
// In this example, getString function returns string literal
// That is being iterated in the next for loop over its length and prints its characters
#include <stdio.h>
#include <stdlib.h>
char *getString(void); // declare
int main() {
char *apple = getString();
int len = strlen(apple);
for(int i = 0; i < len ; i++) {
printf("%c ", apple[i]);
}
return 0;
}
char *getString() {
return "somesthing";
}
Below example will print only printable ascii chars. From 0 to 31 , 0 is for null, 1 is for SOH and so on. Simply you cannot print control codes (ASCII codes < 32) if you print strange output is expected.
#include <stdio.h>
#include <string.h>
#define PRINTABLE_ASCII_CHAR_COUNT 96
/*
Printable chars list
"! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
# A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _
` a b c d e f g h i j k l m n o
p q r s t u v w x y z { | } ~"
*/
char *getASCIIs(void);
int main() {
char *apple = getASCIIs();
int len = strlen(apple);
for(int i = 0; i < len ; i++) {
// p << i << ((i % 16 == 15) ? '\n' : ' ');
printf("%c ", apple[i]);
}
return 0;
}
char *getASCIIs() {
static char buffer[PRINTABLE_ASCII_CHAR_COUNT];
for (int i = 32, j=0 ; i <= PRINTABLE_ASCII_CHAR_COUNT; i++, j++) {
buffer[j] = i;
}
return buffer;
}
enter code here
Your syntax seems legit. I highly suspect that cigar[i] donates the proper character that you are looking for. Trying affirming that by casting cigar[i] into a character using (char) cigar[i]. You might output cigar[i] as a string %s as a part of debugging where does it really point at.
So far, the program that I have initializes and prints the checkboard as it is supposed to. Here are both init and print functions:
#include <stdio.h>
void initBoard(int n, int board[n][n]){
int i, j;
int numberOfRows = n/2 - 1;
for(i=0; i<numberOfRows; i++){
for(j=0; j<n; j++){
if((i%2==0&&j%2==0)||(i%2==1&&j%2==1)){
board[i][j] = 0;
}else{
board[i][j] = -1;
}
board[numberOfRows][j] = 0;
board[numberOfRows+1][j] = 0;
}
}
for(i=numberOfRows+2;i<n;i++){
for(j=0;j<n;j++){
if((i%2==0&&j%2==0)||(i%2==1&&j%2==1)){
board[i][j] = 0;
}else{
board[i][j] = 1;
}
}
}
}
void printBoard(int n, int board[n][n]){
int i, j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(board[i][j]==0){
board[i][j] = '-';
}else if(board[i][j]==1){
board[i][j] = 'W';
}else{
board[i][j] = 'D';
}
printf("%3c", board[i][j]);
}
printf("\n");
}
}
However, implementation of move function (provided example is for only right direction) which should move pieces on the board seem to not work.
void move(int n, int board[n][n], int x, int y, char direction){
if(direction!='R'||direction!='L'){
printf("\nYou can move only to right or left.\n\n");
}else if(direction=='R'){
if(x==n-1){
printf("You cannot move the piece to the right because you are already in a right corner of the board.\n");
}else{
if(board[y][x]==0){
printf("There is no piece in the column %i and row %i.\n", x, y);
}else if(board[y][x]==1){
if(y==0){
printf("You are on the kings row! Further move cannot be made.\n");
}else{
if(board[n-2-y][x+1]!=0){
printf("You cannot move in the following direction because there is already a piece in that cell.\n");
}else{
board[n-1-y][x] = 0;
board[n-2-y][x+1] = 1;
}
}
}else{
if(y==n-1){
printf("You are on the kings row! Further move cannot be made.\n");
}else{
if(board[n-y][x+1]!=0){
printf("You cannot move in the following direction because there is already a piece in that cell.\n");
}else{
board[n-1-y][x] = 0;
board[n-y][x+1] = -1;
}
}
}
}
}
After testing it out, here is the test:
int main(){
int size = 8;
int board[size][size];
initBoard(size, board);
printBoard(size, board);
move(size, board, 2, 2, 'R');
printBoard(size, board);
return 0;
}
...it shows in the console:
- D - D - D - D //this board is printed w/o any move function.
D - D - D - D -
- D - D - D - D
- - - - - - - -
- - - - - - - -
W - W - W - W -
- W - W - W - W
W - W - W - W -
You can move only to right or left.
D D D D D D D D //this is what happened when move function was called.
D D D D D D D D
D D D D D D D D
D D D D D D D D
D D D D D D D D
D D D D D D D D
D D D D D D D D
D D D D D D D D
The first call to printBoard overwrites the contents of board with the characters 'D', 'W', '-'.
move doesn't actually do anything at all to the board, because your test if(direction!='R'||direction!='L') is always false. (The message "You can move only to right or left." should have been a tip-off.) You probably meant to write if(direction!='R' && direction!='L'). So it is not the cause of the all-D output you are seeing.
So at the second call to printBoard, the array board is still full of the characters 'D', 'W', '-'. But printBoard is expecting it to be full of 1,-1,0. The way your test is written, anything other than those values falls into the last else clause, as if the value were -1, and is printed as (and replaced with) D.
You probably want to change printBoard to be non-destructive, and you'll probably have some more debugging to do on move once you have fixed the test to make it actually functional. I am very suspicious of your mixed board[y] and board[n-1-y] indices. You need to make up your mind whether y counts rows from the top of the board, or the bottom.
Reminder: always post complete code when asking for help! In this case, your first guess about which function contained the bug was completely wrong. Someone could have wasted a lot of time poring over that code in vain.
Another tip: I didn't do anything clever other than single-step your code in a debugger, watching the contents of board and seeing where it changed. I suggest getting familiar with your debugger and using it to try to understand the behavior of your code before asking the Internet for help. (And when you do ask, report what you learned from your debugging session.)
I am new to programming and have been asked to create a table with 3 variables x, y and z.
To create x and y, I was asked to use a for loops and have does so. For z, I have to multiply the values of x and y but I'm not entirely sure how to work out z and how to place it in a table.
Please help. I have given an example of how my results should be.
What I've done so far:
int x, y, z;
for (x = 1; x <= 4; x++)
printf(" %d ", x);
for (y = 2; y <= 5; y++)
printf(" %d ", y);
return 0;
The data structure should be not complex
int matrix[3][5];
for(i=0; i<5;i++){
matrix[0][i]=i+1;
matrix[1][i]=i+2;
matrix[2][i]=matrix[0][i]*matrix[1][i];
}
You can change to char matrix to include your headers
You could see that course
https://www.edx.org/course/c-programming-pointers-and-memory-management
If the task is only to print a table, like the one posted, all you need is one loop:
#include <stdio.h>
int main(void)
{
// print the header of the table
puts("======================\n x y z = x * y\n----------------------");
for ( int x = 1; // initialize 'x' with the first value in the table
x <= 5; // the last value shown is 5. 'x < 6' would do the same
++x ) // increment the value after each row is printed
{
int y = x + 1; // 'y' goes from 2 to 6
int z = x * y; // 'z' is the product of 'x' and 'y'
// print each row of the table, assigning a width to each column,
// numbers are right justified
printf("%3d %3d %3d\n", x, y, z);
}
puts("======================");
return 0;
}
The output beeing
======================
x y z = x * y
----------------------
1 2 2
2 3 6
3 4 12
4 5 20
5 6 30
======================
int x[] = {1,2,3,4,5,.....} <-----for storing values of x
int y[] = {2,3,4,5,6,....} <------for storing values of y
Take another array for storing z values.
So now we have z[i]=x[i]*y[i] where i=0,1,2,........n also y[i]=x[i]+1
Use a loop to calculate and print the result.
I'd like for the program to solve my equation yet sadly it doesn't. Additionally, I'd want for it to print an answer depending on the value of x that I input in the equation. Please let me know how I would be able to print the answer or how I can program it so that the equation gives me an answer that I can then print.
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
/* Main program */
void main ()
{
/*
variable declaration section comments
l: length value
q: value of q
ei: value of ei
s: l devided by 2 since 0 < x < l/2
b: the length l (thus, 20)
z: 0
first_equation: The first equation pertaining to 0 < x < l/2
second_equation:The second equation pertaining to l/2 < x < l
*/
double x, first_equation, second_equation, l, q, ei, s, b, z;
l = 20.0;
q = 4000.0;
ei = 1.2 * (pow(10.0, 8.0));
s = l / 2.0;
b = l;
z = 0.0;
printf ("please enter the x-value\n");
scanf ("%lf", &x);
/* Deflection equations */
first_equation = ((q * x) / (384.0 * ei)) * ((9 * (pow(l, 3.0))) - (24.0 * l * (pow(x, 2.0))) + (16 * (pow(x, 3.0))));
second_equation = ((q * l) / (384.0 * ei)) * ((8 * (pow(x, 3.0))) - (24.0 * l * (pow(x, 2.0))) + (17 * (pow(l, 2.0)) * x) - (pow(l, 3.0)));
/* Determining what equation to use */
if (x >= z && x <= s)
printf ("\n first_equation\n\n");
else if (x > s && x <= b)
printf ("\n second_equation\n\n", second_equation);
else if (x < 0 || x > b)
printf ("\n invalid location\n\n");
return;
}
This...
printf ("\n second_equation\n\n", second_equation);
... does not print the second_equation variable: it provides it as an argument to printf, but printf only uses extra arguments as directed by %f or other conversion instructions embedded in the text provided as the first argument. You could write:
printf ("\n second_equation %f\n\n", second_equation);
You may want to do something similar for first_equation.
Alternatively [when I answered the question was tagged C++] you could use C++ I/O routines (scanf and printf are from the C library, and have a number of disadvantages, the most obvious here being that you have to remember funny letter codes like "lf" matching your data types)...
#include <iostream>
...at the very top of your file, then in your function write...
std::cout << "\n second_equation " << second_equation << "\n\n";
You could also use C++ I/O for input, replacing scanf with...
if (!(std::cin >> x))
{
std::cerr << "you didn't enter a valid number\n";
exit(1);
}
Your code is really unclear; but going by your question, you seem to want to be able to print your answer. In that case, here is the proper syntax
printf ("Answer: %d \n", yourAnswer); //if 'yourAnswer' is decimal or number
To use one of your code snippets, you'll have this:
printf ("\n second_equation: %d\n", second_equation);
Help with problem: I need to write C + + program, which input and displays numeric variables with operators printf and scanf. the values are:
E = 10 ^ 3
F = -450
H = 0,005 * 10 ^ 2
X = -43,562 * 10 ^ (-3)
I = 75600
Y = -0,00036
But I do not understand how I have to enter 10 ^ 3 and other... at the moment my code is ...
#include <stdio.h>
#include <conio.h>
main()
{
int E;
int F;
puts("Введите значения E и F");
scanf("%d,%d", &E,&F);
printf("Значение E=%2d\n",E);
printf("Значение F=%4d\n",F);
puts("-----------------------------");
float H;
float X;
int I;
float y;
puts("Введите значение I");
puts("Введите значение H, X, Y");
getch();
return 0;
}
If I understood your question correctly, the answer is: use scientific notation
10 ^ 3 becomes 1e3,
0.005 * 10 ^ 2 becomes 0.005e-2
and so on