如何使用二维数组来计算和存储来自其他数组的值的频率
How to use a bidimensional array to calculate and store frequencies of values from other array?
我开始学习C编程,所以我不知道如何做基本的事情(可能是我所做的没有意义)。我要做的就是从用户那里获取 1-9 的未定义数量的整数并将其加载到数组中,当用户输入 0 或 10 时,循环结束并且数组完成,所以我有: int quals[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++;
}
}
在原始代码中,您不会根据"freq"中的每个可能值检查每个"限定"。
我认为这样的东西就是你要找的。
#include <iostream>
using namespace std;
int main()
{
//make an example 'qualifications'
int SIZEQ = 5;
const int SIZEQ = 5;
int qualifications[SIZEQ] = {1,1,2,3,4};
int freq[2][10]={{0,1,2,3,4,5,6,7,8,9},{0,0,0,0,0,0,0,0,0,0}};
for (int i = 0; i < SIZEQ; i ++) // loop through each qualification
{
for (int j = 0; j < 10; j++) // loop through each frequency to see if it matches
{
if(qualifications[i]==freq[0][j])
{
freq[1][j] += 1;
}
}
}
int k;
k=1; //skip 0
while(k<=9){
//j print 1-9 numbers and freq[1][j] print the number of
//times it is repeated
cout << "Qualification " << k << " repeated " << freq[1][k] << " times." << endl;
k++;
}
return 0;
}
输出应为:
Qualification 1 repeated 2 times.
Qualification 2 repeated 1 times.
Qualification 3 repeated 1 times.
Qualification 4 repeated 1 times.
Qualification 5 repeated 0 times.
Qualification 6 repeated 0 times.
Qualification 7 repeated 0 times.
Qualification 8 repeated 0 times.
Qualification 9 repeated 0 times.
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 还有其他方法可以在数组中写入多维数组吗?
- 为什么在某些编译器中可以声明像int myarr[noconstant]这样的数组;而不是在其他编译器中?
- 严格的别名是否会阻止您通过其他类型写入 char 数组?
- Sizeof返回的是指针大小,而不是数组大小.有其他方法可以找到尺寸吗
- 对数组进行排序并匹配其他数组
- 对相同其他数组旁边的数组进行排序
- 对数组进行排序并匹配其他数组并得到错误
- 使用传入数组初始化其他数组
- 我无法将二维数组的两个元素的差异分配给C++中其他数组的元素?
- 当我们在 C++ 中的参数中传递它们时,如何修改一些数组而不修改其他数组
- C++ 将文件中的数字读取到数组中,然后将其打印到其他数组中
- 如何使用二维数组来计算和存储来自其他数组的值的频率
- 如何检查数组是否等于任何其他数组C++
- 通过其他数组中的指针对数组的元素进行计数
- 删除数组中的对象,并通过让其他数组对象相应地向左移动来摆脱空数组空间
- 排序一个数组和后面的其他数组
- 如何将其他数组传递给推力的min_element谓词
- 根据其他数组对数组进行排序