【发布时间】:2018-07-17 03:18:23
【问题描述】:
我刚刚开始第二学期的 CS,我们使用的编程语言是 c++,当然我是新手。我正在编写一个程序,将每月的温度(高温和低温)记录到二维数组中。我之前有一些编程经验,主要是 javascript 和 python,但没有什么特别的。程序编译并成功运行,但我没有得到想要的结果。首先,这是我的完整代码(至此)
#include<iostream>
#include<fstream>
using namespace std;
const int MONTHS = 12;
const int TEMPS = 2;
void getData();
void averageHigh(double arr1[][TEMPS]);
int main()
{
cout << "This program outputs average high and low temperature for a year in Puerto Rico:\n\n";
getData();
return 0;
}
void getData()
{
ifstream tempsHigh_Low;
int i = 0, j = 0;
double temps[MONTHS][TEMPS];
tempsHigh_Low.open("tempsHigh_Low.txt");
if (tempsHigh_Low.is_open())
{
for (i; i < MONTHS; i++)
{
for (j; j < TEMPS; j++)
{
tempsHigh_Low >> temps[i][j];
}
}
}
averageHigh(temps);
}
void averageHigh(double arr1[][TEMPS])
{
double tempsAvgHigh[MONTHS][TEMPS];
double sumElements = 0;
double numElements = MONTHS;
double averageHighTemp;
int v = 0, k = 0;
for (v; v < MONTHS; v++)
{
for (k; k < TEMPS; k++)
{
sumElements = sumElements + tempsAvgHigh[v][k];
}
}
cout << sumElements << endl; //right here, it prints out memory loc and
//and issues out warning...this is a test to find out whether i can
//proceed to take the arithmetic mean of the first column of the array
//averageHigh
}
我想补充一点,这是我第一次使用这个网站,对于任何错误,我提前道歉!提前致谢!
【问题讨论】:
-
该行不打印内存地址。
-
将代码放入编辑器以更好地缩进它,编译器会尖叫着让你听。不要忽略警告。警告意味着您的代码在语法上是正确的并且可以编译,但逻辑看起来很不稳定。编译器是抵御错误的第一道防线,因此请充分利用它告诉您的信息。
标签: c++ function multidimensional-array