简体   繁体   English

如何使用C ++计算1-100数组中的覆盖率百分比?

[英]How to calc percentage of coverage in an array of 1-100 using C++?

This is for an assignment so I would appreciate no direct answers; 这是一项任务,因此不希望直接回答。 rather, any logic help with my algorithms (or pointing out any logic flaws) would be incredibly helpful and appreciated! 相反,对我的算法的任何逻辑帮助(或指出任何逻辑缺陷)都将非常有用并受到赞赏!

I have a program that receives "n" number of elements from the user to put into a single-dimensional array. 我有一个程序可以从用户那里接收“ n”个元素并将其放入一维数组中。 The array uses random generated numbers. 该数组使用随机生成的数字。 IE: If the user inputs 88, a list of 88 random numbers (each between 1 to 100) is generated). IE:如果用户输入88,则会生成一个88个随机数的列表(每个数字在1到100之间)。 "n" has a max of 100. “ n”的最大值为100。

I must write 2 functions. 我必须编写2个函数。

Function #1: 功能#1:

Determine the percentage of numbers that appear in the array of "n" elements.
So any duplicates would decrease the percentage.
And any missing numbers would decrease the percentage.
Thus if n = 75, then you have a maximum possible %age of 0.75 
(this max %age decreases if there are duplicates)

This function basically calls upon function #2. 

FUNCTION HEADER(GIVEN) = "double coverage (int array[], int n)"

Function #2: 功能2:

Using a linear search, search for the key (key being the current # in the list of 1 to 100, which should be from the loop in function #1), in the array.

Return the position if that key is found in the array 
(IE: if this is the loops 40th run, it will be at the variable "39", 
and will go through every instance of an element in the array 
and if any element is equal to 39, all of those positions will be returned? 
I believe that is what our prof is asking)

Return -1 if the key is not found. 

Given notes = "Only function #1 calls function #2, 
and does so to find out if a certain value (key) is found among the first n elements of the array."

FUNCTION HEADER(GIVEN) = "int search (int array[], int n, int key)"

What I really need help with is the logic for the algorithm. 我真正需要帮助的是算法的逻辑。

I would appreciate any help with this as I would approach this problem completely differently than our professor wants us. 我将不胜感激,因为我将以完全不同于我们教授希望的方式解决这个问题。

My first thoughts would be to loop through function #1 for all variable keys of 1 through 100. And in that loop, go to the search function (function #2), in which a loop would go through every number in the array and add to a counter if a number was (1)a duplicate or (2) non-existent in the array. 我的第一个想法是针对1到100的所有可变键,通过函数#1循环。然后在该循​​环中,转到搜索功能(函数#2),其中循环将遍历数组中的每个数字并加如果一个数字在数组中是(1)一个重复项或(2)不存在,则返回计数器。 Then I would subtract that counter from 100. Thus if all numbers were included in the array except for the #40 and #41, and then #77 was a duplicate , the total percentage of coverage would be 100 - 3 = 97%. 然后,我将从100中减去该计数器。因此,如果数组中除#40和#41之外的所有数字都包含在内,然后#77是重复的,则覆盖率的总百分比将为100-3 = 97%。

Although as I type this I think that may in of itself be flawed? 尽管在我键入此内容时,我认为可能本身存在缺陷? ^ Because with a max of 100 elements in the array, if the only number missing was 99, then you would subtract 1 for having that number missing, and then if there was a duplicate you would subtract another 1, and thus your percentage of coverage would be (100-2) = 98, when clearly it ought to be 99. ^因为数组中最多包含100个元素,如果缺少的唯一数字是99,则由于缺少该数字,您将减去1,然后如果有重复的数字,则将减去另外1,因此覆盖率也将减少(100-2)= 98,显然应该为99。

And this ^ is exactly why I would REALLY appreciate any logic help. 而这正是我非常感谢任何逻辑帮助的原因。 :) :)

I know I am having some problems approaching this logically. 我知道我在逻辑上要解决这个问题。

I think I can figure out the coding with a relative amount of ease; 我想我可以相对轻松地弄清楚编码。 what I am struggling witht he most is the steps to take. 我最努力的是要采取的步骤。 So any pseudocode ideas would be amazing! 因此,任何伪代码想法都将是惊人的!

(I can post my entire program code so far if necessary for anyone, just ask, but it is rather long as of now as I have many other functions performing other tasks in the program) edit: I am seeing multiple people edit my post/tags. (到目前为止,如果有人需要,我可以发布我的整个程序代码,只是问一下,但由于我还有很多其他功能可以执行程序中的其他任务,所以现在已经很久了)编辑:我看到很多人在编辑我的帖子/标签。 The tags are not ones I put xD So please don't downvote my question because you had to edit this?D: Someone just edited this back to the original way it was before someone else edited it. 标记不是我放入xD的标记,所以请不要拒绝我的问题,因为您必须编辑此标记?D:有人刚刚将其编辑回原来的方式,直到其他人对其进行了编辑。 Ah

I may be mistaken, but as I read it all you need to do is: 我可能会误会,但在阅读时,您需要做的是:

  • write a function that loops through the array of n elements to find a given number in it. 编写一个循环遍历n个元素的数组以在其中找到给定数字的函数。 It would return the index of first occurence, or a negative value in case the number cannot be found in the array. 它会返回第一次出现的索引,如果在数组中找不到该数字,则返回一个负值。
  • write a loop to call the function for all numbers 1 to 100 and count the finds. 编写一个循环以为所有1到100的数字调用该函数并计算发现。 Then divide the result by 100. 然后将结果除以100。

I'm not sure if I understand this whole thing right, but 1 function you can do it, if you don't care about speed, it's better to put array into a vector , loop through 1..100 and use boost find function http://www.boost.org/doc/libs/1_41_0/doc/html/boost/algorithm/find_nth.html . 我不确定我是否理解正确,但是可以使用1个函数,如果您不关心速度,最好将数组放入vector ,遍历1..100并使用boost find函数http://www.boost.org/doc/libs/1_41_0/doc/html/boost/algorithm/find_nth.html There you can compare current value with the second entry value in the vector, if it contains you decrease, not not decrease, if you want to find if the unique number is in array, use http://www.cplusplus.com/reference/algorithm/find/ . 在那里,您可以将当前值与向量中的第二个输入值进行比较,如果该值包含您递减的值,而不是不递减的话,如果要查找唯一数字是否在数组中,请使用http://www.cplusplus.com/reference / algorithm / find / I don't understand, how the percentage decreases, so it's on your own and I don't rly understand second function, but if its linear search use again find. 我不明白百分比是如何减少的,​​所以它是靠您自己的,我也不愿意了解第二个函数,但是如果再次使用线性搜索可以找到它。 PS Vector description http://www.cplusplus.com/reference/vector/vector/begin/ . PS矢量说明http://www.cplusplus.com/reference/vector/vector/begin/

You want to know how many numbers in the range [1, 100] appear in your given array. 您想知道在给定数组中出现[1,100]范围内的数字。 You can search for each number in turn: 您可以依次搜索每个数字:

size_t count_unique(int array[], size_t n)
{
    size_t result = 0;

    for (int i = 1; i <= 100; ++i)
    {
        if (contains(array, n, i))
        {
            ++result;
        }
    }

    return result;
}

All you still need is an implementation of the containment check contains(array, n, i) , and to transform the unique count into a percentage (by using division). 您所需要做的只是实现包含检查contains(array, n, i) ,并将唯一计数转换为百分比(使用除法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM