Okay I have a lab assignment for class to make a program that is suppose to do a number of things: 1. A void function named convertWeight converts a weight in pounds (type int) and ounces to the equivalent weight in kilograms (type int) and grams
A void function named showArray that takes two parameters: one array parameter of base type int, and one call-by-value parameter for the size of the array parameter. This function simply prints out all elements of the array parameter, where two consecutive elements are separated by a horizontal tab.
In the main() function definition, do the followings:
a. declare an array named pounds of 10 integers, initializing its first 3 elements to the following values: 1, 5, 10, and auto-initializing the remaining elements to 0.
b. write a for-loop that reads in 7 weights in pounds, storing the entered values to the last 7 elements of the array pounds.
c. print out one prompt line “The entire list of weight:” and then call the function showArray to display the entire array pounds.
d. write another for-loop that calls the function convertWeight to convert each weight in pounds given in the array pounds to the equivalent weight in kilograms and grams.
This what I came up with:
#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;
}
I am new to this Array thing and I cant get what my book is telling me. Could you please point out what it is that is wrong with my program and tell me why so I know.
here is my error list:
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
Any help givin will be appreciated!
pounds = pounds[a];- You can't assign a single array element into the array variable itself. Just define a new int variable with a non conflicting name.std::arrayand not C arrays whenever possible.