whats wrong with this one? i am trying to display the values in an odd index can any one help?

    int main()
    {
        int numArrays[] = {"2 ,67 ,23 ,5 ,7 ,34 ,14 ,4 ,8 , 62"};
        int i;

        printf("A. Values of NumArray with Odd Index: \n");
        for(i=0;i<=10; i++)
        {
           i+=1;
           printf("%d ", NumArray[i]);     
         } 

         }

there is more than one compile error dont use " " when initializing an integer array so it should be

int numArrays[] = {2 ,67 ,23 ,5 ,7 ,34 ,14 ,4 ,8 , 62};

also while printing u used a captial N it should be numArray not Numarray

#include <stdio.h>

int main()
{
int numArrays[] = {2 ,67 ,23 ,5 ,7 ,34 ,14 ,4 ,8 , 62};
int i;
printf("A. Values of NumArray with Odd Index: \n");
for(i=0;i<10; i++)
{
i+=1;
printf("%d ", numArrays[i]);
}
}

This looks to me like C code and not C++ but to print only the values of the odd index numArrays[1], numArrays[3], numArrays[5] etc begin the for loop with i = 1 and then rather than using i++ which increments by one use i+=2 which increments i by two each time. There was also a problem with the input, you don't need the quotation marks.

#include <stdio.h>

int main()
{
    int numArrays[] = {2, 67, 23, 5, 7, 34, 14, 4, 8, 62};
    int i;

    printf("Values of array with odd index:\n");

    for (i = 1; i < 10; i += 2) /*increment the counter i by 2 */
    {
        printf("Array element %d, is %d\n", i, numArrays[i]); 
    }

    return 0;
}

hahha yea i noticed my mistake a while ago and thanks i read all the comments and was amazed and learned new things thank you so much ^^

This article has been dead for over six months. Start a new discussion instead.