C++ 将数组传递给函数C++;
我需要帮助创建7个函数来打破这段代码。通过使用“按引用传递”。我不知道如何在函数之间传递多维数组C++ 将数组传递给函数C++;,c++,function,multidimensional-array,pass-by-reference,fstream,C++,Function,Multidimensional Array,Pass By Reference,Fstream,我需要帮助创建7个函数来打破这段代码。通过使用“按引用传递”。我不知道如何在函数之间传递多维数组 #include <fstream> #include <string> #include <iomanip> #include <iostream> #include <cmath> using namespace std; int main() { const int courseSize = 3; const int exam
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const int courseSize = 3;
const int examSize = 6;
int n=0;
int strLength =0;
double scoreTotal =0;
char lettergrade;
string header, name, address, phone, social, course, studentID;
const int letterGrade_A = 90;
const int letterGrade_B = 80;
const int letterGrade_C = 70;
const int letterGrade_D = 60;
int age, yearsAtTXST, numstudents;
double testscore;
double numGrade = 0;
const int minStrLength = 1;
const int maxStrLength = 100;
const int minint = 1;
const int maxint = 100;
const int minCourse = 1;
const int maxCourse = 3;
const int minTest = 1;
const int maxTest = 5;
const double test1 = 0.10;
const double test2 = 0.15;
const double test3 =0.15;
const double test4 = 0.20;
const double final_test = 0.40;
const double maxGrade = 100.0, minGrade = 1.0;
//initialize arrays
string nonNumerical[studsize][strSize];
int numeric1[studsize][numSize];
double numeric2[studsize][courseSize][examSize];
char lettergrades[studsize][courseSize];
cout << fixed << showpoint << setprecision(2);
//opening files
ifstream inputFile;
inputFile.open("Project4_A04314548_Input.txt");
ofstream fout;
fout.open ("Project4_A04314548_Output.txt");
if( !( inputFile && fout))
{
cout << "Error opening file.\n";
}
cout << " Enter a number of students: ";
cin >> numstudents;
while( !(numstudents ==3))
{
cout << " error number of students must be 3. Enter again: ";
cin >> numstudents;
}
//take in string data from input file
for(int x =0; x< studsize; x++)
{
for(int y=0; y < strSize; y++)
{
if(y >= strSize)
break;
getline(inputFile, nonNumerical[x][y]);
strLength = nonNumerical[x][y].length();
if(strLength < minStrLength || strLength > maxStrLength)
{
fout << " The given string for nonNumerical [" << x << "][" << y <<
"] is not within the proper range of 1-100 characters." << endl;
fout << " Please fix the issue and rerun the program for a correct
output." << endl << endl;
continue;
}
}
}
//above put into input data function
//take in data from input file
for(int x=0; x<studsize;x++)
{
for(int y=0; y < numSize; y++)
{
if(y>=numSize)
break;
inputFile >> numeric1[x][y];
if( numeric1[x][y] < minint || numeric1[x][y] > maxint)
{
fout << "The given integer for numerical1[" << x << "][" << y <<
"]is not within the proper range of 1-100." << endl;
fout << " please fix the problem and rerun the program for a correct
output." << endl << endl;
break;
}
}
}
//above put into data function
// take in double data from input file
for( int x1=0; x1<studsize; x1++)
{
for(int x2=0; x2 < courseSize; x2++)
{
for(int x3=0; x3<(examSize-1);x3++)
{
if(x3 >=(examSize-1))
break;
inputFile >> numeric2[x1][x2][x3];
if( numeric2[x1][x2][x3] < minGrade || numeric2[x1][x2][x3] >
maxGrade)
{
cout << "The given value fro numeric2[" << x1 << "][" << x2
<< "][" << x3 << "] is not within the proper range of 1-100" << endl
<< "Please fix the issue and rerun the program for a correct
output." << endl;
break;
}
else if( x3 == 0)
scoreTotal += (numeric2[x1][x2][x3]* test1);
else if( x3== 1)
scoreTotal += (numeric2[x1][x2][x3]* test2);
else if( x3 == 2)
scoreTotal += (numeric2[x1][x2][x3]* test3);
else if( x3 == 3)
scoreTotal += (numeric2[x1][x2][x3]* test4);
else
scoreTotal += (numeric2[x1][x2][x3]* final_test);
}
numeric2[x1][x2][5] = scoreTotal; // final numeric grade
scoreTotal = 0;
if(x2 >=courseSize)
break;
if((numeric2[x1][x2][5] > maxGrade) || (numeric2[x1][x2][5] <
minGrade) )
{
cout << "Error in calculating grade. Please fix the issue
then rerun the program. ignore broken program!";
cout << endl << endl;
continue;
}
else if(numeric2[x1][x2][5] >= letterGrade_A)
lettergrades[x1][x2] = 'A';
else if(numeric2[x1][x2][5] >= letterGrade_B)
lettergrades[x1][x2] = 'B';
else if(numeric2[x1][x2][5] >= letterGrade_C)
lettergrades[x1][x2] = 'C';
else if(numeric2[x1][x2][5] >= letterGrade_D)
lettergrades[x1][x2] = 'D';
else
lettergrades[x1][x2] = 'F';
}
}
for(int a1=0; a1< studsize; a1++)
{ fout << nonNumerical[a1][0]<< endl;
fout << right << setw(35) << "Name of Student:\t";
fout << nonNumerical[a1][1] << endl;
fout << right << setw(35) << "Student ID:\t";
fout << nonNumerical[a1][2]<< endl;
fout << right << setw(35) << "Address:\t" ;
fout << nonNumerical[a1][3] << endl;
fout << right << setw(35) << "Telephone Number:\t";
fout << nonNumerical[a1][4] << endl;
fout << right << setw(35) << "Student Soc. Security:\t";
fout << nonNumerical[a1][5] << endl;
fout << right << setw(35) << "Age:\t";
fout << numeric1[a1][0] << endl;
fout << right << setw(35) << "Number of years at Texas State:\t";
fout << numeric1 [a1][1] << endl << endl;
for(int b1=0; b1 <courseSize; b1++)
{
fout << right << setw(35) << "Course number:\t";
fout << nonNumerical[a1][(b1+6)] << endl;
for(int c1= 0; c1 <(examSize-1); c1++)
{
fout << right << setw(32) << "Exam #" << (a1 +1) << ":\t";
fout << numeric2[a1][b1][c1] << endl;
}
fout << right << setw(35) << "Numerical grade:\t";
fout << numeric2[a1][b1][5] << endl;
fout << right << setw(35) << "Letter grade:\t";
fout << lettergrades[a1][b1] << endl;
if( numeric2[a1][b1][5] < 70)
{
fout << right << setw(14) << " Warning Note: Your grade is too low
and needs improvements!" << endl << endl;
}
else if ( numeric2[a1][b1][5] >= 95)
{
fout << right << setw(14) << " Appreciation Note: Congratulations,
Your performance is Excellent!" << endl << endl;
}
else
fout << endl;
}
fout << endl;
}
inputFile.close();
fout.close();
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
const int courseSize=3;
常数int examize=6;
int n=0;
int strLength=0;
双倍总分=0;
煤焦等级;
字符串标题、姓名、地址、电话、社交、课程、学生ID;
混凝土等级=90;
const int letterGrade_B=80;
混凝土构件等级=70;
混凝土等级=60;
国际年龄,年级,学生;
双倍测试分数;
双numGrade=0;
const int minStrLength=1;
常量int最大长度=100;
常数int minint=1;
常数int maxint=100;
常数int minCourse=1;
常量int maxCourse=3;
常数int minTest=1;
常量int maxTest=5;
常数双测试1=0.10;
常数双测试2=0.15;
常数双测试3=0.15;
常数双测试4=0.20;
常数双最终试验=0.40;
const double maxGrade=100.0,minGrade=1.0;
//初始化数组
字符串非数字[studsize][strSize];
int numeric1[studsize][numize];
双倍数字2[学生人数][课程规模][考试规模];
char lettergrades[studsize][courseSize];
cout(最大长度)
{
fout而不是使用数组
std::string nonNumerical[studsize][strSize];
使用std::vector
std::vector<std::vector<std::string>> nonNumerical(studsize, std::vector<std::string(strSize));
首先,C++通过引用发送数组。
你的问题不清楚……但如果我答对了,你应该发送如下二维数组:
void f1(int a[][20]) ;
关键是,您必须指定第二维度。您可以使用如下指针传递任意大小和维度的数组:
myfunction1(string *nonNumerical)
{
string something=nonNumerical[0][0];
}
不要忘记:你不能直接询问数组的大小。你必须通过数组来传递大小。< /P>两个函数就足以说明你的问题。这只是一个代码的方法。我甚至找不到你的函数显示。你是否要学习其他人都为你做的事情?请注意C++总是通过数组B。y参考请编辑您的答案以便格式化祝您好运通过字符串数组[10][10];
进入该函数。你有什么问题?如果你开始批评和贬低它,请解释一下。忽略返回类型的缺失,假设string*
是string**
,因为替代方法完全错误,string array[10][10]
不会衰减为字符串**
,因此无法进行函数调用。最好的方法是void myfunction1(std::string nonnumeric[][10])
。一维数组将衰减为指针。二维数组将衰减为指向数组的指针,而不是指向指针(或指向指针的指针).我不知道从哪里得到的,但我使用过这个公式,在一维、二维或三维数组中工作得很好,没有任何问题。唯一的限制是,您需要发送正确大小的数组,比如:void myfunction(string*nonnumeric,int-xsize,int-ysize,int-zsize)
您使用的编译器和版本是什么?这是该国长期以来的法律。
void f1(int a[][20]) ;
myfunction1(string *nonNumerical)
{
string something=nonNumerical[0][0];
}