【发布时间】:2019-02-01 16:42:43
【问题描述】:
我开始学习C编程,所以我不知道如何做基本的事情(可能是我所做的没有意义)。 我要做的是从用户那里获得一个未定义的 1-9 整数并将其加载到一个数组中,当用户输入 0 或 10 时,循环完成并且数组完成,所以我有: int 资格[SIZEQ] 和加载它的函数,它可以工作。 现在我必须计算这个数组中引入的每个数字的频率。我想有很多方法可以简单地做到这一点(我不知道),我正在尝试使用另一个二维数组来完成它,它有 2 行和 10 个列,使用 1 行来包含来自的值0 到 9 将每个数字与数组限定 [] 的数字进行比较。另一行存储每个数字在资格中出现的次数。不知道怎么弄,我试试:
// 1st row of the freq array contains the 1-9 possible qualifications
// 2nd row to count the times that every value appears in qualifications array
// example: 1st row {0,1,2,3...} if 1 repeated 3 times:
// 2nd row {0,2,0,0...}
int freq[2][10]={{0,1,2,3,4,5,6,7,8,9},{0,0,0,0,0,0,0,0,0,0}};
int j=0; //var to load 0-9 positions of freq[1][j]
for (int i = 0; i < SIZEQ; i ++)
{
if(qualifications[i]==freq[0][j]){
//what i want to do is to compare from 0 each cell of
//qual with freq with 1-9 numbers
freq[1][j] = freq[1][j] + freq[1][j];
//if detected a coincidence, increment the field of the row to
//count repetitions
//so, if qualifications[i] it's now '2' when compared to
//freq[0][2] (what it's 2) freq[1][2] it's now 1.
}
j=i;
if(j>=9){ //to avoid that j be bigger than freq column size
j=0;
}
}
j=1; //skip 0
while(j<=9){
//j print 1-9 numbers and freq[1][j] print the number of
//times it is repeated
cout << "Qualification " << j << " repeated " << freq[1][j] << " times." << endl;
j++;
}
}
【问题讨论】:
-
所以你想要一个函数来计算出现次数吗? stackoverflow.com/questions/54476881/…
-
嗯...问题是在我的学校我们还没有学习课程,std:: 的使用以及该代码的许多其他内容,我不理解该代码.. . 我们的家庭作业是计算用户输入的数组和数组的频率,我们只知道数组、循环,并没有更多......
-
因此您需要将其重新标记为c。这不是 C++。不过,您使用
std::cout。无论如何,祝你好运。 -
好的,已编辑。
-
@YSC 如果它使用
std::cout,你怎么能说它“不是 C++”?