Simple Pyramid In C - c

the code I've written creates a pyramid with given input. I need the first part Incrementing by 2, but the code written does not really Incrementing by 2. Aprrechiate any help.
Code:
int main() {
int i, j, n, a, rows;
printf("Enter a Number: ");
scanf("%d", &rows);
printf("\nPattern for %d:\n\n", rows);
for (i = 0; i <= rows; i++) {
printf("%d | ", i);
for(a = 2; a <= i; a+=2) {
printf("%d ", a);
}
for (j = 0; j <= i; j++) {
if (j == i){
printf("%d ", j);
}
}
for(n = 1; n < i; n++){
printf("%d ", j);
j++;
}
printf("\n");
}
return 0;
}
Needed Output:
0 | 0
1 | 0 1
2 | 0 2 2 3
3 | 0 2 4 3 4 5
4 | 0 2 4 6 4 5 6 7
5 | 0 2 4 6 8 5 6 7 8 9
6 | 0 2 4 6 8 10 6 7 8 9 10 11
Current Output:
0 | 0
1 | 1
2 | 2 2 3
3 | 2 3 4 5
4 | 2 4 4 5 6 7
5 | 2 4 5 6 7 8 9
6 | 2 4 6 6 7 8 9 10 11

I had to add an extra if condition for i = 0 because it was not fitting in the pattern I figured out. I have done it in just 2 loops instead of you using 3 loops.
Here is the brief explanation. Comment on this answer if you don't understand the code still, I will add a detailed explanation.
Case i = 0 is a special case and it is handled separately using an if.
for i > 0, you can notice that there are 2 series in parallel
even numbers always starting from 0 and if you count them then they are equal to value of i in that row.
another series whose first number is the value of i in that row and it also has number of elements equal to value of i in that row.
That's why in both the loops that handle the series they are run from 0 till i-1.
I hope it helps you and please accept as correct answer if it did.
#include <stdio.h>
int main() {
int i, j, n, a, b, c, rows;
printf("Enter a Number: ");
scanf("%d", &rows);
printf("\nPattern for %d:\n\n", rows);
for (i = 0; i <= rows; i++) {
if(i == 0){
printf("0 | 0\n");
continue;
}
printf("%d | ", i);
// For handling the even number series
for(a = 0, b = 0; a < i; a++, b+=2) {
printf("%d ", b);
}
// For handling the incremental series
for (j = 0, c = i; j < i; j++, c++) {
printf("%d ", c);
}
printf("\n");
}
return 0;
}

You try to overcomplicate simple task.
void pyramid(int rows)
{
for(int row = 0; row < rows; row++)
{
for(int even = 0; even < row * 2; even += 2)
{
printf("%d ", even);
}
if(row == 0) printf("%d ", row);
else
{
for(int remain = row; remain < row * 2; remain++)
{
printf("%d ", remain);
}
}
printf("\n");
}
}
https://godbolt.org/z/cdWqsq

Related

How could I align my output to the center?

