Home > front end >  function that returns a version of the given arrays where each zero value in the array is replaced b
function that returns a version of the given arrays where each zero value in the array is replaced b

Time:06-16

I'm having a bit of trouble with this problem. The full text of the problem is as follows : "Write a function that returns a version of the given array of non-negative integers where each zero value in the array is replaced by the smallest odd value to the right of the zero in the array. If there is no odd value to the right of the zero, leave the zero as a zero."

Here is my code:

#include <stdio.h>

void lowestOdd(int num[], int size) {
    int i, temp;
    for (i = 0; i < size; i  ) {
        if (num[i] % 2 != 0 && num[i] < num[i   1]) {
            temp = num[i];
        }
    }

    for (i = 0; i < size; i  ) {
        if (num[i] = 0) {
            num[i] = temp;
        }
    }
}

void printArray(int array[], int size) {
    int i;
    for (i = 0; i < size; i  ) {
        printf("%d/n", array[i]);
    }
}

int main() {
    int i, size;
    int myarr[20];
    printf("What is the size of your array? \n");
    scanf("%d", &size);

    for (i = 0; i < size; i  ) {
        scanf("%d", &myarr[i]);
    }
    lowestOdd(myarr[20], size);
    printArray(myarr[20], size);
    return 0;
}

I've tried implementing pointers in the lowestOdd function, but to no avail. I do think they're necessary here, but I'm not really that good at pointers. The warnings I get are mostly 'warning: passing argument 1 of 'lowestOdd' makes pointer from integer without a cast [-Wint-conversion]'. Also, in my code, I haven't added the statements that would check whether the number is a zero or whether there are any odd values to the right of the zero.

CodePudding user response:

In the declaration

int myarr[20];

myarr is the identifier - the name used to refer to the array itself. myarr has the type int [20].

When used in this expression

lowestOdd(myarr[20], size);

[20] is the array subscript operator, accessing index 20. This is index is out of bounds, as the valid indices for the type int [20] are 0 to 19. This out of bounds access will cause Undefined Behaviour.

This warning

warning: passing argument 1 of 'lowestOdd' makes pointer from integer without a cast [-Wint-conversion]

is given because, although an invalid index to access, the expression myarr[20] evaluates to an int. lowestOdd expects an int * as its first argument.

Similar to before, in

if (num[i] % 2 != 0 && num[i] < num[i   1])

num[i 1] will access num[size] when i is size - 1 (again, valid indices are 0 to size - 1).

This is assignment

if (num[i] = 0)

where you want a comparison

if (num[i] == 0)

Note that

scanf("%d", &size);

for (i = 0; i < size; i  ) {
    scanf("%d", &myarr[i]);

risks the same out of bounds access if the user enters a value greater than 20 for size.


Ignoring the out of bounds access for a moment, lowestOdd attempts to find the last occurrence of the smaller number from each pair of numbers in the array, where the left number must be odd.

It then replaces all zeroes in the array with this value.

There is a chance temp is never assigned anything, and thus has an indeterminate value.

This is not correct.


Here is an example program (using a variable-length array).

Note that the syntax array i is equivalent to &array[i].

? : is the conditional operator: in a ? b : c, if a is non-zero the expression evaluates to b, else it evaluates to c.

#include <stdio.h>

int min(int a, int b)
{
    return a < b ? a : b;
}

int find_lowest_odd(int *base, size_t len)
{
    int value = 0;

    for (size_t i = 0; i < len; i  )
        if (base[i] & 1) /* base[i] % 2 != 0 */
            value = value ? min(value, base[i]) : base[i];

    return value;
}

void mutate_array(int *a, size_t len)
{
    for (size_t i = 0; i < len; i  )
        if (0 == a[i])            /* search from the next position */
            a[i] = find_lowest_odd(a   i   1, len - i - 1);
}

void print_array(int *a, size_t len)
{
    for (size_t i = 0; i < len; i  )
        printf("%d ", a[i]);

    putchar('\n');
}

int main(void) {
    size_t size;

    printf("What is the size of your array?: ");

    if (1 != scanf("%zu", &size))
        return 1;

    int array[size];

    for (size_t i = 0; i < size; i  ) {
        printf("#%zu: ", i   1);

        if (1 != scanf("%d", array   i))
            return 1;
    }

    print_array(array, size);
    mutate_array(array, size);
    print_array(array, size);
}

I/O:

What is the size of your array?: 10
#1: 0
#2: 2
#3: 0
#4: 5
#5: 3
#6: 0
#7: 7
#8: 2
#9: 0
#10: 0
0 2 0 5 3 0 7 2 0 0
3 2 3 5 3 7 7 2 0 0

CodePudding user response:

Once you can try this.
Here's my naive approach. For every zero in the array it is checking for the smallest odd element from that position to the last index and storing it in variable named smaller. After checking it replaces the original value of that index with smaller one.

#include<stdio.h>  
void lowestOdd(int *num, int size){
    int i, j;
    for(i = 0; i < size - 1; i  ){
        if (num[i] != 0) continue;
        int smaller = 99998;
        for(j = i 1; j < size; j  ){
            if (num[j] % 2 != 0 && num[j] < smaller) smaller = num[j];
        }
        if (smaller != 99998) num[i] = smaller;
    }
}

void printArray(int *array, int size){
    int i;
    for (i=0; i<size; i  ){
        printf("%d\n", array[i]);
    }
}

int main()  
{  
    int i, size;
    int myarr[20];
    printf("What is the size of your array? \n");
    scanf("%d", &size);
    
    for (i=0; i<size; i  ){
        scanf("%d", &myarr[i]);
    }
    lowestOdd(myarr, size);
    printArray(myarr, size);
    return 0;  
}
  • Related