Форум » C/C++ для начинающих (C/C++ for beginners) » array with alternate even and odd » Ответить

array with alternate even and odd

learnernew79: hey, as discussed program for alternate even and odd numbers [pre2] #include<stdio.h> int main() { int a[]={9,8,8,7,6,5,14}; int n= sizeof(a) / sizeof(a[0]); int i,j; int t1; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[ i ]>a[ j ]) { t1=a[ i ]; a[ i ]=a[ j ]; a[ j ]=t1; } } } [/pre2]

Ответов - 26, стр: 1 2 All

Сыроежка: Hi, If you need simply to output an array alternating odd and even numbers of the array then the program (written in C) can look the following way [pre2] #include <stdio.h> int main( void ) { int a[] = { 9, 8, 8, 7, 6, 5, 14 }; const size_t N = sizeof( a ) / sizeof( *a ); for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[ i ] ); printf( "\n" ); int *odd = a, *even = a; int i = 0; while ( odd != a + N && even != a + N ) { if ( i ^= 1 ) { while ( odd != a + N && *odd % 2 != 1 ) ++odd; if ( odd != a + N ) printf( "%d ", *odd++ ); } else { while ( even != a + N && *even % 2 != 0 ) ++even; if ( even != a + N ) printf( "%d ", *even++ ); } } for ( ; odd != a + N; ++odd ) if ( *odd % 2 == 1 ) printf( "%d ", *odd ); for ( ; even != a + N; ++even ) if ( *even % 2 == 0 ) printf( "%d ", *even ); printf( "\n" ); return 0; } [/pre2] Its output is the following [pre2] 9 8 8 7 6 5 14 9 8 7 8 5 6 14 [/pre2]

learnernew79: what is Int *odd=a and ^ operator?

Сыроежка: This record [pre2] int *odd = a; [/pre2] is equivalent to [pre2] int *odd = &a[0]; [/pre2] and declares a pointer that points to the first element of the array. So for example expression *odd returns the element of the array that is pointed to by pointer odd. For example after the declaration above *odd is equivalent to a[0]. In the program it is more suitable to use pointers instead of indices. Operator ^ is the bitwise exclusive XOR operator For example 0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0 So in the program expression i ^ 1 is either 1 or 0.


learnernew79: okay, i got it, is it possible to use any simple statements and execute instead of pointers and bitwise?

Сыроежка: You could use indices instead of pointers and instead of the bitwise operator you could use a bool variable. For example something like this [pre2] size_t odd = 0, even = 0; bool odd_turn = true; while ( odd != N && even != N ) { if ( odd_turn ) { odd_turn = false; //... [/pre2]

learnernew79: can you please implement and show me? sorry for the trouble, i am learning stuffs, so

Сыроежка: If to use indices then the function can look the following way [pre2] #include <stdio.h> int main( void ) { int a[] = { 9, 8, 8, 7, 6, 5, 14 }; const size_t N = sizeof( a ) / sizeof( *a ); for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[ i ] ); printf( "\n" ); size_t odd = 0, even = 0; int odd_turn = 1; while ( odd != N && even != N ) { if ( odd_turn ) { while ( odd != N && a[odd] % 2 != 1 ) ++odd; if ( odd != N ) printf( "%d ", a[odd++] ); } else { while ( even != N && a[even] % 2 != 0 ) ++even; if ( even != N ) printf( "%d ", a[even++] ); } odd_turn = !odd_turn; } for ( ; odd != N; ++odd ) if ( a[odd] % 2 == 1 ) printf( "%d ", a[odd] ); for ( ; even != N; ++even ) if ( a[even] % 2 == 0 ) printf( "%d ", a[even] ); printf( "\n" ); return 0; } [/pre2] By the way do not forget to select the best answer at SO for your previous question. In this case you will get some points to your reputation and will be able to ask several questions per day.

learnernew79: 1. const size_t N = sizeof( a ) / sizeof( *a ); for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[ i ] ); printf( "\n" ); size_t odd = 0, even = 0; here, why size_t why not int? i want it to be simpler, so 2. for ( ; odd != N; ++odd ) here, why no initialization like odd=0 and ++odd and odd++ is same? P.S sure, will select your answer as best one, thanku for helping and sorry for asking so many questions

Сыроежка: The operator sizeof yields a result of type size_t. And constant N is declared correspondingly as having type size_t and it will be compared with variables odd and even. So it is better to declare them as having the same type size_t because in fact they will be compared with the expression sizeof( a ) / sizeof( *a ) As for the second question then we should output elements of the array that do not have a pair of an even or odd element. That is we need to output the "tail" of the array starting either with current value of odd or even depending on whether there are more odd elements or even elements in the array.

learnernew79: so size_t is user defined type? can we do "int N = sizeof( a ) / sizeof( *a ); " and 2nd one, why no initizalyion, i asked in for loop. is it same for ( odd=0; odd != N; ++odd ) and for ( ; odd != N; ++odd ) is same?

Сыроежка: size_t is an implementation defined typedef for some unsigned integer type. We can do this [pre2] int N = sizeof( a ) / sizeof( *a ); [/pre2] provided that type int can accomodate the value returned by the right side operand. Nevertheless it is a bad style of programming and is error-prone. As for the loops then there are used the current values of odd and even. It would be an error to initialize them. These loops are used to append the "tail" of the array to the already ordered sequence.

learnernew79new: yea, got it, thanks for clearing the doubts and giving a new perspective in solving a problem :) P.S : the another problem which u programmed in stackoverflow.com please correct the output for me

