Write a c program to read 10 values Store them in an array a

Write a c program to read 10 values. Store them in an array according to the following criteria If the value is positive store it at the even index If the value is negative store it at the odd index. The output may look like this:

Solution

#include <stdio.h>

int main()
{
    //declare array to hold nos
    int nos[10];
    //declare variables
    int tmp,oddIndex=-1,evenIndex=-2;
    //prompt user to enter values
    printf(\"Enter 10 values: \");
    for(int i=0;i<10;i++)
    {
        //read value
        scanf(\"%d\",&tmp);
        //check if its even
        if(tmp>0)
        {
            //if inputted number is even,increase index and if it\'s less than 10,set value
            evenIndex+=2;
            if(evenIndex<9)
            {
                nos[evenIndex]=tmp;
            }
        }
        //check if inputted no is odd
        else
        {
            //if odd no is provided,increase index and if index is less than 10,assign value to array to odd position
            oddIndex+=2;
             if(oddIndex<10)
            {
                nos[oddIndex]=tmp;
            }
        }
    
    }
    //loop through array and display array elements
      for(int i=0;i<10;i++)
        {
            printf(\"%d\\t\",nos[i]);
        }

    return 0;
}

Sample output:

Enter 10 values: 10 90 -2 4 -8 27 30 390 -30 5                                                                                                                  

10      -2      90      -8      4       -30     27      0       30      0

 Write a c program to read 10 values. Store them in an array according to the following criteria If the value is positive store it at the even index If the valu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site