I'm trying to decrements the space loop value by 2 at the row 6 position. After that, as it be like decremented by 1 at the 7th and 8th row so on.
I'm trying to print a pyramid structure with numbers.
#include <stdio.h>
main() {
int i, j, n, s, p, m;
scanf("%d", &n);
int num = 1;
for (i = 1; i <= n; i++) {
printf("row:%d ", i);
s = 0;
for (s = i; s <= n; s++) {
if (i == (n / 2) + 1 && j == (n / 2) + 1) {
s = s + 1;
printf("%d", s);
}
// if ((i == (n / 2) + 1)) {
// s = s + 2;
//// printf("in:%d ", s);
// }
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%d", num);
num = num + 1;
}
for (p = 1; p < i; p++) {
num = num - 1;
printf("%d", num);
}
printf("\n");
}
}
What type of pyramid you are printing?
If you want to decrease counter by 2 you can use following code:
for(int i=10; i>0 ; i-=2)
this will decrease counter by 2 in each loop.
Related
I'm trying to write a program that prints out a christmas tree that looks like this:
The user inputs the height, in this example the height is 6. If the input is in range from 0 to 3, the height should be 3, because otherwise it's not printable, and if the input is less than 0, the program should terminate.
My code for some odd reason is infinitely printing the 'Input height'. Where is the error?
Here's my code snippet:
#include <stdio.h>
void main(){
int i, j, n, s;
while (1){
printf("Input height: ");
scanf("%d", &n);
if (n < 0) break;
if (n == 0 || n == 1 || n == 2 || n == 3)
s == 3;
else
s == n;
for (i = 0; i < s; i++){
for (j = 0; j < 2*s - 1; j++){
if (j > s - (i - 1) && j < (s + (i - 1)) - 1)
printf("*.");
if (j == s + (i - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}
for (j = 0; j < 2*s - 1; j++){
if (j == s - 1 || j == s || j == s + 1)
printf("*");
else
printf(" ");
}
}
}
The lines: s == 3; and s == n; do absolutely nothing.
== is a comparison, not an assignment.
Here is much better code:
#include <stdio.h>
int main(void) {
int n = 8;
char row[2*n];
for( int i=0; i<2*n-1; i+=2 )
{
strcpy(row+i, "*.");
}
for(int i=0; i<n; ++i)
{
printf("%*.*s\n", n+i+1, 2*i+1, row);
}
printf("%*s\n", n+2, "***");
return 0;
}
Result:
Success #stdin #stdout 0s 5464KB
*
*.*
*.*.*
*.*.*.*
*.*.*.*.*
*.*.*.*.*.*
*.*.*.*.*.*.*
*.*.*.*.*.*.*.*
***
With a little creativity, I made the program even shorter with only a single for-loop.
#include <stdio.h>
int main(void) {
int n = 8;
char row[2*n];
strcpy(row, "*");
for( int i=0; i<n; ++i )
{
printf("%*s\n", n+i, row);
strcat(row, ".*");
}
printf("%*s\n", n+1, "***");
return 0;
}
As mentioned by others there are some issues with you mixing up == and =.
I will be posting a version that prints out the christmas tree but leaves out the . that you also want to include, as you should be able to finish it yourself.
#include <stdio.h>
int main()
{
int i, j, n, s;
while (1)
{
printf("Input height: ");
scanf("%d", &n);
// if input is negative, exit
if (n < 0)
{
break;
}
// if input is 0,1,2 or 3 change to 3
if (n == 0 || n == 1 || n == 2 || n == 3)
{
s = 3;
}
else
{
s = n;
}
// loop through each row
for (i = 0; i < s; i++)
{
// loop through each column
for (j = 0; j < 2 * s - 1; j++)
{
// if column is within the tree print a star
if (j >= s - i - 1 && j <= s + i - 1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
// print base of tree
for (j = 0; j < 2 * s - 2; j++)
{
// if column is part of base print star
if (j == s - 2 || j == s - 1 || j == s)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
A simple solution:
#include <stdio.h>
int main(){
int i=0, j=0, n=0, s=0;
while (1){
printf("Input height: ");
scanf("%d", &n);
printf("\n");
if (n < 0) break;
s = (n <= 3) ? 3 : n;
for (i=0; i < s; ++i){ // rows
for (j=0; j < s-i; ++j) // white spaces
printf(" ");
for (int k=0; k < i; ++k) // *.
printf("*.");
printf("*\n"); // always, unique or last *
}
for (i=0; i < s-1 ; ++i)
printf(" ");
printf("***\n\n");
}
return 0;
}
Here is my code so far for producing Fibonacci numbers. I keep getting a zero because it it adding a space bar at the very end, but I need a space between numbers.
#include<stdio.h>
void fib(int);
int main()
{
int n = 0;
while (n <= 0 || n > 70)
{
printf("enter the value(between 0 and 70)");
scanf("%d", &n);
if (n <= 0 || n >= 70)
printf("wrong input\n");
}
fib(n);
return 0;
}
void fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[n]; // 1 extra to handle case, n = 0
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
printf("%d %d ", f[0], f[1]);
for (i = 2; i < n; i++)
{
f[i] = f[i - 1] + f[i - 2];
printf("%d ", f[i]);
}
}
Remove the trailing space in the first printf and print the space before instead of after the number in the loop.
printf("%d %d", f[0], f[1]);
for (i = 2; i < n; i++) {
f[i] = f[i - 1] + f[i - 2];
printf(" %d", f[i]);
}
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;
}
I want here to implement the value of array[i][j] into itself, but firstly I have to check if it is in range between example: -99 and 99. If the input is out of these boundaries, it should stop the program.
I tried it with a do-while loop and just now I tried while loop.
#include <stdio.h>
#include <stdlib.h>
int main(){
int array[2][2], i, n, j;
/*
do
{
printf("Value= ");
scanf("%d", &n);
array[i][j] = n;
i++;
j++;
}
while(n < 99 && n > -99);
*/
while(array[i][j] < 99 && array[i][j] > -99){
for(i = 0; i < 2; ++i){
for(j = 0; j < 2; ++j){
printf("Value= ");
scanf("%d", &array[i][j]);
}
}
}
// Print the result
for(i = 0; i < 2; ++i){
for(j = 0; j < 2; ++j){
printf("\n[%d][%d]: ", array[i][j]);
}
}
}
I got a endless loop which doesn't exit if the value is incorrect (out of these boundaries).
Try this, tested and works:
int *array, i, val, n, m;
printf("Put in array size in the form of n-m where n is number of rows and m is number of columns: ");
scanf("%d-%d", &n, &m);
array = (int *) malloc(sizeof(int) * n * m);
i = 0;
while (i < n * m) {
printf("Value for row: %d, column: %d: ", i / m + 1, i % m + 1);
scanf("%d", &val);
if (val > 99 || val < -99) continue;
*(array + i) = val;
i++;
}
for (i = 0; i < n * m; i++) {
if (i > 0 && i % n == 0) printf("\n");
printf("%d\t", *(array + i));
}
free(array);
Without pointers (Variable sized arrays does not work on C90, needs newer standard, or you may use fixed sized arrays):
int i, val, n, m;
printf("Put in array size in the form of n-m where n is number of rows and m is number of columns: ");
scanf("%d-%d", &n, &m);
int array[n][m];
i = 0;
while (i < n * m) {
printf("Value for row: %d, column: %d: ", i / m + 1, i % m + 1);
scanf("%d", &val);
if (val > 99 || val < -99) continue;
array[i / m][i % m] = val;
i++;
}
for (i = 0; i < n * m; i++) {
if (i > 0 && i % n == 0) printf("\n");
printf("%d\t", array[i / m][i % m]);
}
In your array undefeated values, in the first while you try to check random number in your memory. And so your i j will be random number.
Put if statement in your for loop to check the value in array, and delete while loop
Description of the problem :
Compute the number of all the sequences which go up down from some input n.
So the user input n; with that n then I create an array of numbers 1..n and then number the sequences with that property
Example: n = 4
1 3 2 4
1 4 2 3
2 3 1 4
2 4 1 3
3 4 1 2
Answer: 5
My program works but for some reason I sometimes get 0 instead of the answer.
#include <stdio.h>
#include <stdlib.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void swap(int *fir, int *sec) {
int temp = *fir;
*fir = *sec;
*sec = temp;
}
void permute(int *array, int i, int length, int *count) {
if (length == 2) {
*count = 1;
return;
}
if (length == i) {
int v = 0, flag = 1;
while (v < length) {
if (v % 2 == 0) {
if (array[v] < array[v + 1]) {
v++;
} else {
flag = 0;
return;
}
}
if (v % 2 != 0) {
if (array[v] > array[v + 1]) {
v++;
} else {
flag = 0;
return;
}
}
}
if (flag == 1) {
/*
int a;
for (a = 0; a < length; a++)
printf("%d", array[a]);
printf("\n");
*/
*count = *count + 1;
}
}
int j = i;
for (j = i; j < length; j++) {
swap(array + i, array + j);
permute(array, i + 1, length, count);
swap(array + i, array + j);
}
return;
}
int main(int argc, char **argv) {
int n;
scanf("%d", &n);
int *arr = safeMalloc(n * sizeof(int));
int i;
for (i = 0; i < n; i++) {
arr[i] = i + 1;
}
int count = 0;
permute(arr, 0, n, &count);
printf("%d\n", count);
return 0;
}
You basically generate all permutations of the array elements and count the valid ones.
Your code has a minor flaw:
the loop while (v < length) { goes one step too far: you access tab[v + 1] so the loop should stop at v < length - 1. As currently coded, it has undefined behavior.
You can further simply the code:
there should be no need to special case length == 2.
flag useless as you always return when you clear it.
if (v % 2 != 0) is redundant: else would suffice.
Here is a fixed and simplified version:
#include <stdio.h>
#include <stdlib.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void swap(int *fir, int *sec) {
int temp = *fir;
*fir = *sec;
*sec = temp;
}
void permutate(int *array, int i, int length, int *count) {
if (i == length) {
for (int v = 0; v < length - 1; v++) {
if (v % 2 == 0) {
if (array[v] >= array[v + 1]) {
return;
}
} else {
if (array[v] <= array[v + 1]) {
return;
}
}
}
*count = *count + 1;
} else {
for (int j = i; j < length; j++) {
swap(array + i, array + j);
permutate(array, i + 1, length, count);
swap(array + i, array + j);
}
}
}
int main(int argc, char **argv) {
int n;
if (scanf("%d", &n) == 1 && n > 0) {
int *arr = safeMalloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
int count = 0;
permutate(arr, 0, n, &count);
printf("%d\n", count);
}
return 0;
}
if you call tab(n,k) the number of updown sequence of length n with k being the last number in your sequence, you can write a recursive formula and implement it like that:
int N = 5+1;
int** tab = new int*[N];
for (int n = 0; n < N; n++) {
tab[n] = new int[N];
for (int k = 0; k < N; k++) {
tab[n][k] = 0;
}
}
tab[1][1] = 1;
for (int n = 2; n < N; n++) {
for (int k = 1; k <= n; k++) {
if (n % 2 == 0) {
for (int j = 0; j < k; j++) {
tab[n][k] += tab[n-1][j];
}
}
else {
for (int j = k; j < n; j++) {
tab[n][k] += tab[n-1][j];
}
}
}
}
int res = 0;
for (int j = 0; j < N; j++) {
res += tab[N - 1][j];
}
You can solve this without iterating through the permutations. Say you're trying to calculate f(n). Where can the new, high number go? It has to go in an 'up' position, which is an even position. You can have any valid sequence of odd length preceding it, and any valid sequence following it.
Let's say we're calculating f(n,k) where the highest val is in position k, zero indexed. This is zero for k even. For odd k we get:
f(n,k) = choose(n-1, k) * f(k) * f(n - k - 1)
To get f(n), sum f(n,k) over odd k < n.
We have to calculate the first few by hand.
f(0) = 1
f(1) = 1
f(2) = 1
f(3) = f(3,1) = choose(2,1) * f(1) * f(1) = 2 * 1 *1 = 2
f(4) = f(4,1) + f(4,3) = choose(3,1) * f(1) * f(2) + choose(3,3) * f(3) * f(0) = 3*1*1 + 1*2*1 = 5
f(5) = f(5,1) + f(5,3) = choose(4,1) * f(1) * f(3) + choose(4,3) * f(3) * f(1) = 4*1*2 + 4*2*1 = 16