我如何在数组中找到特定值的频率
How do I find the frequency of a specific value in an array?
我是C 编程中的新手,我关注这本书,名为Alex Allain的C ,这本书中有一个TIC-TAC-TOE练习,我正在很难完成该练习,到目前为止,我所做的是提示用户输入X或O值,该值存储在2D数组中,我想跟踪例如Char X在数组中出现3次,所以我使用Counter ,但它只会增加一次。以下是我到目前为止所做的事情的来源,希望它将使我的问题更加清晰,并让我知道我的代码的结构和功能等方面的方式:
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
void display_array(char array[][3]);
void check_input(char array[][3], int input);
void check_winner(char array[][3]);
int check_x(char array[][3]);
int check_o(char array[][3]);
int _tmain(int argc, _TCHAR* argv[])
{
char array[3][3];
int counter = 0;
for(int row = 0; row < 3; row++){
cout << "n";
for(int col = 0; col < 3; col++){
counter++;
array[row][col] = counter;
}
}
display_array(array);
system("PAUSE");
return 0;
}
void display_array(char array[][3]){
int position_input;
string symbol_input;
do{
for(int i=0; i < 3; i++){
for(int j=0; j < 3; j++){
cout << " [ ";
cout << array[i][j];
cout << " ] ";
}
cout << "n";
}
cout << "Position: ";
cin >> position_input;
check_input(array, position_input);
}while(position_input != 0);
}
void check_input(char array[][3], int input)
{
char user_input = input;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(user_input == array[i][j])
{
cout << "array[" << i << "][" << j << "] replace with: ";
cin >> array[i][j];
}
}
}
check_winner(array);
}
void check_winner(char array[][3]){
cout << check_x(array);
if(check_x(array) == 3){
cout << check_x(array);
}
else if(check_o(array) == true){
cout << "o";
}
}
int check_x(char array[][3]){
int counter, x[3];
counter = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(array[i][j] == array[i][j]){
counter++;
}
x[i] = counter;
return x[i];
}
}
}
int check_o(char array[][3]){
int counter;
counter = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(array[i][j] == 'o'){
counter++;
return counter;
}else{
return counter;
}
}
}
}
您有很多小问题,但最直接的问题似乎是这种事情:
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(array[i][j] == 'o'){
counter++;
return counter;
}else{
return counter;
}
}
}
无论测试元素是否为" O",您将在一个循环后返回。将其更改为这样的东西:
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(array[i][j] == 'o')
counter++;
}
}
return counter;
我的答案:
问题的最后一部分首先:编码看起来不错,如果您会评论一些操作,即使程序非常引人注目(///* */)。
,我会很高兴我认为,在解决练习方面,您的问题有些误导。我一开始就以同样的方式想到了你的方式。问题在于该模式不仅是由3的频率检测到的,因为它允许错误的检查模式。使其视觉:
7 8 9
4 5 6
1 2 3
f.e。3(例如1,2,5或9 6 5)的频率不是正确的答案,而是-1 5 9。即使您计算了每个频率模式 - 它的编码要比我发现的解决方案所定义的解决方案要多得多:
: status_check = possibilities(field,player,2,0,1,1,0,2); //diagonal 1 5 9
status_check = possibilities(field,player,0,0,1,1,2,2); //diagonal 7 5 3
status_check = possibilities(field,player,0,0,1,0,2,0); //vertical 7 4 1
status_check = possibilities(field,player,0,1,1,1,2,1); //vertical 8 5 2
status_check = possibilities(field,player,0,2,1,2,2,2); //vertical 9 6 3
status_check = possibilities(field,player,0,0,0,1,0,2); //horizontal 7 8 9
status_check = possibilities(field,player,1,0,1,1,1,2); //horizontal 4 5 5
status_check = possibilities(field,player,2,0,2,1,2,2); //horizontal 1 2 3
*这就是我如何用num pad锻炼的方式。(我真的为那件代码感到自豪。
我喜欢那本书,但可悲的是我不知道那本书:"奖金:您可以让您的程序检测是否无法赢得游戏在整个网格填充之前的任何一侧?"但是,如果有人跳过该章 - 此解决方案肯定会有所帮助:*
练习就像以下(第121页,跳到CPP):
写一个小型的TIC-TAC TOE程序,允许两个玩家玩 Tic-Tac-Toe竞争性。您的程序应该检查是否是否 玩家赢了,或者如果董事会完全填充(游戏 以领带结束)。奖金:您可以让您的程序检测到游戏是否 在填充整个网格之前,不能赢得任何一侧?
以下代码可以简单地使用C 17标准进行编译和执行,并且所有库应与GCC(GCC)8.2.1
一起使用。#include <iostream>
#include <string>
#include <limits> //just for the input validation
using namespace std;
int input_check(char field[3][3],char player);
void display_field(char field[3][3],int size);
void initialize_field(char field[3][3],int size,char player);
string win_check(char field[3][3],char player,int size);
int code(char field[3][3],int i,int j,char player);
int possibilities(char field[3][3],char player,int a,int aa,int b,int bb,int c,int cc);
int main()
{
char field[3][3];
int size = 3;
char player ='-';
initialize_field(field,size,player);
display_field(field,size);
while (field[0][0]!='w')
{
player = 'X';
input_check(field,player);
system("clear");
display_field(field,size);
cout << win_check(field,player,size);
if (field[0][0] =='w')
{
cout << endl;
break;
}
player = 'O';
input_check(field,player);
system("clear");
display_field(field,size);
cout << win_check(field,player,size);
if (field[0][0] =='w')
{
cout << endl;
break;
}
}
return 0;
}
void initialize_field(char field[3][3],int size,char player)
{
int i,k;
for (i = 0; i < size; i++)
{
for (k = 0; k < size; k++)
{
field[i][k]= player;
}
}
}
int input_check(char field[3][3],char player)
{
int input = 0;
bool def = false;
while (def != true)
{
while(!(cin >> input))
{ //check the Input format for integer the right way
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid input. Try again: ";
}
switch (input)
{
case 1:{
def = code(field,2,0,player);
break;}
case 2:{
def = code(field,2,1,player);
break;}
case 3:{
def = code(field,2,2,player);
break;}
case 4:{
def = code(field,1,0,player);
break;}
case 5:{
def = code(field,1,1,player);
break;}
case 6:{
def = code(field,1,2,player);
break;}
case 7:{
def = code(field,0,0,player);
break;}
case 8:{
def = code(field,0,1,player);
break;}
case 9:{
def = code(field,0,2,player);
break;}
default:{
cout << "Invalid input. Try again: " << endl;
break;}
}
}
/*
7 8 9 00 01 02
4 5 6 10 11 12
1 2 3 20 21 22
*/
return input;
}
int code(char field[3][3],int i,int j,char player)
{
int def=0;
if (field[i][j]=='-')
{
field[i][j]=player;
def = true;
}
else
{
cout << "Invalid input. Try again: " << endl;
}
return def;
}
void display_field(char field[3][3],int size)
{
int i,k;
for (i = 0; i < size; i++)
{
for (k = 0; k < size; k++)
{
cout << field[i][k];
cout << " ";
if (k==2) //seperate with new line after the third k
{
cout << endl;
}
}
}
}
string win_check(char field[3][3],char player,int size)
{
string status;
int status_check;
/*
7 8 9 00 01 02
4 5 6 10 11 12
1 2 3 20 21 22
*/
if ((field[0][0]!='-')&&(field[0][1]!='-')&&(field[0][2]!='-') &&(field[1][0]!='-')&&(field[1][1]!='-')&&(field[1][2]!='-')&&(field[2][0]!='-')&&(field[2][1]!='-')&&(field[2][2]!='-'))
{
status = "Rien ne va plus - Nichts geht mehr meine Lieben. Unentschieden";
field[0][0]='w';
}
status_check = possibilities(field,player,2,0,1,1,0,2); //diagonal 1 5 9
status_check = possibilities(field,player,0,0,1,1,2,2); //diagonal 7 5 3
status_check = possibilities(field,player,0,0,1,0,2,0); //vertical 7 4 1
status_check = possibilities(field,player,0,1,1,1,2,1); //vertical 8 5 2
status_check = possibilities(field,player,0,2,1,2,2,2); //vertical 9 6 3
status_check = possibilities(field,player,0,0,0,1,0,2); //horizontal 7 8 9
status_check = possibilities(field,player,1,0,1,1,1,2); //horizontal 4 5 5
status_check = possibilities(field,player,2,0,2,1,2,2); //horizontal 1 2 3
if (status_check == true)
{
status = "Player " + string(1, player) + " won!!";
field[0][0] ='w';
}
return status;
}
int possibilities(char field[3][3],char player,int a,int aa,int b,int bb,int c,int cc)
{
int status;
if ((field[a][aa]==player)&&(field[b][bb]==player)&&(field[c][cc]==player))
{
status = true;
}
return status;
}
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 使用 for 循环 c++ 确定与数组中特定频率匹配的值的函数
- 如何使用二维数组来计算和存储来自其他数组的值的频率
- 我如何在数组中找到特定值的频率
- 给定索引 i,j(j>=i) 如何在子数组(i,j)上找到 A[j] 的频率?
- 是否可以计算出 std::vector 扩展其数组大小的频率
- 获取给定数字集中具有相同数字频率的组数
- 检查数组值连续出现的频率
- 计算 C 数组中三元组的频率以进行索引
- 计数数组中元素的频率
- 给定一个范围,我必须计算数组中数字的频率.给定的解决方案是否有效