为什么断点显示数组的第二个值是一个大数字?额外学分工作

Why is the breakpoint showing the second value of my array to be a large number? Extra Credit work

本文关键字:数字 一个 工作 数组 显示 断点 第二个 为什么      更新时间:2023-10-16

使用嵌套while循环来计算给定字符串中每个字符的数量,并将这些数字放入数组中。然后找到数组中的最大数字以确定最常见的字符。将此字符返回给调用方。

当放置断点 |down 时(如下所述(,我得到第一个数组值是正确的,第二个数组值不正确。 我不知道我哪里出错了。我不得不承认我现在很疲惫,所以这可能是我忽略的容易的事情。我不知道。

#include <iostream>
using namespace std;
#include <string>
#include <string.h>
char median(char *);
int main() {
const int SIZE = 50;
char thing[SIZE];
char *strPtr;
cout << " give me a string: " << endl;
cin.getline(thing, SIZE);
strPtr = thing;
char mostcommon = median(strPtr);
cout << mostcommon;
}
char median(char *strPtr) {
char holder = 'x';
int numberof[50];
int counter = 0;
int arrayspacecounter = 0;
int thirdcounter;
int fourthcounter;
while (*strPtr != '') {
holder = *strPtr;
while (*strPtr != '') {
strPtr++;
if (holder == *strPtr) {
counter++;
}
}
numberof[arrayspacecounter] = counter; //counts the number of each character. 
arrayspacecounter++;
strPtr++;
counter = 0;
}

v 断点设置在这里 ^

//find the largest number in numberof[]
int largest = 0;
for (thirdcounter = 0; thirdcounter <= 100; thirdcounter++) {
for (fourthcounter = 1; fourthcounter <= 100; fourthcounter++) {
if (largest < numberof[fourthcounter]) {
largest = numberof[fourthcounter];
}
}
}
return *(strPtr + (largest));
}

numberof未初始化,因此最初将包含垃圾值,任何未使用的条目仍将包含断点所在的垃圾值。用:

int numberof[50] = { 0 };

接下来fourthcounter上升到 100,但你numberof只有 50 个元素,将幻数50替换为常数,如下所示MAX_ELEMENTS

const size_t MAX_ELEMENTS = 50;
int numberof[MAX_ELEMENTS] = { 0 };
....
for (thirdcounter = 0; thirdcounter < MAX_ELEMENTS; thirdcounter++)
{
for (fourthcounter = 1; fourthcounter < MAX_ELEMENTS; fourthcounter++)
{

或者,只需使用您已经创建的arrayspacecounter

for (thirdcounter = 0; thirdcounter < arrayspacecounter; thirdcounter++)
{
for (fourthcounter = 1; fourthcounter < arrayspacecounter; fourthcounter++)
{

我不确定为什么你最后有两个 for 循环?外面的似乎是多余的。修复各种其他错误会导致工作功能:

char median(const char* strPtr)
{
const size_t MAX_ELEMENTS = 50;
int numberof[MAX_ELEMENTS] = { 0 };
int counter = 0;
int arrayspacecounter = 0;
int fourthcounter;
const char* temp = strPtr;
while (*temp != '')
{
const char* holder = temp;
while (*temp != '')
{
temp++;
if (*holder == *temp)
{
counter++;
}
}
numberof[arrayspacecounter] = counter; //counts the number of each character. 
arrayspacecounter++;
temp = holder;
temp++;
counter = 0;
}
//find the largest number in numberof[]
int largest = 0;
for (fourthcounter = 1; fourthcounter < arrayspacecounter; fourthcounter++)
{
if (numberof[largest] < numberof[fourthcounter])
{
largest = fourthcounter;
}
}
return *(strPtr + (largest));
}

不过,您的代码可能会简单得多:

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
char median(const char*);
int main()
{
char mostcommon = median("test");
std::cout << mostcommon;
}
char median(const char* strPtr)
{
std::map<char, int> frequencies;
for (auto ch = strPtr; *ch != ''; ch++)
{
frequencies[*ch]++;
}
auto max = std::max_element(frequencies.begin(), frequencies.end(), [](const auto& a, const auto& b) { return a.second < b.second; });
return max->first;
}

您的程序在某处的中位数中出现段错误,我的猜测是您正在尝试在某处使用数组边界之外的位置。

如果您只想计算频率,请尝试更简单的方法,例如这样。

#include <string>
#include<iostream>
int main()
{
std::string phrase;
std::cout << "Enter a phrase n";
std::getline(std::cin,phrase);
int a = 'A'; //index 0 of your array
int count=0;int max=0;
int counter[26] = {0}; //array that will hold your frequencies
char highestFreq;
for(char c:phrase) {
if(!isspace(c)) count = ++counter[int(toupper(c))-a];
if(count>max) max=count, highestFreq=c;
}
std::cout << "max is: "<<max << ". The letter is '"<< highestFreq << "'.n"<< std::endl;
}
相关文章: