Hey Guys,

I am new to the concept of Arrays and I need a little help.
I have included the instructions for the problem as well as the code that I have so far. Can you help me with what I am doing wrong?

Thanks!


Write a program that asks the user to enter a height in inches. Then convert
and output a table showing the height in input inches, centimeters andfeet-
inches. There are 2.54 centimeters to the inch.

For example, if the input is 112 inches then the equivalent measurement in
centimeters is 284.48 and the equivalent measurement in feet and inches will
be 9 feet and 4 inches. If the user enters a negative number then skip the
calculation for centimeters and feet-inches.

Use an array data structure size 20 to store the input inches. Allow the user
to enter at most 20 values using the sentinel 0 to allow the user to stop
before the maximum 20 values are entered. Convert the input measurements to
both centimeters and feet- inches using three more arrays size 20.

Output a table of values that includes only the positive input values, the
equivalent measurement in both centimeters and feet-inches. Also, find the
average of the positive input values and then output the average.

Then print out another table that shows all the input values that are greater
than the calculated average.

See possible output below for the input:
112, 98, -34, 19, 183, -15, 12, 54, 0

-------------------------------------------------------------------
Table of Measurements
_____Inches___Centimeters________Feet_&_Inches
_______112______284.48___________9_______4
________98______248.92___________8_______2
________19_______48.26___________1_______7
_______183______464.82__________15_______3
________12_______30.48___________1_______0
________54______137.16___________4_______6

Average Input value: 79.67 inches

Input Values Above Average
__________112
___________98
__________183

#include <cmath>
 #include <iostream>
  #include <iomanip>
 using namespace std;
	
int main()
 {	
	int ct = 0;
	const int size = 20;
	int temp, inches, sum, ctPos, remInches, feet, howMany, average;
	float cent;
	
	cout << "Please enter height in inches: " << endl;
	cin >> temp;
	
	while ( !(temp < 0 ) && ct < size)
	   {
            inches[ct] = temp;
            ct = ct + 1;
            cout << "Please enter height in inches: " << endl;
            cin >> temp;
        }    

     
     howMany = ct;   
        
       if (howMany !=0)
       {
         for (ct = 0; ct < howMany; ct = ct + 1)
          {
                 cent = 2.54 * inches;
                 feet = inches /12;
                 remInches = inches % 12;
          }
          
          cout << setw(13) << "inches" << setw(10) << "  centimeters" 
          << setw(15) << " feet" << setw(15) << "  inches" << endl; 
          
          for (ct = 0; ct < howMany; ct = ct + 1)
           if ( !([ct] < 0) )
          {
            cout << setw(10) << inches << setw(10) << cent
            << setw(15) << feet << setw(15) << remInches << endl;   
          } 
        
        
        sum = 0
        ctPos = 0
        for (ct = 0; ct < howMany; ct = ct + 1)
        if  ([ct] > 0)
         {
             ctPos = ctPos + 1;
             sum = [ct] + sum;
             average = sum / ctPos;
         }
                 
     cout << setw(13) << "inches" << setw(10) << "  centimeters" 
          << setw(15) << " feet" << setw(15) << "  inches" << endl; 
          
          for (ct = 0; ct < howMany; ct = ct + 1)
            if ([ct] > average)
            {
               cout << setw(10) << inches << setw(10) << cent
              << setw(15) << feet << setw(15) << remInches << endl;   
            } 
                         
       }//end of orginal if statement
       
       else 
       cout << "No Data to Process!" << endl;
       
       system ("pause");    
               
               
}

>Can you help me with what I am doing wrong?
Sure, what's your problem? (be specific!) Are you getting unexpected output?

Well, start from the beginning: what is an array declaration:

int inches[size]; // where size is a constant expression

It's not the best method to learn language: waste a time in asking help on forums instead of read elementary textbook on a very basic language concept.
;)

im guessing that you are getting an error with trying to store into inches[ct]. you might want to recheck on how to declare an array.

>>It's not the best method to learn language: waste a time in asking help on
>>forums instead of read elementary textbook on a very basic language concept.
Absolutely.

To OP: You have declared a integer variable called inches and not an array.
Another point is : Always initialize your variables before you use them.

Also, It is better to use the code tag as :
[code=cpp] //Your code goes here

[/code]

This article has been dead for over six months. Start a new discussion instead.