Programming a reversed pyramid in c - c

Im working on an optional test review problem for an introduction to C class, and I need to have a program that prints out the following based upon what ever number a user enters:
Enter a number: 5
5
44
333
2222
11111
000000
11111
2222
333
44
5
So far this is the code that I have written
#include <stdio.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf_s("%d",&number);
for (row = 1; row <= number + 1; row++){
for (space = number; space >=row; space--){
printf(" ");
}
for(column = 1;column <= row; column++){
printf("%d",space);
}
printf("\n");
}
for (row = 1; row <=number;row++){
for(space = 1;space <= row;space++){
printf(" ");
}
for(column = number;column >=row;column--){
printf("%d",space);
}
printf("\n");
}
return 0;
}
This is the output that I get
Enter a number: 5
0
11
222
3333
44444
555555
22222
3333
444
55
6
I've spent quite a few hours trying to figure out how to print the upper half of the half diamond using the user entered numbers but I can't seem to figure it out. Could anyone point me in the right direction?

Your numbers are just off a bit, correct the printf calls and you're done:
First one:
printf("%d", number - space);
Second one:
printf("%d", space - 1);
A slightly better (more readable and a bit more logical) way would be to use other variables instead:
First one:
printf("%d", number + 1 - row);
Second one:
printf("%d", row);
Also note that some basic math can help you to realize the following:
Total number of rows: 2 * number + 1
Number of spaces: 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5 => abs(number - row) (If starting your rows with 0)
Number to print: Same as "Number of spaces"
Number count: 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 => 6 - number_of_spaces
This gives a much cleaner, more readable version with only one loop:
#include <stdio.h>
#include <math.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf_s("%d",&number);
for (row = 0; row <= number * 2; row++){
int number_of_spaces = abs(number - row);
int number_to_print = number_of_spaces;
int number_count = 6 - number_of_spaces;
for (space = 1; space <= number_of_spaces; space++){
printf(" ");
}
for(column = 1;column <= number_count; column++){
printf("%d", number_to_print);
}
printf("\n");
}
}
return 0;
}

To expand on schnaader's answer (which is perfectly fine and complete), you can improve your code even more, letting printf() do the spacing for you rather than doing a loop and several calls to printf():
printf("%*s", width, "");
Here width is replaced with the calculated space you'd like to fill. The precision * is a special placeholder that tells the function to take the actual precision/length from the parameter list. Since the string to print is empty, it will always fill the whole range with space characters.

for (row = 0; row <= number + number; row++) {
int t = row * (row <= number) + (number + number - row) * (row > number);
printf("%*c", number - t, ' ');
printf("%*d\n", t + 1, t);
}

There are numerous ways, I've just modified your code
#include <stdio.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf("%d",&number);
for (row = 1; row <= number + 1; row++){
for (space = number; space >=row; space--){
printf(" ");
}
for(column = 1;column <= row; column++){
printf("%d",number -space); //change1
}
printf("\n");
}
for (row = 1; row <=number;row++){
for(space = 0;space < row;space++){ //change 2
printf(" ");
}
for(column = number;column >=row;column--){
printf("%d",space);
}
printf("\n");
}
return 0;
}

