lundi 28 décembre 2020

what is wrong if convert to conditional operator

I want to convert an "if" to "conditional operator (:?)". I tried to write it but I get an error expected expression before "for".

The original code:

#include <stdio.h>
int main ()
{
    int n, i, k, a [10], new;
    printf ("n:");
    scanf ("% d", & n);
    printf ("Initial \ n");
    for (i = 0; i <n; i ++)
    {
        a [i] = i;
        printf ("% d. value:% d \ n", i, a [i]);
    }
    printf ("Enter the index value you want to change:");
    scanf ("% d", & k);
    if ((k> = 0) && (k <n))
    {
        for (i = 1; i <= n; i ++)
        {
            if (i == k)
            {
                printf ("What number will you replace with array index% d?:", a [i]);
                scanf ("% d", & new);
                a [i] = new;
            } // if (i == k) end
        } // end of for loop

                        printf ("Last state \ n");
            for (i = 0; i <n; i ++)
            {
                printf ("% d. value:% d \ n", i, a [i]);
            }
    } // end of if statement
    else
        printf ("You did not enter a value within the array boundaries. \ n");
    return 0;
}

I want to convert it to conditional form.

I tried this:

#include <stdio.h>
int main ()
{
    int n, i, k, a [10], new;
    printf ("n:");
    scanf ("% d", & n);
    printf ("Initial \ n");
    for (i = 0; i <n; i ++)
    {
        a [i] = i;
        printf ("% d. value:% d \ n", i, a [i]);
    }
    printf ("Enter the index value you want to change:");
    scanf ("% d", & k);
    if ((k> = 0) && (k <n))

        for (i = 1; i <= n; i ++)

        {
            (i == k)?printf("What number will you replace with array index% d?:", a [i]), scanf("% d", & new),(a [i] = new): for(i = 0; i < n; i ++), printf("Last state \ n"), printf("% d. value:% d \ n", i, a [i]), printf("You did not enter a value within the boundaries of the array. \ n" );
        }
    return 0;

}

I get an error: expected expression before "for". How can I write this?

Aucun commentaire:

Enregistrer un commentaire