c++ 尝试在随机生成的数组中打印出以7结尾的数字的总数[重复]

czfnxgou  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(120)

此问题在此处已有答案

Why does flowing off the end of a non-void function without returning a value not produce a compiler error?(11个回答)
Why does C++ allow missing return value? [duplicate](1个答案)
7天前关闭.

// minicount.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

double evenOut(int arr[], int size, double count) {
    double evenCount = count;
    
    if (size == 0) {
        return evenCount;
    }

    if ((arr[size-1] % 2) == 0 ) {
        evenCount += 1;
        evenOut(arr, size - 1, evenCount);
    }
    else {
        evenOut(arr, size - 1, evenCount);
    }
}

double sevenFact(int arr[], int size, double count) {
    double sevenCount = count;
    string a = to_string(arr[size - 1]);

    if (size == 0) {
        //cout << "Final total number of 7's -> " << sevenCount << endl;
        //cout << "7 to the power of " << sevenCount << " is " << fixed << pow(7, sevenCount) << endl;
        return sevenCount;
    }
    
    if (a[a.length() - 1] == '7') {
        sevenCount += 1;
        sevenFact(arr, size - 1, sevenCount);
    }
    else {
        sevenFact(arr, size - 1, sevenCount);
    }

}

int main()
{
    const string FILE = "randArray.txt";
    const int SIZE = 1000;
    int arr[SIZE];  

    ofstream OUTPUTFILE(FILE);

    srand(time(NULL));

    for (int i = 0; i < SIZE; i++) {
        arr[i] = (rand() % 100) + 1;
    }

    if (OUTPUTFILE.is_open()) {
        for (int i = 0; i < SIZE; i++) {
            OUTPUTFILE << arr[i] << endl;
        }
    }
    else {
        cout << "ERROR: Could not open file." << endl;
    }

    cout << "EvenOut: The number of even numbers in " << FILE << " is: " << evenOut(arr, SIZE, 0) << endl;
   
    double x = sevenFact(arr, SIZE, 0);

    cout << "SevenFact: Total number of numbers ending with 7: " << x;

}

这是我目前的代码设置。我试图打印出这个数组中以7结尾的数字的总数,但是在SevenFact函数调用中返回给x的数字没有正确存储。如果我在sevenFact函数本身中取消注解两行cout,cout打印出正确的总数,但是返回的数字不是这个。
我以同样的方式设置evenCount函数,即正常返回。
但七伯爵有点不对劲,我不知道是什么。

jdgnovmf

jdgnovmf1#

这篇评论更适合你的老师,而不是你;)
看起来你有一个老师(或C源代码)仍然在教你“C”。在C代码中看起来更像这样:
告诉你的老师阅读C++ core guidelines并相应地更新他的材料。你能把这个给他/她看吗?我也很乐意谈论它。

#include <algorithm>
#include <random>
#include <vector>
#include <iostream>

// learn to make functions for small logical steps
// this will make your final code very readable
// describing what is going on. Tip: when you want to 
// write comments in your code it is usually a sign to 
// make a function
auto create_random_values(std::size_t size)
{
    // use C++ random generator
    // to create evenly distributed values between 1-100 (inclusive)
    // I use statics here so the random generation is created
    // on first call, and reused for all later calls. (random generators have state, you should not recreate them all the time)
    static std::random_device entropy_source;
    static std::mt19937 random_generator{entropy_source()};
    static std::uniform_int_distribution<int> uniform_distribution{1,100};

    // Use std::vector in C++ whenever you have an array
    // and only knows its lenght at runtime. 
    std::vector<int> values(size);

    // it is good practice in C++ to use range based (not index based) for loops
    // whenever you can. 
    for(auto& value : values)
    {
        value = uniform_distribution(random_generator);
    }

    // or to describe intent even better, use no loops at all.
    // the last argument is a lambda expression
    // std::generate(values.begin(),values.end(), [&]{ return uniform_distribution(random_generator); });

    return values;
}

bool ends_with_7(const int value)
{ 
    // do NOT use std::pow it is a floating point function and will lead to rounding errors
    return (value % 10) == 7;
}

// pass vector by const reference
// - avoids copies
// - avoids function accidentally changing the content
auto count_numbers_ending_in_seven(const std::vector<int>& values)
{
    // this why C++ algorithm header is so useful
    // it contains all kind of useful reusable functions
    // that work nicely with containers like std::vector
    return std::count_if(values.begin(),values.end(), ends_with_7);
}

int main()
{
    auto values = create_random_values(100);

    for(const auto& value : values)
    {
        std::cout << value << " ";
        if (ends_with_7(value)) std::cout << "\n";
    }

    std::cout << "\nThe number of numbers ending in 7 = " << count_numbers_ending_in_seven(values);

    return 0;
}

字符串

p8h8hvxi

p8h8hvxi2#

您在evenOutsevenFact中都缺少return语句,在进行递归调用时,您需要返回该调用返回的值

  • Lesiak

相关问题