i want to write a program that prints the value in array which are at odd position, i've this for single dimensional array but confused about 2-D array.

Single dimensional:

int main(void)
{
int num[10];
int i;
for (i=0;i<10;i++)
	{
	printf ("Enter %d value: ",i);
	scanf ("%d",&num[i]);
	}
		for (i=0;i<10;i=i+2)
		printf ("%d ",num[i]);
                
getche ();
}

2 Dimensional effort:

int main(void)
{
int num[10][2];
int i;
for (i=0;i<10;i++)
        {
         for (j=0;j<2;i++)
	  {
	  printf ("Enter %d value: ",i);
	  scanf ("%d",&num[i][j]);
	  }
		for (i=0;i<10;i=i+2)
                    {
                     for (j=0;j<2;j=j+2)
		printf ("%d ",num[i][j]);
                    }
getche ();
}

this incrementation not working for j loop for (j=0;j<2;j=j+2)

Recommended Answers

All 14 Replies

for (j=0;j<2;i++) 3rd expression increment j not i

also you have mismatched braces countof { != countof }

what does it mean ? {I=countof } ???

and whats wrong with the program now

int main(void)
{
int num[3][3];
int i,j;
for (i=0;i<3;i++)
	{
	for (j=0;j<3;j++)
		{
		printf ("Enter %d & %d value: ",i,j);
		scanf ("%d",&num[i]);
		}
	}
		for (i=0;i<3;i=i+2)
		     	{
			for (j=0;j<3;j=j+2)
				{
				printf ("%d ",num[i][j]);
				}
                        }

                
getche ();
}

I see nothing wrong with it particularly so you are going to have to tell us what you want it to do and what it actually does since we have no knowledge of its indented purpose.

scanf ("%d",&num[i]); should have been scanf ("%d",&num[i][j]);

you can check , it is not working , i want the program to print just values at odd positions

you can check , it is not working , i want the program to print just values at odd positions

no, you can check. we don't compile and debug your code for you.

your job here is to post your legible, indented, and syntactically-highlighted code and tell us what the problem is.

our job, if we choose to accept it, is to look at your code and point out the errors or give you hints to improve it.

commented: Yes. +8

no, you can check. we don't compile and debug your code for you.

your job here is to post your legible, indented, and syntactically-highlighted code and tell us what the problem is.

our job, if we choose to accept it, is to look at your code and
point out the errors or give you hints to improve it.

so where is the error ??
why my program not printing the odd position values

scanf ("%d",&num[i]); should have been scanf ("%d",&num[i][j]);

oh sorry..!
now i replaced scanf ("%d",&num[i]); with scanf ("%d",&num[i][j]); but when i m entering values in the output the output is not showing correctly for example;
i entered
1
2
3
4
5
6
7
8
9

the output is 1 3 7 9 , why 5 is missing ?

> for (i=0;i<3;i=i+2)
> for (j=0;j<3;j=j+2)
i is 0 or 2
j is 0 or 2
You're just printing the corners of your matrix.

Try a different loop.

i've changed the loop to:

> for (i=0;i<4;i=i+2)
> for (j=0;j<4;j=j+2)

the output is,
1 3 9 11

why it is not printing values at odd position ?

But all your odd values are not on odd rows/columns are they.

Count from 1 to 9, then work out a row/col from that value (this is a hint).

why should i count from 1 to 9 ? and what is row/col ??

*shrug*

#include <stdio.h>
int main ( ) {
    int mat[3][3] = {
        { 1, 2, 3, },
        { 4, 5, 6, },
        { 7, 8, 9, },
    };
    int i, r, c;
    for ( i = 0 ; i < 9 ; i++ ) {
        if ( (i+1) % 2 == 1 ) {
            r = i / 3;
            c = i % 3;
            printf( "%d ", mat[r][c] );
        }
    }
    printf("\n");

    for ( i = 0 ; i < 9 ; i += 2 ) {
        r = i / 3;
        c = i % 3;
        printf( "%d ", mat[r][c] );
    }
    printf("\n");

    for ( r = 0 ; r < 3 ; r++ ) {
        for ( c = 0 ; c < 3 ; c++ ) {
            if ( mat[r][c] % 2 == 1 ) {
                printf( "%d ", mat[r][c] );
            }
        }
    }
    printf("\n");
    return 0;
}

$ gcc -std=c99 foo.c
$ ./a.out 
1 3 5 7 9 
1 3 5 7 9 
1 3 5 7 9

Thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.