Angular, Blockchain, Science とか

Angular, Blockchain, Science全般 の情報を主に書いていきます。

C 配列の要素の交換

ただの配列の要素の交換

#include <stdio.h>

#define FALSE 0
#define TRUE 1
#define ASIZE 8

void display_array(int array[]);
void find(int array[], int value);
int search(int array[], int item);

main()
{
    int nums[ASIZE] = {1,34,54,22,46,75,2,25};
    
    printf("This is the initial array: \n");
    display_array(nums);
    find(nums, 7);
    find(nums, 46);
}

void display_array(int array[])
{
    int index;
    
    for(index=0; index<ASIZE; index++){
        printf("%4d", array[index]);
    }
    printf("\n");
}

void find(int array[], int value)
{
    int where;
    printf("The value %d was", value);
    where = search(array, value);
    if (where == -1)
        printf("not ");
    printf("found in the array ");
    if(where != -1)
        printf("at index %d", where);
    printf("\n");
}

int search(int array[], int item)
{
    int index;
    int found;
    
    found=FALSE;
    index=0;
    while((index<ASIZE) && !found)
    {
        if(array[index] == item)
            found = TRUE;
        else
            index++;
    }
    if (found)
        return index;
    else
        return -1;
}