C++ 程序函数

标签 c++ arrays

我的函数有问题。当我使用一个函数来操作一个数组,并打印它并转到下一个操作函数时,它使用之前操作过的数组而不是原始数组。例如,当我的函数将每个负数转换为正数时,我调用下一个函数将所有偶数归零,并且我的数组打印出所有零,而不是使用原始数组。

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

#define NUMS_PER_LINE 10    // maximum numbers to be printed on each line.

int numbers[100];   // array to hold upto 100 integer numbers.
int numcnt;     // actual count (<=100) of numbers in above array.

// reads file content into array

void read_array_from_file (const char filename[])
{
    ifstream inpfile(filename);

    if (!inpfile.is_open())
    {
        cout << "Can't open file : " << filename << endl;
        exit(1);
    }

    numcnt=0;   // Initialise count of read numbers

    // Read numbers from the file into array.
    inpfile >> numbers[numcnt];

    while (!inpfile.eof())      // Read until EOF is reached.
    {
        numcnt++;   // Got one more number from input file.
        inpfile >> numbers[numcnt];
    }

    inpfile.close();

    return;
}

// Print out all the values in the array

void print_array_content (int numsinaline)
{
    int i;

    for (i=0; i<numcnt+1; i++)
    {
        if ((i % numsinaline) == 0)
            cout << endl;
        cout << numbers[i] << " ";
    }

    cout << endl << endl;

    return;
}

// calculate average

double calculate_average ()
{
    int i;
    float sum=0;

    for (i=0; i<(numcnt-1); i++)
    {
       sum += numbers[i];
    }

    return (sum/(numcnt-1));
}

// Find numbers larger and smaller than the average.

void find_numbers_smaller_and_larger_than_average (int &larger, int &smaller, int  average)
{
    int i;

    for (i=0; i<(numcnt-1); i++)
    {
        if (numbers[i] < average)
            smaller++;
        else if (numbers[i] > average)
            larger++;
    }

    return;
}

// Convert negative numbers to positive in the array 'numbers'.

void convert_negative_to_positive ()
{
    int i;

    for (i=0; i<(numcnt-1); i++)
    {
        if (numbers[i] < 0)
            numbers[i] *= -1;
    }

    return;
}



// Convert all even numbers into zero.
void zero ()
{
    int i;

    for (i=0; i<numcnt; i++)
    {
        if (numbers[i] > 0)
            numbers[i] *= 0;
    }

    return;
}

最佳答案

首先,您正在为您的数组使用一个全局变量,因此您永远不会将它传递给您的函数。当您更改函数中的全局变量时,它会更改数组中的数据。您应该将该数据传递给函数,而不是使用全局变量。

第二,while(!inpFile.eof()) 不好!不要这样做。

对于文件流:

std::vector<int> numbers;
std::ifstream fin("myfile");
std::copy(std::istream_iterator<int>(fin), std::istream_iterator(),  std::back_inserter<vector<int> >(numbers));

这 3 行会将整个文件读入 vector “numbers”。

第三,在声明函数时,传递数组:

void myFunction(const std::vector<int>& vec); // if you aren't going to change the vector

或者 void myFunction(std::vector& vec);//如果你要改变它

你可以简单地调用它:

myFunction(numbers);

关于C++ 程序函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727438/

相关文章:

c++ - 我可以同时连接到多个蓝牙 HCI 吗?

c++ - 删除后的 CVSListBox 通知

c++ - 使用 boost-asio 时实时将缓冲区写入磁盘

perl - 如何将子例程调用的结果分配给 Perl 中的数组引用?

arrays - 如何按属性类型自动排序 Swift 中的数组?

c++ - Z 阶曲线坐标

python - QGraphicsTextItem 的分页 : Confining text to a specific rectangular area

arrays - MATLAB:如何调用 m 维数组的 "cross-section"?

c - 对数字数组求和但在运行时收到错误

java - 对象数组逻辑问题