C++ 数组函数和操作

标签 c++ arrays

好吧,我有一个类实验作业要制作一个程序,该程序应该做很多事情: 1. 名为 convertWeight 的空函数将以磅(int 类型)和盎司为单位的重量转换为以千克(int 类型)和克为单位的等效重量

  1. 一个名为 showArray 的 void 函数带有两个参数:一个基本类型为 int 的数组参数,以及一个用于数组参数大小的按值调用参数。此函数只是打印出数组参数的所有元素,其中两个连续元素由水平制表符分隔。

  2. 在 main() 函数定义中,执行以下操作:

一个。声明一个名为 pounds 的 10 整数数组,将其前 3 个元素初始化为以下值:1、5、10,并将其余元素自动初始化为 0。

编写一个 for 循环,以磅为单位读取 7 个重量,将输入的值存储到最后 7 个 数组磅的元素。

打印出一个提示行“The entire list of weight:”,然后调用函数showArray来显示整个数组磅。

编写另一个调用函数 convertWeight 的 for 循环,将数组磅中给定的每个磅重量转换为以千克和克为单位的等效重量。

这是我想出的:

#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;
}

我是这个数组的新手,我无法理解书中告诉我的内容。能否请您指出我的程序有什么问题并告诉我原因,这样我就知道了。

这是我的错误列表:

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

我们将不胜感激任何帮助!

最佳答案

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

问题就在这里

showArray(pounds[10]);

这不会传递数组,它会尝试访问数组的第 11 个元素(并调用未定义的行为),并将其传递给 showArray(它以指针作为参数)。你想要做的是:

showArray(pounds);

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

问题出在这里:

pounds = pounds[a];

您正试图将一个 int 分配给一个数组。你根本不需要那行:

convertWeight(pounds[a], ounces, kg, grams);

这也修复了这个错误:

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

关于C++ 数组函数和操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20132416/

相关文章:

c++ - 用于非托管 C++ 的 GDI 对象的调试器可视化工具

c++ - 传递数组时类中出现错误指针错误

c++ - 通过指针将多数组传递给函数

javascript - 使用 JavaScript 通过匹配键值和索引值来组合两个 JSON 对象

javascript - jQuery getJson 函数内填充的数组范围

c++ - boost 继续恢复断言失败

c++ - 在 Mac OS X 上使用 Qwt

c++ - boost 序列化 : How To Predict The Size Of The Serialized Result?

c++ - 对运算符定义的误解 C++

javascript - 在数组上迭代数组而不重复