如何将数组的一部分从 int 转换为浮点数并将值设置为另外两个数组

how to convert a part of array from int to float and set the value to another two arrays

本文关键字:数组 设置 两个 浮点数 一部分 int 转换      更新时间:2023-10-16

编辑: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代表什么......