返回c中向量中止问题的每个第s个分量

jw5wzhpr  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(125)

我们的任务是创建一个程序,在其中输入一个向量,它返回该向量的每个第s个分量。例如,如果x =(1,2,3,4,5,6),s = 2,则输出为(1,3,5)。但我得到了一个zsh中止警告。

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

这是我目前为止的计划!

ruarlubt

ruarlubt1#

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

相关问题