【发布时间】:2013-11-21 21:35:22
【问题描述】:
好的,我在课堂上有一个实验作业,要编写一个程序,该程序应该可以做很多事情: 1. 名为 convertWeight 的 void 函数将以磅(int 类型)和盎司为单位的重量转换为以千克(int 类型)和克为单位的等效重量
名为 showArray 的 void 函数接受两个参数:一个基本类型为 int 的数组参数,以及一个用于数组参数大小的按值调用参数。这个函数简单地打印出数组参数的所有元素,其中两个连续的元素由一个水平制表符分隔。
在 main() 函数定义中,执行以下操作:
一个。声明一个名为磅的 10 个整数的数组,将其前 3 个元素初始化为以下值:1、5、10,并将其余元素自动初始化为 0。
b.编写一个读取 7 个以磅为单位的重量的 for 循环,将输入的值存储到最后 7 个 数组磅的元素。
c。打印出一个提示行“The entire list of weight:”,然后调用函数 showArray 显示整个数组磅数。
d。编写另一个 for 循环,调用函数 convertWeight 以将数组磅中给定的每个重量(磅)转换为千克和克的等效重量。
这是我想出的:
#include <iostream>
using namespace std;
void convertWeight(int pounds, double ounces, int& kg, double& grams);
//Preconditions: parameters pounds and ounces are nonnegative numbers, representing a weight in pounds and ounces
//Postcondition: parameters kg and grams will be set to values of the equivalent weight in kilograms and grams
void showArray(int pounds[10]);
int main()
{
int pounds[10]={1, 5, 10}, i, a, kg;
double ounces, grams;
cout << "Enter 7 additional weights in pounds: \n";
cin >> pounds[3];
for(i = 4; i < 10; i++)
{
cin >> pounds[i];
}
cout << "The entire list of weights: \n";
showArray(pounds[10]);
for(a = 0; a < 10; a++)
{
pounds = pounds[a];
convertWeight(pounds, ounces, kg, grams);
cout << pounds[a] << " pounds = " << kg << " kgs and " << grams << " grams";
}
return 0;
}
void showArray(int pounds[10])
{
cout << pounds[0] << " " << pounds[1] << " " << pounds[2] << " " << pounds[3] << " " << pounds[4] << " " << pounds[5] << " "
<< pounds[6] << " " << pounds[7] << " " << pounds[8] << " " << pounds[9] << " " << pounds[10] << " " ;
}
//Do NOT modify this function definition
void convertWeight(int pounds, double ounces, int& kg, double& grams)
{
const double KGS_PER_POUND = 0.45359237;
const double OUNCES_PER_POUND = 16.0;
const double GRAMS_PER_KG = 1000.0;
double totalKgs;
totalKgs = (pounds + ounces/OUNCES_PER_POUND)*KGS_PER_POUND;
kg = static_cast<int>(totalKgs);
grams = (totalKgs - kg)*GRAMS_PER_KG;
}
我是这个 Array 的新手,我无法理解我的书告诉我的内容。请您指出我的程序有什么问题并告诉我为什么我知道。
这是我的错误列表:
1>c:\users\mackiller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(30): error C2664: 'showArray' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\mackiller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(34): error C2440: '=' : cannot convert from 'int' to 'int [10]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>c:\users\mackiller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(35): error C2664: 'convertWeight' : cannot convert parameter 1 from 'int [10]' to 'int'
1> There is no context in which this conversion is possible
任何帮助将不胜感激!
【问题讨论】:
-
pounds = pounds[a];- 您不能将单个数组元素分配给数组变量本身。只需定义一个名称不冲突的新 int 变量即可。 -
我是使用 for 循环的变量“a”来改变每个循环的值
-
为什么 Stack Overflow 上的这么多 C++ 问题都有某种 War Games inspired 1980 年代风格的命令行界面?此外,尽可能使用
std::array而不是 C 数组。