the following code outputs the first half of the problem.
Notice the checking for errors in the call to scanf()
It compiles cleanly and works correctly
I leave it to you to complete the function for the second half of the output
which should be easy now that you have the first half
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row;
int i; // loop counter/index
int n; // user input
printf( "\nEnter the wedge width: ");
if(1 != scanf(" %d", &n) )
{ // then, scanf failed
perror( "scanf failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
for( row = 0; row <= n; row++ )
{
// print n-row ' '
for( i=0; i<(n-row); i++)
{
printf(" ");
}
// print row+1 '(n-row)'
for( i=0; i<(row+1); i++)
{
printf( "%1d", (n-row));
}
printf( "\n" );
}
// reverse the calculations to print the lower half
return 0;
} // end function: main

Related

Average value of numbers from N to 1000 (included), without even numbers which are divisible by 6 and 17

First I have to input N, N becomes the first number to be checked.
Input: 79
Output should be: 537.70.
int sum=0;
while(1)
{
scanf("%d", &n);
if(n>=10 && n<80)
{
break;
}
printf("New output:\n");
}
for(i=n;i<=1000;i++)
{
if(i%2==0 && i%6!=0 && i%17!=0)
{
sum+=i;
}
I didnt put (float)sum/N to get average because I'm doing something wrong with sum.
More input output:
Input: 10 Output: 505.21
Input: 44 Output: 521.18
As well as keeping a 'running sum', you also need to keep a count of how many numbers were used, so you can properly calculate the average:
#include <stdio.h>
int main(void)
{
int n;
printf("Enter start number: ");
scanf("%d", &n);
int sum = 0, count = 0;
for (int i = n; i <= 1000; ++i) {
if (!(i % 2) && (i % 6) && (i % 17)) {
sum += i;
++count;
}
}
printf("Average is: %.2f\n", (double)sum / (double)count);
return 0;
}
Input: 79
Output should be: 537.70.
Are you sure about this value? I get 538.70 - but I get the given values for the other test cases you cite.

Floyd's Triangle right pattern

I have to creat a program thats that asks from the user to enter a number of rows and then creats a floyd's triangle. The problem is i don't seem to manage to make this particular pattern:
1
2 3
4 5 6
7 8 9 10
I have only managed to creat the basic program
#include <stdio.h>
#include <stdlib.h>
int rows, r, c, a;
int number=1;
int main()
{
printf("Floyd Triangle\n");
printf("--------------");
printf("\nPlease enter an integer number of rows: ");
scanf("%d",&rows);
while(rows<=0)
{
printf("\nYou must enter an integer value: ");
scanf("%d",&rows);
}
for(r=1;r<=rows;r++)
{
for(c=1;c<=r;+c++)
{
printf("%d ", number++);
}
printf("\n");
}
there are no erros in my code so far
Just print some spaces before the first number in each row
// ...
for (r = 0; r < rows; r++) {
printsomespaces(r, rows); // prints some spaces depending on current row and total rows
for (c = 0; c < r; +c++) {
printf("%d ", number++);
}
printf("\n");
}
// ...
If you can't write your own function (no printsomespaces) use a loop instead:
//...
//printsomespaces(r, rows);
for (int space = 0; space < XXXXXXXX; space++) putchar(' ');
//...
where XXXXXXXX is some calculation using r and rows.
Try (untested) 2 * (rows - r) (2 is the width of each number: 1 for the number + 1 for the space).
i haven't learnt how to make my own functions yet. isnt there a way to accomplish this only by using loops?
There is. A main problem of this exercise is to compute the needed width of each column, which of course depends on the number in the bottom row. The count of digits of a number can be determined in various ways; perhaps the easiest is via the snprintf(char *s, size_t n, const char *format, ...) function, which
… returns the number of characters that would have been written
had n been sufficiently large…
If n is zero, nothing is written,
and s may be a null pointer.
// we need to compute the width the of widest number of each column
int width[rows];
const int max = rows*(rows+1)/2; // the greatest number
for (c=1; c<=rows; ++c) // see how many characters will be written
width[c-1] = snprintf(NULL, 0, "%d ", max-rows+c);
for (r=1; r<=rows; ++r, puts(""))
for (c=1; c<=rows; ++c)
if (c <= rows-r) // here comes an empty cell in this row
printf("%-*c", width[c-1], ' ');
else
printf("%-*d", width[c-1], number++);

How to reverse the position of a nested loop output?

I'm a beginner C programmer and a college freshman.
I need a little help here with a test i'm working on here.
I want to make a nested loop that shows a sorted number. Sorta like this:
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16
22 23 24 25 26 27 28
... ... ... and so on, depending the limit of rows you input
I already tried to make a crude trial-and-error test code:
int i;
int j;
int limit;
int number1 = 1;
int number2 = 3;
int spesial = 0;
printf("Input limit : ");
scanf("%d", &limit);
for (i=1;i<=limit;i++)
{
for(j=1;j<=i;j++)
{
if (i%2==0)
{
printf("%d ", number2);
number2--;
}
else
{
printf("%d ", number1);
}
number1++;
}
if (i%2==0)
{
number2=(i*6)-i+(spesial*1);
spesial+=1;
}
printf("\n");
}
I managed to make it sorted to the 7th rows, but the rest are not..
help please...
I want to know if we could actually control the position of the output without sorta crude our way like this.
Also, sorry for my English... I'm not really from an English speaking country and this is my first time posting/question in this site.
Thank you for reading this lengthy question and I hope you have a good day and good night.
https://ideone.com/yCxpHo:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int rows;
int i, j;
int n = 0;
printf ("How many rows do you want? ");
if (scanf("%d", & rows) != 1 || rows < 1) return EXIT_FAILURE;
printf ("\n");
for (i = 1; i <= rows; ++ i) {
for (j = 0; j < i; ++ j) {
printf ("%4d", n + (i % 2 == 0 ? i - j : j + 1));
}
printf ("\n");
n = n + i;
}
return EXIT_SUCCESS;
}
It can be more convenient to create another function that will calculate the biggest number of a row (I called it lineMax).
int lineMax(int num){
int cnt=0;
for (int i=1;i<=num;i++)
cnt+=i;
return cnt;
}
void main(){
int i,j,limit;
printf("Input limit : ");
scanf("%d", &limit);
for(i=1;i<=limit;i++){
if(i%2==0){ //right to left
for(j=lineMax(i);j>=lineMax(i-1)+1;j--)
printf("%d ",j);
}
else{ //left to right
for(j=lineMax(i-1)+1;j<=lineMax(i);j++)
printf("%d ",j);
}
printf("\n");
}
}
You are making a lot of special cases with number1, number2 and special. This will not work for bigger numbers.
One way is to calculate count which will give you the value to start from in each loop of j. count += i and then every time print count -j
count = 0;
for (i=1;i<=limit;i++)
{
count += i;
for(j=0;j< i;j++)
{
printf ("%d ",count-j);
}
printf("\n");
}

Printing patterns using with specific numbers in C

I'm trying to create a C program that prints a triangular pattern according to height by only using 1, 2 and 3 to generate the patterns:
Enter the height:
7
Pattern:
1
22
333
1111
22222
333333
1111111
I am only able to print the numbers but I do not know how to print only using 1, 2 and 3
This is my code so far:
printf("Enter the height: \n");
scanf("%d", &height);
if(height <0 || height > 10){
printf("Please enter height within 1 to 10!");
}
else{
printf("Pattern: \n");
for(row=1; row<=height; row++){
for(int col=1; col<=row; col++){
printf("%d", row );
}
printf("\n");
}
return 0;
}
The output:
Enter the height:
7
Pattern:
1
22
333
4444
55555
666666
7777777
Thank you
Just change your print statement like,
printf("%d", (row % 3) > 0 ? row % 3 : 3);
The change would be:
for(row=1; row<=height; row++){
int num = row%3;
if(num==0)
num = 3;
for(int col=1; col<=row; col++){
printf("%d", num );
}
printf("\n");
}
Logic:
1. Divide the value of row by 3 and get the remainder (i.e., perform row % 3).
2. If the remainder is 0, it means that the row number is a multiple of 3. Therefore, print 3s.
3. Otherwise, print the remainder.
I am only able to print the numbers but I do not know how to print only using 1, 2 and 3
This is because of you have been printing the row number in your code. There are many ways to achieve the same thing.
To solve your problem, you can have a variable (number in my program below) and keep on incrementing it in the outer for loop and once it becomes more than 3, then reset it to 1.
void print_pattern(unsigned height)
{
unsigned row,number,column;
for (row=0, number=1; row < height; row++) {
for (column=0; column <= row; column++)
printf("%u", number);
printf("\r\n");
if (++number > 3)
number = 1;
}
}
Or You can use the operator modulus %. You can apply it on row variable (see the program below) to get the the number to be printed at a particular row.
void print_pattern(unsigned height)
{
unsigned row,column;
for (row=0; row < height; row++) {
for (column=0; column <= row; column++)
printf("%u", (row % 3) + 1);
printf("\r\n");
}
}
use the mod operation % ..
(number%4) is between 0 and 3 ..
since you want it from 1 to 3 then take (number%3+1) ..
printf("Enter the height: \n");
scanf("%d", &height);
if(height <0 || height > 10){
printf("Please enter height within 1 to 10!");
}
else{
printf("Pattern: \n");
for(row=1; row<=height; row++){
for(int col=1; col<=row; col++){
printf("%d", (row%3)+1 );
}
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<conio.h>
int main(){
int n; int l=1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int j=i;
while(j!=0){
printf("%d",l);
j--;
}
l++;
if(l==4){
l=1;
}
printf("\n");
}
}

How to modify the input appearance on command window

I feel quite 'stupid' as asking this question but if anyone can show me the methods to modify the input result appeared on the command window.
Example:
I want to sort 5 numbers (1, 3, 4, 7, 5) in smallest-to-biggest order and the result on the command window must be:
input: 1 3 4 7 5 /* 1 line input */
output: 1 3 4 5 7 /* 1 line output */
Edit:
Here is my code
for (i = 0; i < 5; i++)
{
scanf("%d ", &array[i]);
}
If I use this code the result on command window must be:
1
3
4
7
5
But I want all the input number in only 1 line as:
1 3 4 7 5
So what do I have to do with my code?
Regarding to your edited question, just replace "%d " with "%d".
#include <stdio.h>
#define N 5
int main(void){
int i, j, array[N];
printf("Please enter the %d numbers.\n", N);
printf("input : ");
for(i=0;i<N;++i){
scanf("%d", &array[i]);
if(i!=0){
for(j=i;j>0 && array[j-1] > array[j];--j){
//swap array[j] and array[j-1]
int tmp = array[j];
array[j] = array[j-1];
array[j-1] = tmp;
}
}
}
printf("output : ");
for(i=0;i<N;++i){
if(i!=0)
putchar(' ');
printf("%d", array[i]);
}
putchar('\n');
return 0;
}

Resources