Search code examples
cloopsnestedsegmentation-faultnested-loops

Number Pattern under conditions


I am trying to code a program, such that, it asks the user a number, (for simplicity, 1-9).

Say user says 3, then this number acts as the number of rows of the Triangle pattern.

e.g.

Input - 3

_ _ _
_ _
_

Input - 5

_ _ _ _ _ 
_ _ _ _  
_ _ _ 
_ _  
_ 

Where _ are blank spaces to be filled.

A number is then generated comprising of the number of digits as provided by the user. The digits should be odd values only.

If input = 3
Number =  573, 397, 195 and all possible permutations.

The program should then subtract 1 from every single digit and print them as a triangle.

For example

Input = 5
(Therefore Rows = 5, number of digit = 5)
Number = 33795

Expected Output -

2 2 6 8 4
2 6 8 4
6 8 4
8 4
4

My language of preference is Python, but as am learning C, suggestions in any language are appreciated.

My Attempt -

1st Approach

#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int randomOdd(int base, int limit)
{
  int odd = 0;
  while (!(((odd = rand() % limit - base + 1) + base) % 2));
  return odd;
}


int main(void)
{
    int rows;
    int j;
    int A[100];
    scanf ("print %d",&rows);
    for(int i = 0; i < 5; i++)
    {
        A[i] = randomOdd(0, 10);
    }
    for (int i = 5; i > 0; i --)
    {
        for (j = rows - i; j < 5; j ++)
            printf("%d ", A[j]);
        printf("\n");
    }
return 0;
}

This Results in a Segmentation Fault.

2nd Approach This doesn't result in any output for some reason.

#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int randomOdd(int base, int limit)
{
  int odd = 0;
  while (!(((odd = rand() % limit - base + 1) + base) % 2));
  return odd;
}


int main(void)
{
    int rows;
    scanf ("print %d",&rows);
    
    int A[1000];
    
    for(int i = 0; i <= rows; i++)
    {
        A[i] = randomOdd(0, 10);
    }
    for (int m = rows; m > 0; m--)
    {
         for(int j=m-1; j>=0; --j) {
            printf("%d ", 9-A[j-1]);
        }
        printf("\n");
    }
return 0;
}

Solution

  • I think this could help you.

    from random import randint
    
    inp = int(input())
    numbers = [(randint(1, 9)-1) for i in range(inp)] # generate the random numbers
    
    for i in  range(inp):
        for j in numbers:
            print(j, end="")
        numbers.pop(0)  # remove the first element from the list 
        print("")
    

    Edit:
    I modified your code a bit and now works.

    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 100
    
    int randomOdd(int lower, int upper)
    { 
        int number = 0;
        while(number % 2 != 1){  // generate random numbers while the number is odd
            number = ((rand() % (upper - lower)) + lower) - 1;
        }
        return number;
    }
    
    int main()
    {   
        int rows, i, j;
        int a[MAX];
        scanf("%d", &rows);
        srand(time(NULL));  // you need srand to be able to generate random numbers
    
        for(i = 0; i < rows; i++)
        {
            a[i] = randomOdd(1, 10);
        }
        for(i = 0; i<rows; i++)
        {
            for(j=i; j<rows; j++)
            {
                
                printf("%d ", a[j]);
            }
            printf("\n");
        }
        return 0;
    }