计算并查找数组中特定的via用户输入 [英] Count and look for the specific via user inputs within an array

查看:97
本文介绍了计算并查找数组中特定的via用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,它接受三个参数并返回将出现在骰子卷中的3个数。目前,我认为我当前的代码能够返回重复的数字,例如,如果我输入'5,5,3',它会告诉我重复的数字是5.



现在,我被困在我需要告诉我的功能的部分搜索任何3s的用户输入并告诉我3s出现了多少次..

任何指针?



我尝试过:



I am trying to write a function where it takes in three arguments and returns the number of 3s that will appear in the dice rolls. At the moment, I think my current code is able to return the repeating number, for example, if I enter in '5, 5, 3', it will tells me that the repeating number is 5.

Right now, I am stuck at the portion where I need to tell my function to search the user inputs for any 3s and to tell me how many times have the 3s appeared..
Any pointers?

What I have tried:

#include <iostream>
using namespace std;

void dThrees (int a[], int size);

int main()
{
    const int SZ = 3;
    int arr[SZ];    
    
    for (int i = 0; i < SZ; i++)
    {
        cout << "Enter in 3 Values : ";
        cin >> arr[i];
    }
    
    dThrees(arr, SZ);
}

void dThrees (int arr[], int size)
{
  int i, j;
  for(i = 0; i < size; i++)
    for(j = i+1; j < size; j++)
      if(arr[i] == arr[j])
        cout << "Number of time 3s appeared : " << arr[i] << endl;
}  

推荐答案

想想如何用一张纸和一支铅笔手动完成它。

尝试设置一个给你答案的过程,程序应该是你程序的基础。



这段代码看起来像是重用代码检测重复,你刚刚更改了标签。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



您需要更改 dThrees 的逻辑,您了解查找重复项与计算3的逻辑不同。
Think about how you would do it manually with a sheet of paper and a pencil.
Try to device a process that give you the answer, the procedure should be the base of your program.

This code look like a reuse of a code detecting duplicate and you just changed the labels.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

You need to change the logic of dThrees, you understand that finding duplicates is not the same logic as counting 3's.


使用C ++算法

Make use of the C++ algorithms
std::count(&arr[0], &arr[SZ], 3);


这篇关于计算并查找数组中特定的via用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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