PLS HELP IN THE CODE ^_^

#include <stdio.h>
#include <stdlib.h>
void swap(int *a , int *b){
    int temp ;
    temp = *a ;
    *a = *b;
    *b = temp;
}
void sort(int arr[] , int n ){
    int i , j , min_id ;
    for(i = 0 ; i < n-1 ; i++){
        min_id = i ;
        for(j = i+1 ; j < n ; j++ ){
            if(arr[j] < arr[min_id]){
                min_id = j;
            }
        }
        swap(&arr[min_id] , &arr[i]);
    }
}
int main(void) {
	int L , R  , n , i , j = 0  , *p ,  count = 0   ;
	scanf("%d %d" , &L,&R);
	for(i = L ; i <= R ; i++){
	    if(i%2 != 0 ){
	        count++ ; 
	    }
	}
	p = (int * )malloc(count * sizeof(int));
	for(i = L ; i <= R ; i++){
	    if(i%2 != 0 ){
	        p[j] = i ;
	        j++;
	    }
	}
	n = sizeof(p)/sizeof(int);
	sort(p , n);
	for(i = 0 ; i < n ; i++){
	    printf("%d " , p[i]);
	}
	return 0;
}

this is outputting only 2 values whereas it should be outputting all the odd values in ascending order within the given range .
eg :> I input 2 and 9 it outputs 3 5
whee as it should output 3 5 7 9

Please format your code - the forum software has mangled it and it won’t compile! :slight_smile:

1 Like

thanx i have formatted my code :slight_smile:

1 Like

Thanks :slight_smile: This line:

sizeof(p)/sizeof(int);

doesn’t do what you think it does: p is a pointer to int, so sizeof(p) always returns the same value, (8 on my machine).

Instead, do n = count, or just forget about n and use count instead.

1 Like

ouuuhhhh now i get it , thanx a lot for telling , i tried to like use sizeof to get the size of the array but now i see the prob . :smile:

1 Like