I was doing some hackerrank practice and I couldnt solve it. I am newbie any ideas to solve this problem?
Please write a software program that prints the following pyramid pattern to the screen. Your program should get a number ānā as an input and should print n lines of the pyramid. (For example if your input is 4, then the following output is expected.)
(You can choose any programming language that you want.)
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
#include<stdio.h>
int main()
{
int i,j,k,l=1;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
printf(" ");
}
for(k=1; k<=l; k++)
{
printf("%d",k);
}
l = l+2;
printf("\n");
}
return 0;
}
These types of problems can be solved by simple mathematics.
I would advice you to solve this by problem by breaking it into simpler and smaller use cases as follows:
Number of blank spaces on each line.
Number of Elements (on each line) until the centre of pyramid.
Number of Elements (on each line) after the centre of pyramid.
#include<stdio.h>
int main()
{
int N;
scanf("%d", &N);
int numberOfBlankSpaces = N + 2;
for(int i=1; i<=N ;i++){
for(int j=0;j<numberOfBlankSpaces;j++){
printf(" ");
}
numberOfBlankSpaces -= 2;
int number_of_elements_until_centre_on_each_line = i;
int startingElement = i;
//Print increasing order until center
while(number_of_elements_until_centre_on_each_line--){
printf("%d ", startingElement);
startingElement++;
}
// Starting element = Element at center - 1
startingElement-=2;
//Print decresing order
while(startingElement >= i){
printf("%d ", startingElement);
startingElement--;
}
printf("\n");
}
return 0;
}
Input: 4
Output:
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
Related
I currently am having a small trouble in my code, I am supposed to make a program that adds / sums all numbers inside of an array, while I have no problem in doing that, I currently have a problem with the part in which you are supposed to scan the numbers to be put in the array, here are the example of the input
3
5
1 2 3 4 5
8
1 2 3 4 5 6 7 8
9
1 2 3 4 5 6 7 8 9
What this means is that, the user inputs number "3" as it means to create 3 arrays, the number "5" afterward means to put 5 numbers inside of the array (1 2 3 4 5), after the user has inputted the numbers inside of an array, the user inputs "8" which means to make another array consisting of 8 numbers, and then putting numbers into the array again, and so on.
However I am having a problem in which after inputting all the numbers in the array that consists of 5 number, the program instead inputs 5 number into another array again (instead of asking the amount of numbers to be put inside of another array), so instead the number "8 1 2 3 4" gets inputted in another array, and I did not know which part I did wrong.
Here are my C code :
#include <stdio.h>
int main(){
int x, y;
int i;
int n;
int c=1;
int count=0;
int sum=0;
scanf("%d", &y); //this determines the amount of array to be inputted
scanf("%d", &x); //this determines the amount of numbers to be inputted inside of an array
int line[x];
for(int i=0; i<y; i++){
sum=0;
for(int i=0; i<x; i++){
scanf("%d", &line[i]); //scan number for the array
sum += line[i];
}
printf("Case #%d: %d\n", c, sum);//output of all sum
c++;
}
}
You need to read the size for each array - currently you only read it once.
e.g.:
int numLines;
scanf("%d", &numLines);
for(int lineIdx = 0; lineIdx < numLines; lineIdx++) {
// read the number of elements for each array
int numNumbers;
scanf("%d", &numNumbers);
int line[numNumbers];
for(int i = 0; i < numNumbers; i++) {
scanf("%d", &line[i]);
}
}
Additionally you can avoid storing the individual numbers, since you're only interested in the sum, e.g.:
int sum = 0;
for(int i = 0; i < numNumbers; i++) {
int number;
scanf("%d", &number);
sum += number;
}
Also you could defer outputting the sums until all inputs have been processed, so it doesn't visually get interleaved into the input.
This would be a possible way to write that program: godbolt
#include <stdio.h>
int main() {
// get the number of arrays we need to read
int numLines;
scanf("%d", &numLines);
// The sums of each array
int sums[numLines];
for(int lineIdx = 0; lineIdx < numLines; lineIdx++) {
// read the number of elements for each array
int numNumbers;
scanf("%d", &numNumbers);
// sum up all the numbers of the array
sums[lineIdx] = 0;
for(int i = 0; i < numNumbers; i++) {
int number;
scanf("%d", &number);
sums[lineIdx] += number;
}
}
// after all arrays have been entered,
// output the sums for each case:
for(int lineIdx = 0; lineIdx < numLines; lineIdx++) {
printf("Case #%d: %d\n", lineIdx, sums[lineIdx]);
}
}
I'm trying to use a for loop to add a space to the beginning of my output for every line. For every line, it will add more space between as it goes through each line to get the flipped triangle.
My code
#include <stdio.h>
#include <math.h>
// compiler issue on 2nd digit
int main(){
long long number;
printf("Enter a number\n");
printf("Enter your number = ");
scanf("%lld", &number);
for(int j=1; j<= (log10(number)+1); j++){
for(int i=1; i<= (log10(number)+1) - (j-1); i++){
printf("%d ", (number%((int)pow(10, i)))/(int)pow(10, i-1));
}
printf("\n");
}
return 0;
}
My output:
Enter a number
Enter your number = 12345
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
What I'm trying to output:
Enter a number
Enter your number = 12345
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
A sample program as what I had in mind
int main()
{
int l;
for(l=0; l<3; l++){
printf("%*s Moving\n", l, "");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
int n, i, input_cases,x;
float num[100], sum[2] = {0.0}, average[2] = {0.0};
//scanf("%d", &input_cases);
//for (x=0; x<input_cases; x++){
printf("Enter the total amount of numbers: ");
if (scanf("%d", &n) != 1) { /* validate your input */
fprintf (stderr, "error: invalid input.\n");
return 1;
}
for (i = 0; i < n; i++) {
if (scanf("%f", &num[i]) != 1) { /* validate input */
fprintf (stderr, "error: invalid input.\n");
return 1;
}
}
/* sum/average 1st-half */
for (i = 0; i < n/2; ++i)
sum[0] += num[i];
average[0] = sum[0] * 2 / n;
/* sum/average 2nd-half */
for (i = n/2; i < n; ++i)
sum[1] += num[i];
average[1] = sum[1] * 2 / n;
if (average[0]>average[1]){
printf("%.6lf\n", average[0]);
}
else{
printf("%.6lf\n", average[1]);}
//}
return 0;
}
So what this code does is it reads in a list of numbers, splits up that list in half, finds the average of the two split up lists, and then tells you the highest average out of the two lists. For example:
Input:
Enter the total amount of numbers: 10
10
8
9
15
12
2
3
8
7
11
Output:
10.800000
What I'm trying to do is perform this operation multiple times. So say I want to do this with 2 different lists. My program should give me the 2 highest averages of the 2 different lists. I've added in a for loop to try and make that happen but somewhere in between the loop, my highest average for my second number list gets messed up and I can't seem to figure out where. I've commented out my for loop to show you guys that doing it without a loop works perfectly. It's only when I add the for loop in that my calculations become messed up. With the for loop, this is what shows:
Input:
2
Enter the total amount of numbers: 10
10
8
9
15
12
2
3
8
7
11
10.800000(This is the result)
Enter the total amount of numbers: 4
3
3
2
1
30.000000 (This should be 3.000000)
What I WANT it to show:
Input:
2
Enter the total amount of numbers: 10
10
8
9
15
12
2
3
8
7
11
Enter the total amount of numbers: 4
3
3
2
1
Output:
10.800000
3.000000
I simply moved my variable declarations right under my main for loop.
I need to generate the following output of odd numbers in pyramid pattern.
The output will be like
1
3 3
5 5 5
7 7 7 7
I have written the following code. What portion i should modify?
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=1;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
Current Output:
Current output is like-
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
You could write:
for(r=1; r <= num; r+=2) //we only need odd numbers
{
times = r/2 + 1; //how many times to print odd number
for(c=1; c <= times; c++)
printf("%d",r); //print one character at a time
printf("\n");
}
You'll probably understand it better if you only iterate through odd numbers. I'm currently doing that and calculating how many times I need to print that number, then I'm just printing it as many times as times is.
you have 2 errors in this code .
m is not declared anywhere .
you are running a infinite loop
try this .
#include<stdio.h>
int main()
{
int num,r,c,m;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=r;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
In your code, instead of
m=1;
you should write
m= ( (r/2) + 1);
Oterwise, all the time, you'll be iterating in the for loop only once.
Some little modifications and it works:
#include<stdio.h>
int main()
{
int num,r,c,m=0;
printf("Enter structure number : \n");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m++;
for(c=1; c<=m; c++)
printf("%d ",r);
printf("\n");
}
}
return 0;
}
m is undeclared
line feed at the end of the printf message
m incremented each odd iteration
space between printed unmbers
Here is a demo.
the inner for loop should look like this:
for(c=1; c <= r/2; c++)
printf("%d ",r);
just think about it for a second. you want to print a rounded r/2 of numbers in every line, right?
like:
3/2 -> 1.5 -rounded-> 1 -> prints: 3
5/2 -> 2.5 -rounded-> 2 -> prints: 5 5
and so on.
you can run the code here on ideone.com
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
int m=r;
for(c=1; c<=m; c++)
{
if(c%2 != 0){
printf("%d ",r);
}
}
printf("\n");
}
}
return 0;
}
and test
sh-4.3# main
Enter structure number : 9
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
In row 1, you should print 1.
In row 2, you should print 3.
In row 3, you should print 5.
In row 4, you should print 7.
............................
............................
In row n, you should print 2*n-1.
You can check this:
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
printf("%d",2*r-1);
printf("\n");
}
return 0;
}
Please take a look at this:
for(r=1; r<=num; r+=2) // increment by 2, work for r= 1,3,5,7...
{
for(c=1; c<=r; c+=2)// increment by 2
printf("%d",r);
printf("\n");
}
If you want alternate numbers like 1,3,5,7... just increment value by 2.
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;
}