Home > Enterprise >  Complete this part to get the result
Complete this part to get the result

Time:11-15

Hey I need to get to this result at the end:

enter image description here

As you can see when you run the code it won't print the full word like "aaa", it would print each letter on its own.

And I need to complete the code where it's (1) (2) (3)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define n 3

void what(char* string);

int main()
{
    char s[4] = { 'a', 'a', 'a', 0 };
    int i, j, t;
    t = n * n * n;

    while (t--)
    {
        for (i = 0; i < n - 1; i  )
            printf("\n", s[i]);

        printf("\n", s[i]);

        if (  s[n - 1] > 'c')
        {
            (1); //In my opinion it's s[???]  ; not sure if that's the right solution I also don't know what to put inside the [].

            for  (j = (2) ; j >= 0; j--) //In my opinion it's - j=2
            {
                if (3) // In my opinion it's - if (s[j] == 'd')
                    s[j] = 'a';
                else break;
            }


        }
    }

}

CodePudding user response:

In the snippet below you find just the while containing the missing code.

while (t--)
{
    for (i = 0; i < n - 1; i  )
        printf("", s[i]);

    printf("\n", s[i]);

    if (  s[n - 1] > 'c')
    {
        s[n-1] = 'a'; /* (1) */

        for  (j = n-2; /* (2) */ j >= 0; j--) //(2)
        {
            if (  s[j] > 'c') /* (3) */
                s[j] = 'a';
            else break;
        }
    }
}

First of all let me say that I changed also the print section, removing that newline that caused every letter to be printed in a different line:

printf("", s[i]); //Was printf("\n", s[i]);

Only after the third letter a newline character will be printed.

Let's now describe the code added in every missing "spot":

  1. The last character, incremented after every iteration, needs to be reset to 'a'
  2. Now that the last character is ok,you need to apply the same policy to all the remaining characters (in fact, though the code is not ready for that, this code is meant to work also for n != 3).
    So you must iterate from n-2 to 0.
  3. You have to apply the usual rule to reset jth character, so just check if ( s[j] > 'c')

CodePudding user response:

I don't understand how you are trying to solve the problem, but you can easily solve the problem with 3 nested loops:

#include <stdio.h>

int main( void )
{
    for ( int i = 0; i < 3; i   )
    {
        for ( int j = 0; j < 3; j   )
        {
            for ( int k = 0; k < 3; k   )
            {
                printf( "%c%c%c\n", 'a' i, 'a' j, 'a' k );
            }
        }
    }
}

Note that this solution is only guaranteed to work on character sets in which the characters 'a' to 'c' are stored contiguously, which is the case with all ASCII-compatible character sets. But ISO C does not require that the character set stores these letters contiguously.

If you want the solution to be guaranteed to work on all ISO C compliant character sets, then you could use the following code:

#include <stdio.h>

int main( void )
{
    const char chars[] = { 'a', 'b', 'c' };

    for ( int i = 0; i < 3; i   )
    {
        for ( int j = 0; j < 3; j   )
        {
            for ( int k = 0; k < 3; k   )
            {
                printf( "%c%c%c\n", chars[i], chars[j], chars[k] );
            }
        }
    }
}
  • Related