Сыроежка: What do you mean saying about another problem?

learnernew79new: #include <stdlib.h> #include <stdio.h> int cmp( const void *lhs, const void *rhs ) { int a = *( const int * )lhs; int b = *( const int * )rhs; return ( b < a ) - ( a < b ); } int main() { int a[] = { 9, 8, 8, 7, 6, 5, 14 }; const size_t N = sizeof( a ) / sizeof( *a ); qsort( a, N, sizeof( int ), cmp ); /* for ( size_t i = 0; i < N; i++ ) printf( "%d ", a ); printf( "\n" ); */ int *p = a; int *start = a, *end = a; do { if ( ++p == a + N || *p != *end + 1 ) { printf( "{ %d", *start ); start == end ? printf( " }\n" ) : printf( ", %d }\n", *end ); start = end = p; } else { end = p; } } while ( p != a + N ); } this one i want Output: 1, 3-5, 7-9 not { 5, 8 } { 8, 9 } { 14 }

Сыроежка: Try to do it yourself. It is not difficult.

learnernew79new: i tried, outputs comes different, atleast say me, those lines which should change, not the whole program

Сыроежка: How did you get these values 1, 3-5, 7-9 ?

learnernew79new: i dint get it, i need to get it that way, its the illustration of how output should be

Сыроежка: Here you are. [pre2] #include <stdlib.h> #include <stdio.h> int cmp( const void *lhs, const void *rhs ) { int a = *( const int * )lhs; int b = *( const int * )rhs; return ( b < a ) - ( a < b ); } int main( void ) { int a[] = { 9, 8, 8, 7, 6, 5, 14 }; const size_t N = sizeof( a ) / sizeof( *a ); qsort( a, N, sizeof( int ), cmp ); /* for ( size_t i = 0; i < N; i++ ) printf( "%d ", a ); printf( "\n" ); */ int *p = a; int *start = a, *end = a; do { if ( ++p == a + N || *p != *end + 1 ) { start == end ? printf( "%d", *start ) : printf( "%d-%d", *start, *end ); if ( p != a + N ) printf( ", " ); start = end = p; } else { end = p; } } while ( p != a + N ); printf( "\n" ); } [/pre2] The program output is [pre2] 5-8, 8-9, 14 [/pre2] And do not forget to select the best answer for that thread at SO.

learnernew79new: int cmp( const void *lhs, const void *rhs ) { int a = *( const int * )lhs; int b = *( const int * )rhs; return ( b < a ) - ( a < b ); } can u explain this part? y *? in here too int *p ; why so many pointers? y not variables? :(

Сыроежка: Pointers are also variables. When you deal with an array then 1) the array name in expressions is converted to pointer and 2) to traverse an array you have to use either pointers or indices.

learnernew79new: so, how to convert a given pointer to indices? is it possible? or vice vers?

Сыроежка: I already showed you early the function that uses indices instead of the pointers.

learnernew79new: yea, u did, just small confusions, any pdf u recommend for reading?

Сыроежка: You should read a book on C++ for beginners. I think you can find a list of books at SO. As for me then I do not read books for beginners so I can not say what books for beginners are published now.

learnernew79new: yea, thanx a lot for help. cleared so many doubts. nice meeting u, sir



полная версия страницы