Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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;
}  
Posted
Updated 14-Mar-16 23:49pm

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.
 
Share this answer
 
v2
Comments
xenas19 15-Mar-16 3:22am    
I have not been taught on using the debugger yet. But as for my code, I am changing it slightly based on one of the exercises that I did to cater to the function
Patrice T 15-Mar-16 3:28am    
Try to get yourself started with debugger, it is not so complicated.

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.
Arthur V. Ratz 21-Mar-16 3:15am    
+5.
Make use of the C++ algorithms
std::count(&arr[0], &arr[SZ], 3);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900