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");
}
Related
I want to draw an image like this (for size 8x8):
##..##..##..##..
##..##..##..##..
..##..##..##..##
..##..##..##..##
##..##..##..##..
##..##..##..##..
..##..##..##..##
..##..##..##..##
##..##..##..##..
##..##..##..##..
..##..##..##..##
..##..##..##..##
##..##..##..##..
##..##..##..##..
..##..##..##..##
..##..##..##..##
My code is:
{
int rows, cols, i, j, k;
scanf("%d", &rows);
scanf("%d", &cols);
k = 1;
for(i=1; i<=rows; i++)
{
for(j=1; j<=cols; j++)
{
if(k == 1)
{
printf("##");
}
else
{
printf("..");
}
k *= -1;
}
if(cols % 2 == 0)
{
k *= -1;
}
printf("\n");
}
return 0;
This code works good, but it's not like in the image!
That's the different approach to the solution. The core idea is to get a base string with 1/2 additional repetition of the pattern. Then you basically "shift" the string back and forth by two positions using pointer arithmetic (to manage the beginning of the string) and putting the null character \0 where appropriate (to manage the end of the string).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main () {
int rows = 0;
int cols = 0;
/* From the example provided I assume that the number of columns
* and rows should be a multiple of 2
* even though the OP hasn't explicitly mentioned it
*/
scanf("%d %d", &rows, &cols);
// input validation goes here
// generating the base string
// notice that the string is 2 characters longer than required
char *str = malloc(sizeof(*str) * (cols + 1) * 2 + 1);
for (int i = 0; i < cols / 2; ++i) {
strcpy(str + i * 4, "##..");
}
strcpy(str + strlen(str), "##");
int n = 0;
int len = strlen(str);
for (int i = 0; i < rows; ++i) {
n = i % 2;
// switching back and forth between null character '\0' and '#'
str[len - 2] = n * 35; //35 - ASCII code for a '#'
// using pointer arithmetic we send to function either first element
// or third
printf("%s\n%s\n", str + n * 2, str + n * 2);
}
free(str);
return 0;
}
EDITED: Generalized the solution for arbitrary (but even!) number of rows and columns.
This code does what you ask for, I think.
#include <stdio.h>
int main() {
int rows, cols;
scanf("%d", &rows);
scanf("%d", &cols);
rows = rows*2;
int k = 0;
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
if(k < 2 && j % 2 == 0) {
printf("##");
}
else if(2 <= k && j % 2 != 0) {
printf("##");
}
else {
printf("..");
}
}
k++;
if(k == 4) {
k = 0;
}
printf("\n");
}
return 0;
}
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;
}
if i give an input of 13, output should be like:
*
**
***
****
***
i've reached only this far:
#include <stdio.h>
int main()
{
int i, j, n = 13;
for(i = 1 ; i <= n ; i++)
{
for ( j = 1; j <= i ; j++)
{
printf("*");
}
t = ((i * (i + 1)) / 2);
printf("\n");
}
printf("%d",t);
return 0;
}
stars should be increasing by 1 in every line, but program will stop when total printed stars reach my inputted number. i am not sure how to start and where to end. i am very new to "for" loops.
You can do something like this
int n,i=1,j,coun=0;
scanf("%d",&n);
while(1)
{
if(i+coun<=n)
{
for(j=0;j<i;j++)
printf("*");
printf("\n");
coun+=i;
}
else
{
for(j=0;j<n-coun;j++)
printf("*");
printf("\n");
break;
}
i++;
}
#include <stdio.h>
int main(){
int i, j, k, n = 13;
for(k = j = i = 1 ; i <= n ; i++, j++){
putchar('*');
if(j==k){
putchar('\n');
j = 0;
++k;
}
}
return 0;
}
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.