So i have assignment to type a code which will ask me for a "w". After i type in a number it will create a rhombus with a diagonal which is 2w. The rhombus must be made of intervals and * . The problem that i face now is that when i type w=5 the diagonal is 5 instead of 10 ....
main()
{
int w;
int i;
int j;
printf("w: ");
scanf("%d", &w);
printf("");
i = 0;
while (w >= i)
{
for (j = 0; j < (w - i); j++)
printf(" ");
for (j = 0; j < i + 1; j++) {
printf("*");
if (j <= i) {
printf(" ");
}
}
printf("\n");
i = i + 1;
}
i = w - 1;
while (i >= 0)
{
for (j = 0; j < (w - i); j++)
printf(" ");
for (j = 0; j < i + 1; j++) {
printf("*");
if (j <= i) {
printf(" ");
}
}
printf("\n");
i = i - 1;
}
return 0;
}
If you add the line w = 2*(w-1) + 1; before any of the loops then you get the correct number of *s to print out ( I just looked for the pattern you were getting and modified the input)
You can also do this problem with just one loop!
Edit:
#include <stdio.h>
#include <math.h>
#define min(a, b) (((a) < (b)) ? (a) : (b))
int main(){
int input, row, column;
printf("input a number: ");
scanf("%d", &input);
printf("");
input = 2*(input-1) + 1;
int pivot = input;
int total_spaces = input*2+1;
for(row = 0; row < total_spaces; row++){
for(column = 0; column < total_spaces; column ++){
if(min(abs(total_spaces - row -1),abs(0 - row)) +
min(abs(total_spaces - column -1),abs(0 - column))
>= input && (row%2 != column %2)){
printf("*");
}
else printf(" ");
}
printf("\n");
}
}
That was a doozy!
I ran your program and I had this :
/a.out
w: 5
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
I do not see where your diagonal is 5. Can you be more specific ?
Also, I understand how this may not be important for you but as-is your code won't compile.
At least add int before your main function.
Related
I'm confused to print this type of pattern given below:
* *
* *
*
* *
* *
I've tried this:
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of base\n>>> ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j + n; j++)
{
if (// ??? )
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
but I don't know what is the condition of if statement
Help Me to solve this please
The pattern consists of exactly n * 2 - 1 rows and columns. So you can run an outer loop to iterate through rows with structure for(i=1; i<= count; i++); where count = n * 2 - 1. Each row contains exactly n * 2 - 1 columns. So, run inner loop as for(j=1; j<=count; j++). And inside this loop we need stars to be printed when row and column number both are equal (i.e. print star whenever if(i == j)) and if(j == count - i + 1). For example:
#include <stdio.h>
int main() {
int i, j, n;
int count;
printf("Enter n: ");
scanf("%d", &n);
count = n * 2 - 1;
for (i = 1; i <= count; i++) {
for (j = 1; j <= count; j++) {
if (j == i || (j == count - i + 1)) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
To not change any code and answer your question "what goes in the if"
n - 1 - i == j || j == i
this does
This will work fine !! :)
#include<iostream>
using namespace std;
int main()
{
int n,m=0;
int m2=0;
cin>>n;
int f=-1,l=n;
for(int i=0;i<n;i++)
{
f=f+1;
l=l-1;
for(int j=0;j<n;j++)
{
if(j==f || j==l) cout<<"*";
else cout<<" ";
}
cout<<endl;
}
}
So, I have a code that creates a 7x7 board with Dynamically Allocated Arrays and inside of a board is full with "?" and what I want to do is creating a new function and inside a function, I used rand command to get random numbers like this,
int random() {
return ((rand() % 7) + 1);
}
Therefore, I had a problem changing 6 random numbers in a board and my Code is below,
This one below is the one I tried to get random numbers for an Array,
printf("Enter number: ");
scanf("%d", &b);
char *rando = (char *)malloc(7 * 7 * sizeof(char));
for (i = 0; i < b; i++) {
rand1 = random();
rand2 = random();
*(rando + rand1 + rand2) = '*';
}
And this one is where I printed the "?" signs and also where I tried to change 6 different signs and it only prints out "else" part ignoring the "if" for some reason
for (j = 0; j < 7; j++) {
if (*(board + i + j) == *(rando + i + j))
printf("| %c ", *(rando + i + j));
else
printf("| %c ", *(board + i + j));
}
And my whole code is this, it's kinda long but most of them are for a nice looking board
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int random() {
return ((rand() % 7) + 1);
}
int main() {
int i, j, k, rand1, rand2, b;
srand(time(NULL));
printf("Enter number: ");
scanf("%d", &b);
char *rando = (char *)malloc(7 * 7 * sizeof(char));
for (i = 0; i < b; i++) {
rand1 = random();
rand2 = random();
*(rando + rand1 + rand2) = '*';
}
char *board = (char *)malloc(7 * 7 * sizeof(char));
for (i = 0; i < 7; i++) {
for (j = 0; j < 7; j++) {
*(board + i + j) = '?';
}
}
for (i = 1; i <= 7; i++) {
printf("%4d", i);
}
printf("\n ");
for (i = 0; i < 7; i++) {
printf("+---");
}
printf("+\n");
for (i = 0; i < 7; i++) {
printf("%d ",i);
for (j = 0; j < 7; j++) {
if (*(board + i + j) == *(rando + i + j))
printf("| %c ", *(rando + i + j));
else
printf("| %c ", *(board + i + j));
}
printf("|\n");
for (k = 0; k <= 7; k++)
if (k == 0)
printf(" ");
else
printf("+---");
printf("+\n");
}
}
I pointed out important parts that I'm stuck with but still not sure if there is a problem in other parts of my code so I showed it here, just in case.
There are multiple problems in your code:
you allocate the 7x7 matrix as a single array of 49 characters. Yet you do not index into this array with the correct formula. The element at position (i,j) is accessed as *(board + 7 * i + j), not *(board + i + j).
It would be simpler to declare rando and board to point to a 2D matrix and use the [] syntax:
char (*board)[7] = malloc(7 * sizeof(*board));
and use board[i][j].
Furthermore, the rando array is uninitialized, so the program has undefined behavior when reading the contents of the elements that have not been set to '*' in the first loop. You must initialize this array with '?'. You can do this with memset().
the function random() returns an integer in the range 1 to 7 inclusive. You should instead compute pseudo-random coordinates in the range 0 to 6. Remove the +1;
the test in the board printing loop is useless: if the board element at position i,j is the same as in the rando matrix you print the rando element otherwise t board element. This always prints the board element.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int random(void) {
return rand() % 7;
}
void init_board(char board[7][7]) {
// board can be initialized with 2 nested loops or
// a single call to
//memset(board, '?', 7 * 7);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = '?';
}
}
}
void print_board(char board[7][7]) {
for (int i = 0; i < 7; i++) {
printf("%4d", i + 1);
}
printf("\n ");
for (int i = 0; i < 7; i++) {
printf("+---");
}
printf("+\n");
for (int i = 0; i < 7; i++) {
printf("%d ", i + 1);
for (int j = 0; j < 7; j++) {
printf("| %c ", board[i][j]);
}
printf("|\n");
printf(" ");
for (int j = 0; j < 7; j++) {
printf("+---");
}
printf("+\n");
}
}
int main() {
int b;
srand(time(NULL));
printf("Enter number: ");
scanf("%d", &b);
char (*rando)[7] = malloc(7 * sizeof(*rando));
if (!rando)
return 1;
init_board(rando);
for (int i = 0; i < b; i++) {
int rand1 = random();
int rand2 = random();
rando[rand1][rand2] = '*';
}
char (*board)[7] = malloc(7 * sizeof(*board));
if (!board)
return 1;
init_board(board);
/* print the mines */
print_board(rando);
/* print the board */
print_board(board);
free(rando);
free(board);
return 0;
}
my code is the following:
if (a == 4)
{
if (n >= 3 && n <= 18)
{
width = n + 2;
for (a = 1; a <= width; a++)
{
printf("*");
}
printf("\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (i == 1 || i == n || j == 1 || j == n)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
This code prints a pattern of lines depending on n.
e.g. For n = 7
*********
*******
* *
* *
* *
* *
* *
*******
What I am trying to do is to print a space in front of every line like this
*********
*******
* *
* *
* *
* *
* *
*******
It should print the empty box in the middle of the upper line.
Add a printf(" "); between the 2 loops.
for (i = 1; i <= n; i++)
{
printf(" ");
for (j = 1; j <= n; j++)
{
...
}
printf(" \n");
}
like this code:
#include<stdio.h>
int main() {
int n = 7;
if (n >= 3 && n <= 18)
{
int width = n + 2;
for (int i = 0; i != width; ++i)
printf("*");
printf("\n");
for (int i = 0; i != n; ++i)
{
printf(" ");
for (int j = 0; j != n; ++j)
{
if (i == 0 || i == n - 1 || j == 0 || j == n - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
return 0;
}
I have this code:
#include <stdio.h>
int main()
{
int n, i, c;
printf("Size");
scanf("%d", &n);
for (i = 1; i <= (n + 1); i++)
{
printf("*");
printf(" ");
}
printf("\n");
for (int a = 1; a <= (((n + 1) * 2)-1); a++)
{
printf("*");
}
printf("\n");
}
What I am trying to do is, after the first line with the spaces. I want to print the same line which is (((n + 1) * 2) - 1) n times in new lines which n is given from the if. For example to give a better understanding
For n = 4:
* * * * *
********* 1st
********* 2nd
********* 3rd
********* 4th
As you can see, it will do the calculation and print the line with spaces and in the next lines it will do the calculation again but it will print the line n times without spaces. I can't find out how to print them in a new line every time.
A loop is missing:
#include <stdio.h>
int main()
{
int n, i, c;
printf("Size");
scanf("%d", &n);
for (i = 1; i <= (n + 1); i++)
{
printf("*");
printf(" ");
}
printf("\n");
size_t width = (((n + 1) * 2)-1);
for (int j = 0; j < n; j++) { /* loop for lines */
for (int a = 1; a <= width; a++) /* loop * in lines */
{
printf("*");
}
printf("\n");
}
}
add another for loop
for(j= 0; j< n;++j)/*do this inner for loop and scope code n times*/
{
for ( a = 1; a <= (((n + 1) * 2)-1); a++)
{
printf("*");
}
printf("\n");
}
I had a program that I had to create which took a user inputted odd number between 1 to 99 and created a magic square which I have successfully done.
#include <stdio.h>
int main()
{
int n;
printf("\nThis programs creates a magic squares of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");
printf("Enter the size of magic square: ");
scanf("%d", &n);
int magicsq[99][99];
int row = 0;
int col = (n - 1) / 2;
magicsq[row][col] = 1;
int i;
for(i = 2; i <= n * n; i++)
{
row = (row + n - 1) % n;
/* printf("i = %d\n", i);
printf("row %d\n", row);
col = (col % n); */
col = (col + 1) % n;
/* printf("col %d\n\n", col); */
if(magicsq[row][col] != 0)
{
row = (n + row + 2) % n;
col = (n + col - 1) % n;
/* printf("n = %d ; row = %d ; col = %d\n", n, row, col); */
}
magicsq[row][col] = i;
}
printf("\n");
int j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%5d", magicsq[i][j]);
}
printf("\n");
}
return 0;
}
I came across another question which stated me to introduce two functions namely, void create_magic_square(int n, char magic_square[99][99]) and void print_magic_square(int n, char magic_square[99][99])
#include <stdio.h>
void create_magic_square(int n, char magic_square[99][99]);
void print_magic_square(int n, char magic_square[99][99]);
int main()
{
int n;
char **magic_square;
printf("\nThis programs creates a magic squares of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");
printf("Enter the size of magic square: ");
scanf("%d", &n);
create_magic_square(n, magic_square[99][99]);
print_magic_square(n, magic_square[99][99]);
return 0;
}
void create_magic_square(int n, char magic_square[99][99])
{
int *magicsq[][];
magic_square[99][99] = magicsq[][];
int row = 0;
int col = (n - 1) / 2;
magicsq[row][col] = 1;
int i;
for(i = 2; i <= n * n; i++)
{
row = (row + n - 1) % n;
printf("i = %d\n", i);
printf("row %d\n", row);
/* col = (col % n); */
col = (col + 1) % n;
printf("col %d\n\n", col);
if(magicsq[row][col] != 0)
{
row = (n + row + 2) % n;
col = (n + col - 1) % n;
printf("n = %d ; row = %d ; col = %d\n", n, row, col);
}
magicsq[row][col] = i;
}
}
void print_magic_square(int n, char magic_square[99][99])
{
printf("\n");
int j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%5d", magicsq[i][j]);
}
printf("\n");
}
}
Upon compiling, I am filled with tons of errors on my declaration of char type array and my usage of the parameters. I have googled char() parameters and types but I am not able to incorporate into my program.
I am new to c so constructive criticism is appreciated and helps me learn better if I am doing anything wrong.
Language: c99 ; Compiler: gcc
Of course there are lot of mistakes in your code related to pointers and arrays and those can't be completely explained in a single answer here.
I recommend you to study the pointers properly. Relationship between pointers and arrays, Multidimensional arrays are all need to be thoroughly understood before attempting to do what you are trying to.