Home > OS >  Returning every s-th component of a vector abort problem in c
Returning every s-th component of a vector abort problem in c

Time:11-25

The task was to create a program in which you input a vector and it returnes every s-th component of the vector. For example, if x = (1, 2, 3, 4, 5, 6) and s = 2, the output is (1, 3, 5). But I get a zsh abort warning.

#include <stdio.h>

void sampleVector(double arr[], int n, int s){
    int j = 0;
    for (j=0; j<n; j  ) {
    arr[j] = 0;
    printf("%d: ",j);
    scanf("%lf",&arr[j]);
    }
    int i=1;
    printf("%f,", arr[i]);
    for (i=1; i<n; i  ){
        s=s*i;
        printf("%f", arr[s]);
    }
}

int main() {
    int n;
    scanf("%d", &n);
    double arr[3]={0,0,0};
    int s;
    scanf("%d", &s);
    sampleVector(arr, n, s);
}

This is my program so far!

CodePudding user response:

void printfEveryNth(const int *array, size_t size, size_t n)
{
    if(array && n)
    {
        for(size_t index = 0; index < size; index  = n)
        {
            printf("%d ", array[index]);
        }
        printf("\n");
    }
}

int main(void)
{
    int array[] = {1, 2, 3, 4, 5, 6};

    printfEveryNth(array, 6 , 2);
}

https://godbolt.org/z/e3Tsa8GKj

  • Related