如何将数组的一部分从 int 转换为浮点数并将值设置为另外两个数组
how to convert a part of array from int to float and set the value to another two arrays
编辑:I正在尝试和搜索,但没有找到如何仅将 (j==3) 时的部分转换为浮点数,因为我需要评估(等级或 idk 确切的单词)从 2 到 6 - 它可以是 3,5...所以我需要浮动。好的,检查我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
unsigned int m[25][4], a[25][4], b[25][4];
int i,j,n,k=0,p=0,g=0,t=0;
do
{
cout<<"Enter number of students: ";cin>>n;
}
while(!(n>0 && n<=25));
for(i=0;i<n;i++)
{
cout<<"Student N:"<<i+1<<"n";
for(j=0;j<4;j++)
if(j==0)
{
do
{
cout<<"Fak nomer: ";cin>>m[i][j];
}
while(!(m[i][j]>=10000000 && m[i][j]<100000000));
}
else if(j==1)
{
do
{
cout<<"Enter Speciallity - 52 for E, 61 for AIUT"<<endl;
cout<<"Code: ";cin>>m[i][j];
}
while(!(m[i][j]==52 || m[i][j]==61));
}
else if(j==2)
{
do
{
cout<<"Group: ";cin>>m[i][j];
}
while(!(m[i][j]==1 || m[i][j]==2 || m[i][j]==3 || m[i][j]==4));
}
else
{
//float f=m[i][j];(i tried to replace all m[i][j] with f but..)
do
{
cout<<"Avg. evaluation: ";cin>>m[i][j];
}
while(!(m[i][j]>=2 && m[i][j]<=6));
//m[i][j]=f;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<4;j++)
cout<<setprecision(8)<<m[i][j]<<"t";
cout<<endl;
}
cout<<endl;
}
假设我们将使整个数组浮点,因此我需要根据两个新数组设置信息,如前所述:
if(m[i][1]==52)
a[k][p]=m[i][j]
else if(m[i][1]==61)
b[g][t]=m[i][j]
实际上我需要根据组代码显示它们,如果 52 显示只有 52 的数组......如果 61 只显示 61 的数组,某种排序它们......我尝试了一些方法,但我得到:"-9.2559631e + 061",我想正如我所说,我需要无符号数组才能工作......编辑!
你不能。数组的所有条目类型相同。
不过,有几种方法可以解决它。首先是制作 float
类型的整个数组。另一种是使用类似 Boost Any 类的东西。
不要使用从任意数字索引的数组来存储不同的数据项。 你要做的就是让每个人都头疼。
struct Student
{
unsigned int fak;
unsigned int speciality;
unsigned int group;
float grade;
}
Student students[25];
students[20].group = 1.7f;
在讨论这个问题时,也请停止使用幻数(如果数字 25 出现两次,它应该是一个常数)。
const int studentCount = 25;
Student students[studentCount];
还有其他事情,但请开始编写代码,所有名称都命名,而不是问我们,和你一起工作的人,或者你未来的自己,试图弄清楚j==3代表什么......
相关文章:
- 我如何处理有关输入的字符串大小超过字符数组设置大小的错误?
- 将字符数组设置为彼此相等的方法不起作用
- 将数组设置在一个对象中,等于另一个对象中的数组
- C++ |如何将整个 2d 数组设置为 O
- 将 2D 数组设置为函数,并根据需要在其他地方调用它
- 使用C 11将数组设置为零
- 将数组设置为包含字符串值会导致错误
- 将数字动态数组设置为数组或容器 c++
- 将对象的数组设置为c 中的null
- 在此初始值设定项方法上将整个数组设置为 NULL 是否C++标准?
- 将结构中的字符串数组设置为函数中的us
- C++如何将整个字符数组设置为空格
- 使用 *指针数组C++设置构造函数和析构函数
- 如何将可变大小的静态数组设置为用 0 填充
- 将double-all的二维数组设置为-1的最快方法
- 使用memset将字符串数组设置为0后,该数组不可用
- 在C++中将多维数组设置为向量
- 将浮点数数组设置为相同值的最快方法
- 为什么使用 'new' 创建的数组设置为 NULL?
- c++使用数组设置交集和并集