Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Okay so I swear this code should work, but obviously it doesn't. I am attempting to create a Snake game in C, however my Stage isn't displaying correctly. I am attempting to make it so the stage has no characters displaying inside, but only around the perimeter, can anyone assist me?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* boolean functions */
#define TRUE 1;
#define FALSE 0;
/* board */
const int length = 20;
const int height = 20;
/* Global Variables */
int x, y, foodX, foodY;
int gameEnd;
int score;
/* movement */
typedef enum {STOP = 0, UP, DOWN, LEFT, RIGHT} movement;
movement move;
/* Game Setup */
void Game()
{
gameEnd = FALSE;
move = STOP;
y = height / 2;
x = length / 2;
foodX = rand() % length;
foodY = rand() % height;
score = 0;
}
/* What is displayed on screen */
void Display()
{
system("cls");
int i;
for( i = 0; i < length + 2; i++)
printf("#\n");
for( i = 0; i < height; i++)
{
int j;
for(j = 0; j < length; j++)
{
if( j == 0 )
printf("#");
printf(" ");
if( j == length - 1)
printf("#\n");
}
}
for( i = 0; i < length + 2; i++)
printf("#\n");
}
void Input()
{
}
void Logic()
{
}
int main(void)
{
Game();
while (!gameEnd)
{
Display();
Input();
Logic();
Sleep(10);
}
return 0;
}
First avoid to make semicolon after the #define, you will get compilation error when you try to pass your constant TRUE to a function.
To have correct display, you should remove the "\n" from printf of first and last for loop of the function Display. And Add printf("\n") just after the for loop. It should be:
void Display()
{
int i;
int j;
system("cls");
for( i = 0; i < length + 2; i++)
printf("#");
printf("\n");
for( i = 0; i < height; i++)
{
for(j = 0; j < length; j++)
{
if( j == 0 )
printf("#");
printf(" ");
if( j == length - 1)
printf("#\n");
}
}
for( i = 0; i < length + 2; i++)
printf("#");
}
In case you want to display a character "*" in position (x, y), you just need to change:
printf(" ");
by:
if (y == i && x == j)
printf("*");
else
printf(" ");
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
The purpose I want to achieve is to shift these numbers to the right, but there is always a bit in the process of implementation that is a garbled problem, I want to solve this problem, please help me point it out, thank you very much.
#include <stdio.h>
#define N 10
int main(void) {
int limit, tranfer, arr[N];
int i;
scanf_s("%d %d", &limit, &tranfer);
for (i = 0; i < limit; i++) {
scanf_s("%d", &arr[i]);
}
//怎样实现来进制来向前后移动呢? -- 循环
int j, t;
for (j = 0; j <(limit - tranfer); j++) {
for (i = 0; i < limit; i++) {
t = arr[i];// 利用那个交互两个数值的思想
arr[i] = arr[i+1];
arr[i+1] = t;
}
}
//问题:少了数组的最后一位则应该是添加 \0 来表示结束
for (i = 0; i < limit; i++) {
printf("%d ", arr[i]);
}
return 0;
}
My compilation results
enter image description here
The result of the request
enter image description here
In following loop:
for (i = 0; i < limit; i++) {
t = arr[i];// 利用那个交互两个数值的思想
arr[i] = arr[i+1];
arr[i+1] = t;
}
when i == limit-1 (last iteration) you swap last array element (arr[limit-1]) with an undefined one behind the last (arr[limit]), causing strange output. To solve that you just need to change your loop condition to i < limit - 1. Final code:
#include <stdio.h>
#define N 10
int main(void) {
int limit, tranfer, arr[N];
int i;
scanf_s("%d %d", &limit, &tranfer);
for (i = 0; i < limit; i++) {
scanf_s("%d", &arr[i]);
}
//怎样实现来进制来向前后移动呢? -- 循环
int j, t;
for (j = 0; j <(limit - tranfer); j++) {
for (i = 0; i < limit - 1; i++) {
t = arr[i];// 利用那个交互两个数值的思想
arr[i] = arr[i+1];
arr[i+1] = t;
}
}
//问题:少了数组的最后一位则应该是添加 \0 来表示结束
for (i = 0; i < limit; i++) {
printf("%d ", arr[i]);
}
return 0;
}
You can use memcpy() for increase performance on big array
with offset of rotate count like this example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
int main(void) {
int limit, tranfer, arr[N];
int i;
scanf("%d %d", &limit, &tranfer);
for (i = 0; i < limit; i++) {
scanf("%d", &arr[i]);
}
int *tmp = malloc(sizeof(int) * limit);
memcpy(&tmp[tranfer], arr, sizeof(int) * (limit - tranfer));
memcpy(&tmp[0], &arr[limit - tranfer], sizeof(int) * (tranfer));
for (i = 0; i < limit; i++) {
printf("%d ", tmp[i]);
}
return 0;
}
I wrote a code that tries to multiply two matrices and put them in another result matrix. The code is working (I think) but it prints a very strange output .. I think it has to do with one of the functions pointing to something diffrent than the values, not sure though.
I tried checking each function in separete, also inizialzing each matrix with {0}.
#include <stdio.h>
#define SIZE 5
//#pragma warning(disable:4996)
//#define _CRT_SECURE_NO_WARNINGS
void read_mat(int mat[][SIZE])
{
int i, j, k = 0;
char s[100]; // assign input str to 's'
fgets(s,25,stdin); // recieving only the first 25 numbers
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (s[k] == '\0') { //if the string is just \0 -- the end of the string
mat[i][j] = 0;
}
else { // is there are values in s
if (s[k] != '0') { // binary matrix -- only 0 or 1
mat[i][j] = 1;
k++;
}
else {
mat[i][j] = 0;
++k;
}
}
}
}
}
void mult_mat(int mat_a[][SIZE], int mat_b[][SIZE], int result_mat[][SIZE])
{
int i, j, k;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
result_mat[i][j] += mat_a[i][k] * mat_b[k][j]; // by definition of matrix multiplication
k++;
}
}
}
}
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("%3d", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
int main()
{
int mat_a[SIZE][SIZE] = {0}, mat_b[SIZE][SIZE] = {0}, result_mat[SIZE][SIZE] = {0}; // initializing matricies to {0}
printf("Please Enter Values For Matrix 1\n");
read_mat(mat_a);
printf("Please Enter Values For Matrix 2\n");
read_mat(mat_b);
mult_mat(mat_a, mat_b, result_mat);
printf("The First Matrix Is :- \n");
print_mat(mat_a);
printf("The Second Matrix Is :- \n");
print_mat(mat_b);
printf("The Resultant Matrix Is :- \n");
print_mat(result_mat);
return 0;
}
the outout I am getting:
enter image description here
thanks!
improve your print function
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%3d), ", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a C problem where I need to reverse all the numbers on the 5th column of a 2x5 matrix.
So if I have
1 2 3 4 89
3 8 6 8 91
This will become
1 2 3 4 98
3 8 6 8 19
The code I've written so far is:
#include <stdio.h>
void inverse() {
int reversedNumber = 0, remainder, mat[10][10], i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
while (mat[i][j] != 0) {
remainder = mat[i][j] % 10;
reversedNumber = reversedNumber * 10 + remainder;
mat[i][j] /= 10;
}
}
printf("Reversed Number = %d", reversedNumber);
}
void main()
{
int mat[10][10], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
inverse(mat[1][5]);
}
After running this I get a ridiculously large number! What should I modify?
There are number of things that can be improved in the code.
First of all you have to set reversedNumber zero inside the innermost loop, this is the reason you get large numbers.
You pass an argument to the function, but the definition is incorrect for the same.
Also, you have stated that you only need to reverse the 5th column, better make call to a function that reverses a single number.
#include<stdio.h>
int inverse(int num) {
int reversednum = 0;
while(num){
reversednum = reversednum*10 + num%10;
num /= 10;
}
return reversednum;
}
void main(){
int mat[10][10],i,j;
printf("Enter your matrix\n");
for(i=0;i<2;i++)
for(j=0;j<5;j++){
scanf("%d",&mat[i][j]);
}
printf("\nHere is your matrix:\n");
for(i=0;i<2;i++){
for(j=0;j<5;j++){
printf("%d ",mat[i][j]);
if(j == 4) mat[i][j] = inverse(mat[i][j]);
}
printf("\n");
}
}
mistakes in your program:
-your function doesnt expect anything i.e empty parameter but you are sending matrix as parameter.
do not unnecessarily use matrix of size [10][10] when your matrix of 2*5
send 'mat' as parameter(i.e address of your matrix) to function inverse
#include <stdio.h>
int main() {
//code
int mat[10][10], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
if(j==4) //only for 5th column
{
// int temp=mat[i][j]; // can use temporary variable instead of changing actual value matrix (better option)
int remainder, reverse =0;
while(mat[i][j]>0)
{
remainder=mat[i][j]%10;
reverse=reverse*10 + remainder;
mat[i][j]=mat[i][j]/10;
}
mat[i][j]=reverse;
}
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Edited: modified code from the question
#include <stdio.h>
void inverse(int mat1[2][5]) {
int i, j;
for (i = 0; i < 2; i++){
int j=4;
int reversedNumber = 0, remainder=0;
while (mat1[i][j] > 0) {
remainder = mat1[i][j] % 10;
reversedNumber = reversedNumber * 10 + remainder;
mat1[i][j] /= 10;
}
printf("Reversed Number = %d\n",reversedNumber);
}
}
void main()
{
int mat[2][5], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
inverse(mat);
}
hope this helps.
You have to pass the matrix to the inverse function so that the matrix (mat) can be modified. If you declare a separate mat array inside inverse then that's a different scope. You also have to figure out how many digits there are in the number. You can use <math.h> functions, or the example below uses basic calculations.
void inverse(int mat[2][5])
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 5; j++)
{
int n = mat[i][j];
int digits = 0;
while(n > 0)
{
digits++;
n /= 10;
}
n = mat[i][j];
int rev = 0;
while(digits > 0)
{
int x = n % 10;
for(int c = 0; c < digits - 1; c++)
x *= 10;
rev += x;
n /= 10;
digits--;
}
mat[i][j] = rev;
}
}
int main(void)
{
int mat[2][5] = {
1, 2, 3, 4, 89,
3, 8, 6, 8, 91 };
inverse(mat);
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 5; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
return 0;
}
I've just recently started trying to learn C through the tutorials on Wikibooks. I've read the beginning C pages listed here and am attempting to do the exercises. I'm having a problem with the second question on loops: wherein I'm trying to make a function to output a triangle made up of lines of * characters, where the height is 2n-1 if the width is n. My first thought was to make a nested loop, the outer of which would create a variable for the line number, and compare it to the max height. The inner loop would create a variable that would essentially serve as the index of the * character within that particular line. My problem is I don't know how to deal with making the lines after the max width decrease in size. Can anyone point me in the right direction? Here is my code:
#include <stdio.h>
void triangle(int);
int main() {
int width;
printf("%s", "Please enter a width for your triangle: ");
scanf("%d", & width);
triangle(width);
return 0;
}
void triangle(int width) {
for (int line = 1; line <= (2 * width) - 1; line++) {
for (int i = 0; i < line && i < width; i++) {
printf("%s", "*");
}
printf("%s", "\n");
}
}
Try this:
void triangle(int width) {
int line, i, rev = 0;
for (line = 1; line < width; ++line) {
for (i = 0; i < line && i < width; i++) {
printf("*");
}
printf("\n");
}
for (; line; --line) {
for (i = 0; i < line && i < width; i++) {
printf("*");
}
printf("\n");
}
}
if you want to do it in one pair of nested for loops try this:
void triangle(int width) {
int i, j, height, tmp;
height = 2 * width - 1;
tmp = 1;
for (i = 0; i < height; i++) {
for (j = 0; j < tmp; j++) {
putchar('*');
}
putchar('\n');
if (i < height / 2) {
tmp++;
} else {
tmp--;
}
}
}
I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}