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;
}
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;
}
I'm trying to print out a hollow, open tent shape using asterisk stars "*". The code uses two for loops, the first for the rows, and the other for the columns.
following is my code:
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
} else {
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf(" ");
}
if(j == n) {
printf("*");
for(j = 1; j <= n; j++) {
printf(" ");
}
}
}
}
}
int main()
{
printTent(4);
}
Output obtained:
* * * *
Desired output:
*
* *
* *
* *
I don't think you will need that
if (n == 1) {
printf("*");
}
We can take care of that in what you've written in the else part.
For n=4, the number of spaces to be printed at the start of each line is 3, 2, 1 & 0.
You seem to be trying to accomplish that with your first inner loop. But
for(j = 0; j < n; j++) {
printf(" ");
}
will always print n spaces. We need to reduce the number of spaces printed by 1 on each iteration of the outer loop.
Coming to your second loop,
for(j = 1; j <= n; j++) {
printf(" ");
}
This has a similar problem only difference being the incrementation of the number of spaces printed.
Try something like this
void printTentNMMod(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
for(j = i; j < n; j++) {
printf(" ");
}
printf("*");
if(i!=0)
{
for(j=0; j<2*(i-1)+1; ++j)
{
printf(" ");
}
printf("*");
}
printf("\n");
}
}
Also, you could shorten this to
void printTent(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
printf("%*c", n-i, '*');
if(i!=0)
{
printf("%*c", 2*i, '*');
}
printf("\n");
}
}
The * in %*c will set the number of places occupied by the character printed by the %c.
I've finished it and I have written annotation.
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
}
else {
for (i = 0; i < n; i++) {
for (j = 0; j < n -i; j++) {// you should use n-i instead of n because the number of spaces is decreasing
printf(" ");
}
if (j == n-i) { //
printf("*");
for (j = 1; j <= i * 2 - 1; j++)//this loop outputs spaces between two "*"
{
printf(" ");
}
if (i != 0)//the first line only needs one "*"
printf("*");
printf("\n"); //Line breaks
}
}
}
}
Another way.
#include <stdio.h>
int main() {
int i, j;
int height = 5;
for(i = height; i > 0; i--) {
for(j = 1; j < height * 2; j++) {
if(j == i || j == height * 2 - i)
printf("*");
else
printf(" ");
}
puts("");
}
return 0;
}
Output
*
* *
* *
* *
* *
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.
At the moment I'm wondering how can I swap the values already in a two dimensional array, in this case I managed to swap them, however, I can't seem to be able to swap them back... How can I make a program swap the values back and forward for the array?
Code:
#include <stdio.h>
void main()
{
//declaration of values
int d = 0;
char value;
float list[5][6], swaplist[5][6];
float average_odd, sum = 0, average_even, sum1 = 0;
//Command list so that the user knows what he can do in this program
printf("Command list:\t \n\nCommand: \t\t Output: ");
printf("\n \"A\" \t\t Declare values of a list.\n \"O\" \t\t Obtain the average value of the even and odd column\n\t\t values in the list.\n");
printf(" \"I\" \t\t Exchange the values on the even columns with the odd ones.\n \"P\" \t\t Print the values of the list.\n \"S\" \t\t End program.");
//========================================================================================================
while (d != 1)
{
printf("\n\nInsert value: ");
scanf(" %c", &value);
//=========================================================================================================
if (value == 'a' || value == 'A')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
printf("Insert value of element %d in column %d: ", i + 1, j + 1);
scanf("%f", &list[i][j]);
swaplist [i][j] = list[i][j];
}
}
}
//=========================================================================================================
if (value == 's' || value == 'S')
{
d++;
}
//=========================================================================================================
if (value == 'P' || value == 'p')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
printf("%2.2f ", list[i][j]);
}
printf("\n");
}
}
//========================================================================================================
if (value == 'o' || value == 'O')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j = j + 2)
{
sum += list[i][j];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 1; j < 6; j = j + 2)
{
sum1 += list[i][j];
}
}
average_even = sum1 / 15;
printf("The average of the even columns = %.2f\n", average_even);
average_odd = sum / 15;
printf("The average of the odd columns = %.2f\n", average_odd);
}
//=======================================================================================================
if (value == 'i' || value == 'I')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j = j + 2)
{
list[i][j] = swaplist[i][j+1];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 1; j < 6; j = j + 2)
{
list[i][j] = swaplist[i][j - 1];
}
}
}
}
}
After swapping, you don't change the state of the swap list. At first, the swap list is a copy of the unswapped matrix. You don't change it to reflect the changes in the matrix and don't keep track of any other state.
You don't really need the swap list. I suggest that you just do a regular swapping of adjacent columns cell by cell:
if (value == 'i' || value == 'I') {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j = j + 2) {
float swap = list[i][j];
list[i][j] = list[i][j + 1];
list[i][j + 1] = swap;
}
}
}
That will toggle the matrix on repeated swaps.
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.