On-line: гостей 0. Всего: 0 [подробнее..]
Программисты всех стран, объединяйтесь!

АвторСообщение



ссылка на сообщение  Отправлено: 30.07.15 17:32. Заголовок: array with alternate even and odd


hey, as discussed
program for alternate even and odd numbers
 
#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;

}
}
}


Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 26 , стр: 1 2 All [только новые]





ссылка на сообщение  Отправлено: 30.07.15 17:43. Заголовок: Hi, If you need sim..


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
 
#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;
}

Its output is the following
 
9 8 8 7 6 5 14
9 8 7 8 5 6 14


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 30.07.15 23:01. Заголовок: doubt


what is Int *odd=a and ^ operator?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 30.07.15 23:19. Заголовок: This record int *od..


This record
 
int *odd = a;

is equivalent to
 
int *odd = &a[0];

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.


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.07.15 01:40. Заголовок: doubt


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.07.15 01:53. Заголовок: You could use indice..


You could use indices instead of pointers and instead of the bitwise operator you could use a bool variable.

For example something like this
 
size_t odd = 0, even = 0;
bool odd_turn = true;

while ( odd != N && even != N )
{
if ( odd_turn )
{
odd_turn = false;
//...


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.07.15 02:07. Заголовок: doubt


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.07.15 13:23. Заголовок: If to use indices th..


If to use indices then the function can look the following way
 
#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;
}


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.


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.07.15 13:44. Заголовок: doubt


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.07.15 14:20. Заголовок: The operator sizeof ..


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.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.07.15 15:37. Заголовок: doubt


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?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 15:41. Заголовок: size_t is an impleme..


size_t is an implementation defined typedef for some unsigned integer type. We can do this
 
int N = sizeof( a ) / sizeof( *a );

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.


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 19:03. Заголовок: thanks


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 19:43. Заголовок: What do you mean say..


What do you mean saying about another problem?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 20:11. Заголовок: sequence


#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 }

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 20:21. Заголовок: Try to do it yoursel..


Try to do it yourself. It is not difficult.

Спасибо: 1 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 21:48. Заголовок: tried


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 22:15. Заголовок: How did you get thes..


How did you get these values 1, 3-5, 7-9 ?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 22:31. Заголовок: i dint


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 23:03. Заголовок: Here you are. #incl..


Here you are.
 
#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" );
}

The program output is
 
5-8, 8-9, 14


And do not forget to select the best answer for that thread at SO.


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.08.15 23:17. Заголовок: doubt


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? :(


Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 26 , стр: 1 2 All [только новые]
Ответ:
1 2 3 4 5 6 7 8 9
большой шрифт малый шрифт надстрочный подстрочный заголовок большой заголовок видео с youtube.com картинка из интернета картинка с компьютера ссылка файл с компьютера русская клавиатура транслитератор  цитата  кавычки моноширинный шрифт моноширинный шрифт горизонтальная линия отступ точка LI бегущая строка оффтопик свернутый текст

показывать это сообщение только модераторам
не делать ссылки активными
Имя, пароль:      зарегистрироваться    
Тему читают:
- участник сейчас на форуме
- участник вне форума
Все даты в формате GMT  3 час. Хитов сегодня: 10
Права: смайлы да, картинки да, шрифты да, голосования нет
аватары да, автозамена ссылок вкл, премодерация откл, правка нет