Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;,将重复实例从数组返回到字符串 背景:这不是作业,它是对C++基础类的完全可选的复习。在我想通过的时候,我会尽我所能地复习每一个例子,这一个我非常坚持,已经讲了大约三个小时了_C++_Arrays - Fatal编程技术网

C++;,将重复实例从数组返回到字符串 背景:这不是作业,它是对C++基础类的完全可选的复习。在我想通过的时候,我会尽我所能地复习每一个例子,这一个我非常坚持,已经讲了大约三个小时了

C++;,将重复实例从数组返回到字符串 背景:这不是作业,它是对C++基础类的完全可选的复习。在我想通过的时候,我会尽我所能地复习每一个例子,这一个我非常坚持,已经讲了大约三个小时了,c++,arrays,C++,Arrays,问题:编写一个函数返回一个字符串,该字符串由a到z范围内的10 x 10小写字母数组的每行中最常见的小写字母组成 如果有多个最常用的字符,请使用按字母顺序排在第一位的字符 不要使用cin或cout #include <iostream> #include <string> using namespace std; string mostFrequent(char c[10][10]){ // this is the function I need to create }

问题:编写一个函数返回一个字符串,该字符串由a到z范围内的10 x 10小写字母数组的每行中最常见的小写字母组成

如果有多个最常用的字符,请使用按字母顺序排在第一位的字符

不要使用cin或cout

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

string mostFrequent(char c[10][10]){
// this is the function I need to create
}

int main(){
    char c[10][10] = {
    'a','b','f','d','e','f','g','h','i','j',
    'a','b','c','r','c','r','g','h','r','j',
    'a','b','c','d','e','f','g','h','o','o',
    'z','w','p','d','e','f','g','h','i','j',
    'o','d','o','d','o','b','o','d','o','d',
    'a','l','l','d','e','f','f','h','l','j',
    'a','b','c','d','i','f','g','h','i','j',
    'a','b','z','v','z','v','g','g','v','z',
    'a','b','c','d','e','f','g','h','i','e',
    'a','b','s','d','e','f','g','h','s','j',
    };

cout << mostFrequent(c) << endl;
return 0;
}
#包括
#包括
使用名称空间std;
字符串mostFrequent(字符c[10][10]){
//这是我需要创建的函数
}
int main(){
字符c[10][10]={
‘a’、‘b’、‘f’、‘d’、‘e’、‘f’、‘g’、‘h’、‘i’、‘j’,
‘a’、‘b’、‘c’、‘r’、‘c’、‘r’、‘g’、‘h’、‘r’、‘j’,
‘a’、‘b’、‘c’、‘d’、‘e’、‘f’、‘g’、‘h’、‘o’、‘o’,
‘z’、‘w’、‘p’、‘d’、‘e’、‘f’、‘g’、‘h’、‘i’、‘j’,
‘o’、‘d’、‘d’、‘o’、‘b’、‘o’、‘d’、‘o’、‘d’、‘d’,
‘a’、‘l’、‘l’、‘d’、‘e’、‘f’、‘f’、‘h’、‘l’、‘j’,
‘a’、‘b’、‘c’、‘d’、‘i’、‘f’、‘g’、‘h’、‘i’、‘j’,
‘a’、‘b’、‘z’、‘v’、‘z’、‘v’、‘g’、‘g’、‘v’、‘z’,
‘a’、‘b’、‘c’、‘d’、‘e’、‘f’、‘g’、‘h’、‘i’、‘e’,
‘a’、‘b’、‘s’、‘d’、‘e’、‘f’、‘g’、‘h’、‘s’、‘j’,
};

cout如果我正确理解了任务,那么你有一个矩阵10x10,你必须创建一个长度为10的字符串,其中
I
位置的字符是行
I
中最常见的字符

string mostFrequent(char c[10][10])  {
  // The idea here is to find the most common character in each row and then append that character to the string
  string s = "";
  for (int i = 0; i < 10; i++) s += findMostFrequentCharacter(c[i]);
  return s;
}

我已经写了代码,如果有编译错误,对不起。你有问题吗?A)计算算法,或者B你知道算法,但是有一个问题把它翻译成C++。在前者的情况下,你的问题不会与C++有关,在后一种情况下,你应该发布WH。a不管你写了多少代码,都要准确地解释哪些部分你不知道该怎么做。在这两种情况下,stackoverflow.com都不是一个代码编写服务。我清楚地看到我的代码缺少什么计数[256]绝对比我尝试的+96或-96以及for(int I=0;I<10;I++)更优雅s+=findMostFrequentCharacter(c[i]);你有,我完全吹嘘过,这很有道理,你是十种类型的真棒,@NathanRaine给自己留了一个循环:

int counts[256]={0};
将把数组的第一个元素初始化为0,然后,因为还有更多的元素没有在大括号中定义值,所以将数组的其余部分设置为默认值0。你可以从中获得各种乐趣,比如
int counts[256]={1,2,3}前三个元素分别为1、2和3,其余为零。
char findMostFrequentCharacter(char c[10]) {
  int counts[256]; // Assuming these are ASCII characters, we can have max 256 different values
  // Fill it with zeroes (you can use memset to do that, but for clarity I'll write a for-loop
  for (int i = 0; i < 256; i++) c[i] = 0;

  // Do the actual counting
  for (int i = 0; i < 10; i++) // For each character
    counts[ c[i] ]++;  // Increase it's count by 1, note that c[i] is going to have values between 65 (upper case A) and 122 (lower case z)

  char mostFrequent = 0;
  // Find the one that is most frequent

  for (char ch = 'A'; ch <= 'z' ch++) // This will ensure that we iterate over all upper and lower case letters (+ some more but we don't care about it)
    if (counts[ch] > counts[c]) c = ch;  // Take the one that is more frequent, note that in case they have the same count nothing will happen which is what we want since we are iterating through characters in alphabetical order
  return c;
}