Thread: Returning pattern

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    21

    Returning pattern

    Hi
    I am writing programm to print pattern like below
    1
    22
    333
    4444
    55555
    666666
    up to n given as a parametr.
    my code below
    Code:
    char* pattern(const int n)
    {
    	char *result = (char*)malloc(sizeof(char)*n*n*n*n);
    	char *temp = (char*)malloc(sizeof(char) * 4);
    	memset(result, '\0', (n*n*n*n));
    	if (n<1)
    		return "";
    	for (int i = 1; i <= n; i++)
    	{
    		for (int j = 1; j <= i; j++)
    		{
    			sprintf(temp, "%d", i);
    			strcat(result, temp);
    		}
    		strcat(result, "\n");
    	}
    	result[strlen(result) - 1] = '\0';
    	return result;
    }
    I pass all basic test but on random test i got some errors. Can you tell me where is mistake

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    How big can n be?

    Your code leaks memory all over the place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    21
    Quote Originally Posted by Salem View Post
    How big can n be?
    maximum n size is 2^16 which give us 65 536 so n should be at least 5 characters long

    Quote Originally Posted by Salem View Post
    Your code leaks memory all over the place.
    I thought that I don't have to calculate perfect value of characters for each n thats why I allocated bigger memory than I need

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    697
    Quote Originally Posted by gawiellus View Post
    maximum n size is 2^16 which give us 65 536 so n should be at least 5 characters long
    Then, it is not possible with your code... result will have to point to a buffer which requires 2^64 bytes ( 1 EiB ).

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    21
    This is task from codewars site so probably n is correct value.
    So the way is to calculate perfect bytes of allocated memory? For example
    Code:
    if(n<9)
     size=n*(n+1)/2;
    if(n>9&&n<=99)
    size=2*(n*(n+1)/2)+(n+1)/2
    if(n>99&&n<=999)
    size=3*(n*(n+1/2)+2*(n*(n+1)/2)+(n+1)/2

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    950
    temp is the one leaking memory since you don't free it before the function ends.
    However, it's pointless for it to be dynamic anyway.

    For n = 65536, result must be 10,687,148,122 bytes.

    If you just have to print it, why are you making a string?
    Why not just
    Code:
    for (int i = 1; i <=n; ++i) {
        for (int j = 0; j < n; ++j)
            printf("%d", i);
        putchar('\n');
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  7. #7
    Registered User
    Join Date
    Jun 2019
    Posts
    21
    Sorry I probably said wrong my task is not print but return as a string. If memory is limited than probably n must be lower than 65536. As I said this is task from codewars site then n must be correct value.

  8. #8
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    269

    Post

    Code:
    #include <stdio.h>
    
    int printPattern(int n) {
        for (int i = 0; i < n; i++) {
            printf("%i",n);
        }
    }
    
    void numberTree(int n) {
        for (int i = 1; i < (n+1); i++) {
            printPattern(i);
            printf("\n");
        }
    }
    
    int main() {
        numberTree( 9 );
        return 0;
    }
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an integer ... not returning; weird error
    By Imanuel in forum C++ Programming
    Replies: 19
    Last Post: 09-25-2011, 01:30 PM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. bit pattern
    By hell0 in forum C Programming
    Replies: 3
    Last Post: 07-25-2011, 08:38 AM
  4. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  5. pattern
    By Unregistered in forum C Programming
    Replies: 16
    Last Post: 05-04-2002, 07:37 PM

Tags for this Thread