#include<stdio.h>
int main()
{
int n ;
printf("Input the number of rows: ");
scanf("%d", &n);
for(int i = 1; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
if(j == i + 1)
{
break;
}
printf("%3d", j);
}
for(int j = i - 1 ; j > 0; j--)
{
if(j == 0)
{
break;
}
printf("%3d", j);
}
printf("\n");
}
}
Program session
Number of rows for this output n = 3
My output:
1
1 2 1
1 2 3 2 1
Preferred output:
1
1 2 1
1 2 3 2 1
This here is an exercise where I have to print a pyramid of numbers where the central number of the pyramid is the number of the row. I understand the logic but as you can see I have not been able to fulfill the task successfully. Any tips?
As pointed out by #WeatherWane, you need to add logic to add extra spaces. If you notice carefully, number of spaces on each line(excluding padding you add with %3d is equal to 3 * (n - i)). You can create a simple method like this to add spacing:
void addSpaces(int N, int currentIndex, int padding) {
for (int index = currentIndex; index < N; index++)
for (int spaces = 0; spaces < padding; spaces++)
printf(" ");
}
then you can call it from your first for loop like this:
for(int i = 1; i <= n ; i++)
{
addSpaces(n, i, 3);
for(int j = 1 ; j <= i ; j++)
{
// ...
I tested it and it seems to align it correctly:
c-posts : $ ./a.out
Input the number of rows: 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

I've got no idea how to write C-code about pyramid [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I made this by an helper and I tried to unify the location of the sum of each row, but I don't know how to fix it. I think the part of printf("%2d", j) from inner for should be changed, but I have no idea for this.
for (i = 1; i <= s; i++)
{
sumOfRow = 0;
for (j = i; j <= s; j++)
{
printf("%2d", j);
sumOfRow += j;
}
printf("\t\t%-5d\n", sumOfRow);
sum += sumOfRow;
}
printf("Sum of all numbers is %d\n", sum);
getchar();
return 0;
}
#include <stdio.h>
int main()
{
int s, i, j, sum = 0;
// create new variable to store sum of single row
int row_sum = 0;
printf("Enter the size:");
scanf("%d", &s);
for (i = 1; i <= s; i++)
{
row_sum = 0; // reset sum for each row
for (j = i; j <= s; j++)
{
printf("%2d ", j);
row_sum += j; // add current number to row_sum
}
printf("\t%d\n", row_sum);
sum += row_sum; // add current row_sum to total sum
}
printf("%d\n", sum);
getchar();
return 0;
}
EDIT
To add padding, you can use the following trick -
printf("%*s", paddingLength, "");
This will print an empty string that will take "paddingLength" characters.
For each value in a row, we have 3 chars, 2 for the value and one for space. So, add a padding of 3*i before printing row_sum.
#include <stdio.h>
int main()
{
int s, i, j, sum = 0;
int row_sum = 0;
printf("Enter the size:");
scanf("%d", &s);
for (i = 1; i <= s; i++)
{
row_sum = 0;
for (j = i; j <= s; j++)
{
printf("%2d ", j); // this will have fixed length of 3
row_sum += j;
}
printf("\%*s%d\n", 3*i , "", row_sum); // print 3*i whitespace before row_sum
sum += row_sum;
}
printf("%*s%d\n", 3*i, "", sum);
getchar();
return 0;
}
Output -
Enter the size:6
1 2 3 4 5 6 21
2 3 4 5 6 20
3 4 5 6 18
4 5 6 15
5 6 11
6 6
91
Try this
#include <stdio.h>
int main()
{
int s, i, j, ttl=0;
printf("Enter the size:");
scanf("%d", &s);
int sum[s];
for (i = 0; i < s; i++)
{
sum[i]=0;
for (j = i; j < s; j++)
{
printf("%2d", j);
sum[i] += j;
}
printf("\n");
}
for(int k=0; k< s;k++){
printf(" sum of %d is: %d ", k, sum[k]);
ttl += sum[k];
}
printf(" total sum is: %d" , ttl);
getchar();
return 0;
}
A straightforward approach can look the following way.
#include <stdio.h>
int main(void)
{
const unsigned int N = 10;
while ( 1 )
{
printf( "Enter the size: " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < N ) ) n = N - 1;
putchar( '\n' );
const int WIDTH = 25;
unsigned int sum = 0;
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int partial_sum = 0;
for ( unsigned int j = 0; j < n - i; j++ )
{
unsigned int value = i + j + 1;
printf( "%u ", value );
partial_sum += value;
}
printf( "%*u\n", WIDTH - 2 * n + 2 * i, partial_sum );
sum += partial_sum;
}
printf( "Total of the numbers = %u\n\n", sum );
}
return 0;
}
The program output might look like
Enter the size: 6
1 2 3 4 5 6 21
2 3 4 5 6 20
3 4 5 6 18
4 5 6 15
5 6 11
6 6
Total of the numbers = 91
Enter the size: 7
1 2 3 4 5 6 7 28
2 3 4 5 6 7 27
3 4 5 6 7 25
4 5 6 7 22
5 6 7 18
6 7 13
7 7
Total of the numbers = 140
Enter the size: 8
1 2 3 4 5 6 7 8 36
2 3 4 5 6 7 8 35
3 4 5 6 7 8 33
4 5 6 7 8 30
5 6 7 8 26
6 7 8 21
7 8 15
8 8
Total of the numbers = 204
Enter the size: 9
1 2 3 4 5 6 7 8 9 45
2 3 4 5 6 7 8 9 44
3 4 5 6 7 8 9 42
4 5 6 7 8 9 39
5 6 7 8 9 35
6 7 8 9 30
7 8 9 24
8 9 17
9 9
Total of the numbers = 285
Enter the size: 0

Pattern Printing in C for printing numbers in vertical pattern

I wrote a function to print the below pattern.
For example, if the n value is 4 the pattern is
1
2 7
3 6 8
4 5 9 10
Or if the value of n is 5, then the pattern is
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
My function gives me the first two block but not the next block. I'm stuck here for long time!
My function is
int printPattern(int n) {
int row, column, fwdCtr = 1, evenCtr = 0, ctr = n;
for(row = 1; row <= n; row++) {
fwdCtr = row;
for(column = 1; column <= row; column++) {
if(column % 2 != 0) {
printf("%d ", fwdCtr++);
} else {
evenCtr = fwdCtr + ctr;
printf("%d ", evenCtr);
ctr = ctr - 2;
}
}
printf("\n");
}
}
What I get is
1
2 7
3 6 4
4 5 5 4
Please give suggestions of changes!
The following code should do it:
#include <stdio.h>
void f(int n)
{
for (int i = 0; i < n; ++i)
{
for (int j=0; j<=i; ++j)
{
// Calculate the numbers used so far by previous columns
int x = 0;
for(int v=0; v<j;++v)
{
x = x + (n-v);
}
if ((j % 2) == 0)
{
// even columns
printf("%d ", x+i-j+1);
}
else
{
// odd columns
printf("%d ", x+n-i);
}
}
printf("\n");
}
}
int main(void)
{
f(5);
return 0;
}
Output:
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
The easy thing to do is just print the right number based on the row and column and the value of n, like this
int main(void)
{
int n = 20;
for (int row = 0; row < n; row++) {
for (int col = 0; col <= row; col++)
printf("%3d ", 1 + col*n - (col-1)*col/2 + (col%2 ? n-1-row : row-col));
printf("\n");
}
}

