-3

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?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Edit the post like [this](https://stackoverflow.com/questions/65483595/how-to-count-words-stored-in-2-d-array-after-adding-another-word/65484123#65484123) – IrAM Dec 28 '20 at 21:18
  • Do you want to replace value into an array with another value ? – MED LDN Dec 28 '20 at 21:22
  • 1
    The `for` loop is a _statement_. You can only have _expression_s in the parts of the conditional operator. Rewriting big if statements like this does not help with readability. Stick with the if statement. – 1201ProgramAlarm Dec 28 '20 at 21:24
  • yes i want to replace value into an array with another value. But i want to replace it with the value I entered from the keyboard – Büşra Sarıkaya Dec 28 '20 at 21:28
  • 1
    Please understand that spaces matter: `\ n` is not the same as `\n`, and `> =` is not the same as `>=`. – Steve Friedl Dec 28 '20 at 21:43
  • Consult the language grammar. All three operands of a conditional operator must be *expressions*. They may not be arbitrary *statements* such as `for`, `while`, etc. – Tom Karzes Dec 28 '20 at 22:13
  • `?:` is not meant to be a control structure - it is not a replacement for the `if` statement. – John Bode Dec 29 '20 at 01:04

1 Answers1

0

don't use white space here scanf ("% d", & n);try to concatenate % with d and & with n

#include <stdio.h>
int main ()
{
int n, i, index, a[10], value;
printf ("n:");
do
{
    scanf ("%d",&n);
}while(n<1||n>10);

printf ("Initial \ n");
for (i=0;i<n;i++)
{
    a[i]=i;
    printf ("\n%d. value:%d \n", i, a [i]);
}
printf ("Enter the index value you want to change:");
scanf ("%d",&index);
if ((index>=0)&&(index<n))// write like this : >= 
    {
         printf("What number will you replace with array index% d?:",a[index]);
         scanf("%d",&value);
         a[index]=value;
    }
    for (i=0;i<n;i++)
    {
        printf ("[%d]", a [i]);
    }

return 0;

}
MED LDN
  • 684
  • 1
  • 5
  • 10