用C修改传递数组值回主要功能 [英] Passing modified array values back to main function in C

查看:113
本文介绍了用C修改传递数组值回主要功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果标题仍是模糊的。

Sorry if the title is still ambiguous.

我在做这个任务的学校,以下是我定义的函数原型,主要功能和change_array功能。

I'm doing this assignment for school and below are my defined function prototypes, the main function and the change_array function.

该计划的总体目标是让用户输入5个不同的号码和存储到一个数组。然后什么change_array功能的作用是增加一倍(乘以2)低于10的任何数字,但是,它是目前没有做它旨在做。我真的很坚持,所以我想知道是否有人能指出我的错误。我不要求一个确切的答案,我只是需要一些指引和指导。

The overall objective of this program is to allow users to input 5 different numbers and be stored into an array. Then what the change_array function does is to double (multiply by 2) any numbers that are below 10, however, it is currently not doing what it is intended to do. I'm really stuck, so I was wondering if anyone can point out my mistakes. I'm not asking for an exact answer, I just need some pointers and guidance.

这是怎么回事错的是,change_array功能不改变任何由用户提供的值。因此,例如,如果用户输入3,5,6,12,32,我的节目的输出仍是3,5,6,12,32。但我真正想要的是,6,10,12,12,32的阵列后,从change_array函数传递回来。

What is going wrong is that the change_array function is not changing any of the values given by the users. So for example, if the user inputs, "3, 5, 6, 12, 32", the output of my program is still "3, 5, 6, 12, 32". But what I really want is, "6, 10, 12, 12, 32" after the arrays are passed back from the change_array function.

有完整的程序编辑:

#include <stdio.h>
#define SIZE 5
void fill_array(double x[], int s);
void change_array(double x[], int s);
void print_array(double x[], int s);

main()
{
    double x[SIZE];

    fill_array(x, SIZE);
    printf("The array is as: \n");
    print_array(x, SIZE);
    change_array(x, SIZE);
    printf("After change, the array is: \n");
    print_array(x, SIZE);
}

void fill_array(double x[], int s)
{
    int i=0;

    printf("Please input 5 Non-Negative double numbers\n");
    for (i=0; i<s; i++) {
        printf("Number %d: ", i+1);
        scanf("%d", &x[i]);
        printf("\n");
    }
}

void change_array(double x[], int s)
{
    int i=0;

    for (i=0; i<s; i++) {
        if (x[i] < 10) {
              (x[i] = (x[i] * 2));
        }
    }
}

void print_array(double x[], int s)
{
    int i=0;

    for (i=0; i<s; i++) {
        printf("%ld \t", x[i]);
    }
    printf("\n");
}

我的code用C语言编写。

My code is written in C.

推荐答案

数组总是通过引用传递,所以这不是问题。它的工作原理当我添加打印功能和一个额外的结束大括号预期。你能提供一个最小的工作示例(MWE),这样我们就可以运行code? (我知道这可能应该是一个评论,但我3个点害羞的能够评论)

Arrays are always passed by reference, so that is not the issue. It works as expected when I add a print function and an extra end curly brace. Can you provide a minimal working example (MWE) so that we can just run the code? (I know this should probably be a comment, but I'm 3 points shy of being able to comment)

这篇关于用C修改传递数组值回主要功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