zoom two dimentional array C

Solved here is the code that worked for me
for (i = 0; i < nbLine; i++)
for (k = 0; k < zoom; k++)
{
for (j = 0; j < nbColumn; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColumn) + j));
}
printf("\n");
}
I have a function that output a two dimensional array ([column][line]) and it need to be zoom, in fact it is like going from
1 2
3 4
to a
1 1 2 2
3 3 4 4 array when zoom is equal to 2
here is some code :
static void output(int* array, int nbColumn, int nbLine, int zoom)
{
int i, j, k, l;
for (i = 0; i < nbColumn; i++)
for (k = 0; k < zoom; k++)
{
for (j = 0; j < nbLine; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColomn) + j));
}
printf("\n");
}
}
when I try this code on a squared array, it works fine, but when using a rectangular one, it fails. I have tried to debug it by replacing the printf("%d ", *(array + (i*nbColumn) + j)); by printf("%d ", (i*nbColumn) + j); and it gives me this result for a 8 colomns by 5 rows array :
0 1 2 3 4 5 6 7
5 6 7 8 9 10 11 12
10 11 12 13 14 15 16 17
15 16 17 18 19 20 21 22
20 21 22 23 24 25 26 27
Thanks for help
A working program source is:
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("in.in");
ofstream out("out.out");
int main(void){
int m[100][100],i,j,l,noLines,noColumns,zoom;
in>>noLines>>noColumns>>zoom;
for(i=0;i<noLines;i++){
for(j=0;j<noColumns;j++){
in>>m[i][j];
}
}
for (i = 0; i < noLines; i++){
for (j = 0; j < noColumns; j++){
for (l = 0; l < zoom; l++) {
out<<m[i][j]<<' ';
}
}
out<<'\n';
}
return 0;
}
On the input:
4 3 2
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
you get the output:
1 1 2 2 3 3
4 4 5 5 6 6
7 7 8 8 1 1
2 2 3 3 4 4
Changing the parameters on the first line of the input (i.e. the bidimensional array width, height and the zoom), changes the output.
From your example I can see that you are zooming only horizontally, since the elements are duplicated horizontally, but the number of rows is left intact. So you do not need vertical zoom and therefore your cycle with k is unuseful. This should work:
static void output(int* array, int nbColumn, int nbLine, int zoom)
{
int i, j, l;
for (i = 0; i < nbLine; i++)
{
for (j = 0; j < nbColumn; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColomn) + j));
}
printf("\n");
}
}

What is wrong in the following code for CodeChef May Long Challenge, Chef and Strange Matrix?

Code
#include <stdio.h>
int main()
{
long long int n, m, p, i, j, total_cost, cost;
scanf("%lld %lld %lld", &n, &m, &p);
long long int ar[n][m];
for(i = 1; i <= n; ++i)
{
for(j = 1; j <= m; ++j)
{
ar[i][j] = j;
}
}
while(p--)
{
scanf("%lld %lld", &i, &j);
ar[i][j] += 1;
}
/*
printf("\n");
for(i = 1; i <= n; ++i)
{
for(j = 1; j <= m; ++j)
{
printf("%d ", ar[i][j]);
}
printf("\n");
}
printf("\n");
*/
if(n == 1 && m == 1)
{
printf("0\n");
return 0;
}
if(m == 1)
{
for(i = 1; i <= n; ++i)
{
printf("0\n");
}
return 0;
}
for(i = 1; i <= n; ++i)
{
total_cost = 0, cost = 0;
for(j = m; j >= 2; --j)
{
cost = ar[i][j] - ar[i][j - 1];
if( cost < 0 )
{
printf("-1\n");
break;
}
total_cost += cost;
}
if(cost >= 0)
{
printf("%lld\n", total_cost);
}
}
return 0;
}
This is giving me WA. Here is the problem description CHEFBM.
Test cases I have checked:
1. 1 5 3
1 2
1 4
1 5
Output
5
2. 4 4 6
2 2
3 2
3 2
4 3
4 4
4 3
Output
3
3
-1
4
3. 1 4 5
1 3
1 2
1 2
1 1
1 1
Output
1
4. 4 4 6
3 2
2 3
3 2
2 4
1 2
3 2
Output
3
4
3
3
5. 4 1 1
1 1
Output
0
0
0
0
6. 1 4 3
1 1
1 1
1 4
Output
-1
7. 1 1 1
1 1
Output
0
8. 1 1 2
1 1
1 1
Output
0
9. 2 2 3
1 1
1 1
1 1
Output
-1
1
And many more. For which test case is this giving wrong answer?
As given in the constraints, n and m both can be 10^5. Allocating a 2-d array a[10^5][10^5], is not feasible here. You are running out of memory and hence you might be getting Run time error or Wrong answer. Your algorithm is O(n^2) so it will not pass in the given time limit. Try to optimize your code.
For further assistance, you can check my code.

Resources